How Long Does It Take How Long Does It Take

英文不是很好的我看了好久才知道什么意思How Long Does It Take
How Long Does It Take

其中我还 调试了一下, 因为没有考虑到多起点, 多终点的情况。考虑到就很简单啦。
除此之外哦,还有就是如何验证图里是否有回路。在这里我用到的是,一个个记录去掉为0的节点,记录他们,
直至没有入度为0的点,然后看看所有的点是否都被记录过,若存在既没有入度为0的点且还有点没被记录过,
则存在回路。

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 N1), 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
 1 #include<iostream> 
 2 #include<vector>
 3 #include<queue>
 4 using namespace std;
 5 #define maxNv 100
 6 #define maxtime 100000
 7 int flag=1;
 8 vector<int> earliest(maxNv,0);
 9 queue<int> q;
10 vector<int> rudu(maxNv,0);
11 struct graph{
12     int Nv,Ne;
13     int G[maxNv][maxNv];
14 };
15 using Graph=graph*;
16 struct enode{
17     int v1,v2;
18     int time;
19 };
20 using edge=enode*;
21 Graph CreateGraph()
22 {
23     Graph gra=new graph();
24     cin>>gra->Nv>>gra->Ne;
25     for(int i=0;i<gra->Nv;i++)
26     for(int j=0;j<gra->Nv;j++)
27     gra->G[i][j]=maxtime;
28     return gra;
29 }
30 void Insert(edge e,Graph gra){
31     gra->G[e->v1][e->v2]=e->time;
32     rudu[e->v2]++;
33 }
34 Graph BuildGraph(){
35     Graph gra=CreateGraph();
36     edge e=new enode();
37     for(int i=0;i<gra->Ne;i++){
38         cin>>e->v1>>e->v2>>e->time;
39         Insert(e,gra);
40     }
41     return gra;
42 }
43 void timeset(int v1,Graph gra){
44     int f=0;
45     for(int i=0;i<gra->Nv;i++)
46     if(gra->G[i][v1]!=maxtime&&earliest[i]+gra->G[i][v1]>f)
47     f=earliest[i]+gra->G[i][v1];
48     earliest[v1]=f;
49 }
50 void solve(Graph gra){
51     int v1=0,v2=0;
52     for(int i=0;i<gra->Nv;i++)
53     if(rudu[i]==0) {
54     q.push(i);
55     earliest[0]=0;    
56     }
57     while(!q.empty()){
58         v1=q.front();
59         q.pop();
60         rudu[v1]--;
61     timeset(v1,gra);
62     for(v2=0;v2<gra->Nv;v2++)
63     if(gra->G[v1][v2]!=maxtime&&rudu[v2]!=-1){
64         rudu[v2]--;
65         if(rudu[v2]==0) q.push(v2);}
66     }
67 //    for(int i=0;i<gra->Nv;i++)
68 //    cout<<rudu[i]<<" ";
69 //    cout<<endl;
70     for(int i=0;i<gra->Nv;i++)
71     if(rudu[i]!=-1) flag=0;
72     if(flag==0) cout<<"Impossible";
73     else{
74         int time=-1;
75         for(int i=0;i<gra->Nv;i++)
76         if(earliest[i]>time) time=earliest[i];
77         cout<<time;
78     }
79 }
80 int main()
81 {
82     Graph gra=BuildGraph();
83     solve(gra);
84     return 0;
85 }
86 } } 
87 int main() { 
88 Graph gra=BuildGraph();
89  solve(gra);
90  return 0; } 
View Code