八、图(下):How Long Does It Take

2023-12-30 14:58
文章标签 long take

本文主要是介绍八、图(下):How Long Does It Take,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

目录

  • 题目描述
  • 代码
  • 解题思路

题目描述

Given the relations of all the activities of a project, you are supposed to find the earliest completion time of the project.

Input Specification:
Each input file contains one test case. Each case starts with a line containing two positive integers N (≤100), the number of activity check points (hence it is assumed that the check points are numbered from 0 to N−1), and M, the number of activities. Then M lines follow, each gives the description of an activity. For the i-th activity, three non-negative numbers are given: S[i], E[i], and L[i], where S[i] is the index of the starting check point, E[i] of the ending check point, and L[i] the lasting time of the activity. The numbers in a line are separated by a space.

Output Specification:
For each test case, if the scheduling is possible, print in a line its earliest completion time; or simply output “Impossible”.

Sample Input 1:

9 12
0 1 6
0 2 4
0 3 5
1 4 1
2 4 1
3 5 2
5 4 0
4 6 9
4 7 7
5 7 4
6 8 2
7 8 4

Sample Output 1:
18
Sample Input 2:

4 5
0 1 1
0 2 2
2 1 3
1 3 4
3 2 5

Sample Output 2:

Impossible

代码

#include<stdio.h>
#include<stdbool.h>
#include<malloc.h> 
#define MaxCheckNum 100
typedef int CheckPoint;
typedef int WeightType;
int Indegree[MaxCheckNum] = {0}; /* 入度数组,初始化为0 */
int Day[MaxCheckNum] = {0};
CheckPoint TopOrder[MaxCheckNum];typedef struct ENode *Edge;
struct ENode{CheckPoint P1;CheckPoint P2;WeightType Weight;
};typedef struct AdjNode *PtrToAdjNode;
struct AdjNode{CheckPoint P;PtrToAdjNode Next;WeightType Weight;
};typedef struct HNode{PtrToAdjNode FirstEdge;
}Head[MaxCheckNum];typedef struct LNode *LGraph;
struct LNode{int Nv;int Ne;Head G;
};LGraph CreateGraph(int N)
{CheckPoint i;LGraph Graph = (LGraph)malloc(sizeof(struct LNode));Graph->Nv = N;Graph->Ne = 0;for(i=0; i!=N; ++i){Graph->G[i].FirstEdge = NULL;}return Graph;
}void InsertEdge(LGraph Graph, Edge E)
{PtrToAdjNode Adj = (PtrToAdjNode)malloc(sizeof(struct AdjNode));Adj->P = E->P2;Adj->Next = Graph->G[E->P1].FirstEdge;Adj->Weight = E->Weight;Graph->G[E->P1].FirstEdge = Adj;++Indegree[E->P2];/* 入度增加 */
}LGraph BuildGraph()
{int Nv, i;scanf("%d",&Nv);LGraph Graph = CreateGraph(Nv);scanf("%d",&Graph->Ne);Edge E = (Edge)malloc(sizeof(struct ENode));for(i=0; i!=Graph->Ne; ++i){scanf("\n%d %d %d",&E->P1,&E->P2,&E->Weight);InsertEdge(Graph, E);}return Graph;
}/* 实现队列 */
CheckPoint Que[MaxCheckNum] = {0};
int first = 0;
int last = -1;bool TopSort(LGraph Graph)
{CheckPoint i;int cnt;PtrToAdjNode W;for(i=0; i!=Graph->Nv; ++i){/* 查找入度为0的检查点,入队列 */if(Indegree[i] == 0)Que[++last] = i;}cnt = 0;while(first<=last){/* 队列不为空 */i = Que[first++];/* 弹出一个入度为0的顶点 */TopOrder[cnt++] = i;/* 存为结果序列的下一个元素 */for(W=Graph->G[i].FirstEdge; W!=NULL; W=W->Next){if(W->Weight+Day[i]>Day[W->P]) /* 更新到达W->P检查点的最小时间 */Day[W->P] = W->Weight+Day[i];if(--Indegree[W->P]==0)Que[++last] = W->P;}}if(cnt != Graph->Nv )return false;elsereturn true;
}int main()
{LGraph Graph = BuildGraph();if(TopSort(Graph)){int i = 0,max = 0;for(i=0;i!=Graph->Nv;++i)if(Day[i]>max)max = Day[i];printf("%d",max);}elseprintf("Impossible");
}

