-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path8.2.cpp
55 lines (53 loc) · 1.09 KB
/
8.2.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#include <iostream>
#include <stack>
using namespace std;
#define maxint 32767
int N,M;
int arc[100][100];
int indegree[100];
int cost[100];
int out[100];
int main(){
int a,b;
cin>>N>>M;
for(int i=0;i<N;i++){
cost[i]=0;
indegree[i]=0;
for(int j=0;j<N;j++){
arc[i][j]=maxint;
}
}
for(int i=0;i<M;i++){
cin>>a>>b;
cin>>arc[a][b];
indegree[b]++;
}
stack<int> s;
for(int i=0;i<N;i++)
if(indegree[i]==0)
s.push(i);
int m=0;
while(!s.empty()){
out[m++]=s.top();
int temp=s.top();
s.pop();
for(int j=0;j<N;j++){
if(arc[temp][j]!=maxint){
indegree[j]--;
if(indegree[j]==0)
s.push(j);
if(arc[temp][j]+cost[temp]>cost[j])
cost[j]=arc[temp][j]+cost[temp];
}
}
}
if(m<N) cout<<"Impossible";
else{
int max=0;
for(int i=0;i<N;i++){
if(cost[i]>max) max=cost[i];
}
cout<<max;
}
return 0;
}