Coolest Ski Route

                                                         Coolest Ski Route

                                                                         时间限制: 1 Sec  内存限制: 128 MB
                                                                                   提交: 152  解决: 39
                                                                     提交] [状态] [讨论版] [命题人:admin]

题目描述

John loves winter. Every skiing season he goes heli-skiing with his friends. To do so, they rent a helicopter that flies them directly to any mountain in the Alps. From there they follow the picturesque slopes through the untouched snow.
Of course they want to ski on only the best snow, in the best weather they can get. For this they use a combined condition measure and for any given day, they rate all the available slopes.
Can you help them find the most awesome route?

输入

The input consists of:
•one line with two integers n (2 ≤ n ≤ 1000) and m (1 ≤ m ≤ 5000), where n is the number of (1-indexed) connecting points between slopes and m is the number of slopes.
•m lines, each with three integers s, t, c (1 ≤ s, t ≤ n, 1 ≤ c ≤ 100) representing a slope from point s to point t with condition measure c.
Points without incoming slopes are mountain tops with beautiful scenery, points without outgoing slopes are valleys. The helicopter can land on every connecting point, so the friends can start and end their tour at any point they want. All slopes go downhill, so regardless of where they start, they cannot reach the same point again after taking any of the slopes.

输出

Output a single number n that is the maximum sum of condition measures along a path that the friends could take.

样例输入

5 5
1 2 15
2 3 12
1 4 17
4 2 11
5 4 9

样例输出

40

提示

来源/分类

GCPC2018 

题意:

看起来很复杂,其实就是在有向图中找一条最长的路

刚开始用最短路的各种方法,各种WA,TLE,这么简单的一道题居然做成这个样子,好伤心

最后自己写了个dfs又超时了一发,简直了,马上又剪枝了一下,终于过啦~~~~~~

菜~~~

代码:

#include<bits/stdc++.h>
#define MAX 1005
using namespace std;
struct node
{
    int v,c;
    node () {}
    node(int x,int y)
    {
        v=x;
        c=y;
    }
};
int maxx;
vector<node>g[MAX];
int dis[1003];
int n,m;
int maxxx;
void  Dijkstra(int i)
{
    for(int j=0; j<g[i].size(); j++)
    {
        node u=g[i][j];
        if( dis[u.v]<dis[i]+u.c)
            dis[u.v]=dis[i]+u.c,maxxx=max(maxxx,dis[u.v]);
        else continue;//这句不加超时,在该点时之前已经计算过到该点更长的距离了,所以在以在此轮与该点相连的其他点不用再计算;
        Dijkstra(u.v);
    }
    return ;
}

int main()
{
    int x,y,z,i;
    while(~scanf("%d%d",&n,&m))
    {
        for (i=1; i<=m; i++)
        {
            scanf("%d%d%d",&x,&y,&z);
            g[x].push_back(node(y,z));
        }
        for(int j=1; j<=n; j++)  dis[j]=0;
        maxx=-1;
        for(int i=1; i<=n; i++)
        {
            maxxx=-1;
            Dijkstra(i);
            if(maxxx>maxx)maxx=maxxx;
        }
        printf("%d\n",maxx);
    }

    return 0;
}

猜你喜欢

转载自blog.csdn.net/wrwhahah/article/details/82502136
ski