解题思路

拓扑排序:
1.用邻接表的方式建图。
2.建立入度数组,通过图可以获得每个结点的入度。
3.入度为0的结点进入一个队列。
4.结点出队列,进入排序数组。出队列的结点的邻接点入度都减1.如果减1后入度变成0了,就把这个邻接点入队列。
计算时间:
增加一个数组Day[ ],储存每个结点的日期,初始都为0。在拓扑排序的第4步,如果出队列的结点的日期Day[i],加上到邻接点的需要耗费的时间W->Weight,大于邻接点原来的日期Day[W->P],就更新邻接点的日期。
最后遍历Day[ ]数组,输出其中的最大值。

这篇关于八、图(下):How Long Does It Take的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/553210

相关文章

long long,_int64使用小结

前言:   在16位环境下,int/unsigned int 占16位,long/unsigned long占32位   在32位环境下,int占32位,unsigned int占16位,long/unsigned long占32位 何时需要使用:   long 和 int 范围是[-2^31,2^31),即-2147483648~2147483647,而unsigned范围是[0,2^32),

《长得太长也是错?——后端 Long 型 ID 精度丢失的“奇妙”修复之旅》

引言 在前后端分离的时代,我们的生活充满了无数的机遇与挑战——包括那些突然冒出来的让人抓狂的 Bug。今天我们要聊的,就是一个让无数开发者哭笑不得的经典问题:后端 Long 类型 ID 过长导致前端精度丢失。说到这个问题,那可真是“万恶之源”啊,谁让 JavaScript 只能安全地处理 Number.MAX_SAFE_INTEGER(也就是 9007199254740991)以内的数值呢?

踩坑记录(Long[]ids)

主要针对Long[] ids 的判空问题 问题代码 public void delYnjC(Long[] ids) {if (CollectionUtils.isEmpty(Collections.singleton(ids))) {throw new NullPointerException("参数不能为空");}naturalYnjCMapper.delYnjC(ids);} 修正

CodeForces 407B Long Path

题意: 有n+1个格子  起点在1  每个格子有个前进1的门  前n个格子有个返回的门(返回前面某个格子)  奇数次走进一个格子就去走返回门  偶数次走前进门  问  走到n+1要走过几道门 思路: 一看就是DP to[i]表示第i个格子的返回门 go[i]表示离开第i个格子需要的步数 sum[i]表示离开前i个格子需要的步数 明显  go[i]=sum[i-1]-sum[to

Command line is too long. Shorten command line for DisplayApplication (1) or

微服务项目启动类起不来,如下 解决办法:IEDA开发环境下 找到你的项目下面的.idea\workspace.xml 添加一个property : <property name="dynamic.classpath" value="true" /> 帮同事看的问题,自己测试没问题就关闭了。图片借用网上的。 参考:参考

[C++] 将LONG类型的color值转换为RGB值

转换原理: The calculation is: (65536 * Blue) + (256 * Green) + (Red) 'Convert RGB to LONG: LONG = B * 65536 + G * 256 + R       'Convert LONG to RGB:  B = LONG \ 65536  G = (LONG - B * 65536) \ 256  R =

Command line is too long. Shorten command line for IhswfldWebApplication or also for Spring Boot ..

工作中,使用idea启动服务时报错如下图: 解决方法:打开启动配置页,选择 JAR mainfest, 点击 Apply, 点击 OK. 再次启动项目

论文笔记:LONG-FORM FACTUALITY IN LARGE LANGUAGE MODELS

Abstract 当前存在问题,大模型在生成关于开放主题的事实寻求问题的时候经常存在事实性错误。 LongFact 创建了LongFact用于对各种主题的长形式事实性问题进行基准测试。LongFact是一个prompt集包含38个领域的数千条提示词,使用GPT-4生成。 Search-Augmented Factuality Evaluator(SAFE) SAFE利用大模型将一个相应拆

dt中ID转化为long[]

1、dt中ID转化为long[]             long[] ids = new long[dataids.Rows.Count];             int index = 0;             foreach (DataRow dr in dataids.Rows)             {                 if (in