问题 C: Coolest Ski Route(dfs优化)

问题 C: Coolest Ski Route

http://exam.upc.edu.cn/problem.php?id=8000&csrf=2JOsy0TloBulQqEqOaC4Z2IM4eHIBJwO

时间限制: 1 Sec  内存限制: 128 MB
提交: 113  解决: 32
[提交] [状态] [讨论版] [命题人: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

题意:滑雪。有n个点,m个坡,都是下坡,即每个点只能访问一次。m行,每行s,t,c,代表s->t的距离是c(注意单向)。

问你最长的路线。

思路:求单向最长路。刚开始想的是dfs所有入度为0的点,取最大值,但是超时了。。。

   后来限制了dfs,不让他遍历判断所有点。利用容器,记录一个点的下限即可。

代码:

#include
#include
#include
#include
#include
#include
#define inf 0x3f3f3f3f
#include
using std::pair;
typedef pair s;

using namespace std;
vector mapp[1005];

int vis[1005];
int flag[1005];
int dis[1005];
int sum,maxx;
int n,m;

void dfs(int a)
{
    for(int i=0;i>n>>m;
    memset(flag,0,sizeof(flag));
    memset(dis,0,sizeof(dis));
    for(int i=0;i>s>>t>>c;
        flag[t]=1;
        mapp[s].push_back(make_pair(t,c));
    }
    for(int i=1;i<=n;i++)
    {
        if(flag[i]==0)
        {
            //sum=0;
            dfs(i);
            //memset(vis,0,sizeof(vis));
        }
    }
    maxx=0;
    for(int i=1;i<=n;i++)
        //cout<

 

你可能感兴趣的:(算法,比赛,UPC)