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

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/a17865569022/article/details/82497278

问题 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;
}

猜你喜欢

转载自blog.csdn.net/a17865569022/article/details/82497278
ski