[SSL] The troubles of the 1762 factory
Time Limit:1000MS
Memory Limit:65536K
Description
A factory found that the machines in the factory consume a lot of raw materials when producing products, that is, a lot of raw materials become waste. Therefore, the factory wants to find the production line that consumes the largest amount of raw materials for transformation in order to reduce costs. The production line in the factory is a directed acyclic network, with N machines representing N nodes in the network. The arc <I, j> (i <j) represents the loss amount of raw materials transferred from machine i to machine j.
Input
The first line is two integers N, M (N<=100, M<=1000), which respectively represent the number of nodes and arcs of the network. From the second line to M+1 line, each line contains three integers A, B, C, indicating that the loss on the arc is C.
Output
Only an integer is the loss of the line with the largest loss.
Sample Input
5 5
1 2 2
2 4 9
1 3 7
3 4 1
4 5 6
Sample Output
17
Ideas
Use floyd to find the longest way.
Code
#include<iostream>
#include<cstdio>
#include<queue>
#include<cstring>
#include<cmath>
#include<queue>
#include<cstring>
using namespace std;
int n,m;
int a[2010][2010];
void input()
{
int i,j,x,y,z;
memset(a,-1,sizeof(a));
scanf("%d%d",&n,&m);
for(i=1;i<=m;i++)
{
scanf("%d%d%d",&x,&y,&z);
a[x][y]=max(a[x][y],z);
}
for(i=1;i<=n;a[i][i]=0,i++);
return;
}
void floyd()//求多源最长路
{
int i,j,k;
for(k=1;k<=n;k++)
for(i=1;i<=n;i++)
for(j=1;j<=n;j++)
if(a[i][k]>-1&&a[k][j]>-1&&a[i][k]+a[k][j]>a[i][j])
a[i][j]=a[i][k]+a[k][j];
return;
}
int main()
{
int i,j,ans=0;
input();
floyd();
for(i=1;i<=n;i++)//找最大值
for(j=1;j<=n;j++)
ans=max(ans,a[i][j]);
printf("%d",ans);
return 0;
}