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≤10000and 0<M≤100000 as 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只要把每个子状态的最优求出就行
#include <iostream>
#include <vector>
#include <algorithm>
#include <cstring>
using namespace std;
#define maxn 100100
struct No
{
vector<int> len;
vector<int> next;
} ;
No node[maxn];
int ans[maxn];
int cal(int a){
if(ans[a] != 0)return ans[a];
for(int i=0; i<node[a].len.size(); i++){
ans[a] = max( (node[a].len[i]+cal(node[a].next[i])), ans[a]);
}
return ans[a];
}
int main()
{
int T;
scanf("%d", &T);
while(T--){
memset(node, 0, sizeof(node));
memset(ans, 0, sizeof(ans));
int m, n;
scanf("%d%d", &n, &m);
int temp1, temp2, temp3;
for(int i=0; i<m; i++){
scanf("%d%d%d", &temp1, &temp2, &temp3);
node[temp1].next.push_back(temp2);
node[temp1].len.push_back(temp3);
}
int res = 0;
for(int i=0; i<n; i++){
res = max(cal(i), res);
}
cout<<res<<endl;
}
return 0;
}