In this winter holiday, Bob has a plan for skiing at the mountain resort.
This ski resort has M different ski paths and N different flags situated at those turning points.
The i-th path from the Si-th flag to the Ti-th flag has length Li.
Each path must follow the principal of reduction of heights and the start point must be higher than the end point strictly.
An available ski trail would start from a flag, passing through several flags along the paths, and end at another flag.
Now, you should help Bob find the longest available ski trail in the ski resort.
Input Format
The first line contains an integer T, indicating that there are T cases.
In each test case, the first line contains two integers N and M where 0<N≤10000 and 0<M≤100000as described above.
Each of the following M lines contains three integers Si, Ti, and Li (0<Li<1000) describing a path in the ski resort.
Output Format
For each test case, ouput one integer representing the length of the longest ski trail.
样例输入
1 5 4 1 3 3 2 3 4 3 4 1 3 5 2
样例输出
6
题目来源
题意:有向图求最长路
解题思路:DP求最长路,用到了Floyd的思想,把每一点的最长路都求一遍,记录最大即可
#include<iostream>
#include<deque>
#include<memory.h>
#include<stdio.h>
#include<map>
#include<string>
#include<algorithm>
#include<vector>
#include<math.h>
#include<stack>
#include<queue>
#include<set>
#define INF 1<<29
#define MAXN 100005
using namespace std;
const int MAXV=100005;
int max(int a,int b){
return a>b?a:b;
}
int dp[100005];
struct edge{
int v1,v2,w,next;
}e[MAXV];
int edge_num;
int head[MAXV];
void insert_edge(int v1,int v2,int w){
e[edge_num].v1=v1;
e[edge_num].v2=v2;
e[edge_num].w=w;
e[edge_num].next=head[v1];
head[v1]=edge_num++;
}
int dr(int i)
{
if(dp[i])
return dp[i];
for(int j=head[i];j!=-1;j=e[j].next)
dp[i]=max(dp[i],dr(e[j].v2)+e[j].w);
return dp[i];
}
int main() {
int t;
scanf("%d",&t);
while(t--){
int a,m;
memset(dp,0,sizeof(dp));
edge_num=0;
memset(head,-1,sizeof(head));
scanf("%d%d",&a,&m);
int t1,t2,t3;
for(int i=0;i<m;i++){
scanf("%d%d%d",&t1,&t2,&t3);
insert_edge(t1,t2,t3);
}
int ans=0;
for(int i=1;i<=a;i++)
ans=max(ans,dr(i));
printf("%d\n",ans);
}
return 0;
}