UPC 组队训练 C:Coolest Ski Route(DFS +DP优化)

问题 C: Coolest Ski Route
时间限制: 1 Sec 内存限制: 128 MB
提交: 120 解决: 33
[提交] [状态] [讨论版] [命题人: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
这里写图片描述
题意:给定一个有向图寻找点i到点j之间的最大距离
1)从入度为0的点开始处理,初度为0的结束
2)Floyd算法会超时
3)DFS深搜,但是过程中用dp数组记录途经的点且以此点开始的情况下最长路径。

#include<bits/stdc++.h>
using namespace std;

int mapp[1005][1005];
int vis[1005];
int flag[1005];
int sum,maxx;
int n,m;
int dp[1005];
int dfs(int a)
{
    if(vis[a]) return dp[a];
    for(int i=1;i<=n;i++)
    {
        if(mapp[a][i])
            dp[a]=max(dp[a],dfs(i)+mapp[a][i]);
    }
    vis[a]=1;
    return dp[a];
}

int main()
{
    cin>>n>>m;
    memset(mapp,0,sizeof(mapp));
    memset(vis,0,sizeof(vis));
    memset(flag,0,sizeof(flag));
    memset(dp,0,sizeof(dp));
    int s,t,c;
    for(int i=0;i<m;i++)
    {
        scanf("%d %d %d",&s,&t,&c);
        flag[t]++;
        if(mapp[s][t])
            mapp[s][t]=max(mapp[s][t],c);
        else mapp[s][t]=c;
    }
    maxx=0;
    for(int i=1;i<=n;i++)
    {
        if(flag[i]==0)
        {
            maxx=max(maxx,dfs(i));
        }
    }
    cout<<maxx<<endl;
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值