topic:
analysis:
Determining whether the topological sort to complete the project (i.e., determines whether there is a loop), if it can complete the project, the required maximum time needed to complete the project, it is worth noting that the maximum time n-1 is not necessarily at this point, could 0 to any point in the n-1, the last cycle time required to find the longest time.
Code:
#include <iostream>
#include <cstring>
#include <cstdio>
#include <queue>
#include <vector>
#define INF 0x3f3f3f3f
using namespace std;
const int MAXN = 105;
int n,m;
int ve[MAXN],inDegree[MAXN];
int mat[MAXN][MAXN];
queue<int> q;
bool Topological()
{
int cnt = 0;
for(int i=0;i<n;++i)
if(inDegree[i]==0)
q.push(i);
while(!q.empty())
{
int now = q.front();
cnt++;
q.pop();
for(int i=0;i<n;++i)
{
if(mat[now][i]!=INF)
{
inDegree[i]--;
if(inDegree[i] == 0)
q.push(i);
if(ve[now]+mat[now][i] > ve[i])
ve[i] = ve[now] + mat[now][i];
}
}
}
if(cnt == n) return true;
else return false;
}
int main()
{
scanf("%d%d",&n,&m);
int st,ed,ti;
memset(mat,INF,sizeof(mat));
for(int i=1;i<=m;++i)
{
scanf("%d%d%d",&st,&ed,&ti);
mat[st][ed] = ti;
inDegree[ed]++;
}
bool flag = Topological();
int ans = 0;
if(!flag)
printf("Impossible\n");
else
{
for(int i=0;i<n;++i)
ans = max(ans,ve[i]);
printf("%d\n",ans);
}
return 0;
}