计蒜客 Skiing

来源:互联网 发布:淘宝客软文推广 编辑:程序博客网 时间:2024/06/05 21:06

题意:

        求最长路,记忆化搜索。

In this winter holiday, Bob has a plan for skiing at the mountain resort.

This ski resort has MM different ski paths and NN different flags situated at those turning points.

The ii-th path from the SiSi-th flag to the TiTi-th flag has length LiLi.

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 TT, indicating that there are TT cases.

In each test case, the first line contains two integers NN and MM where 0<N≤100000<N10000 and 0<M≤1000000<M100000 as described above.

Each of the following MM lines contains three integers SiSiTiTi, and Li (0<Li<1000)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.

样例输入

15 41 3 32 3 43 4 13 5 2

样例输出

6

题目来源

2017 ACM-ICPC 亚洲区(乌鲁木齐赛区)网络赛

#include<iostream>#include<vector>#include<algorithm>#include<cstdio>#include<cstring>#include<queue>using namespace std;struct node{    int next,dist;    node(int a,int b){next=a,dist=b;}};int num[10005];vector<node>G[10005];int dfs(int x){    if(num[x]) return num[x];    for(int i=0;i<G[x].size();i++)    {        int nx=G[x][i].next,dis=G[x][i].dist;        num[x]=max(num[x],dis+dfs(nx));    }    return num[x];}int main(){    int t;    int n,m;    int a,b,c;    scanf("%d",&t);    while(t--)    {        memset(num,0,sizeof(num));        scanf("%d%d",&n,&m);        for(int i=0;i<=n;i++)            G[i].clear();        while(m--)        {            scanf("%d%d%d",&a,&b,&c);            G[a].push_back(node(b,c));        }        int ans=0;        for(int i=1;i<=n;i++)            ans=max(ans,dfs(i));        printf("%d\n",ans);    }    return 0;}


原创粉丝点击