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

Problem Description

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

This ski resort has M different ski paths and N different flags situated at those turning points.

The ii-th path from the Si​-th flag to the Ti​-th flag has length Li​.

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

The first line contains an integer T, indicating that there are T cases.

In each test case, the first line contains two integers N and M where 0<N≤10000 and 0<M≤100000as described above.

Each of the following M lines contains three integers Si​, Ti​, and Li​ (0<Li​<1000) describing a path in the ski resort.

Output

For each test case, ouput one integer representing the length of the longest ski trail.

Sample Input

1
5 4
1 3 3
2 3 4
3 4 1
3 5 2

​​​​​​​Sample ​​​​​​​Output

6

题意:t 组数据,每组给出一个 n 个点 m 条边的带权有向图,问整个图的最长路

思路:DAG 图求最长路,利用拓扑排序来解决即可

Source Program

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<string>
#include<cstring>
#include<cmath>
#include<ctime>
#include<algorithm>
#include<utility>
#include<stack>
#include<queue>
#include<vector>
#include<set>
#include<map>
#include<bitset>
#define EPS 1e-9
#define PI acos(-1.0)
#define INF 0x3f3f3f3f
#define LL long long
#define Pair pair<int,int>
const int MOD = 1E9+7;
const int N = 50000+5;
const int dx[] = {1,-1,0,0,-1,-1,1,1};
const int dy[] = {0,0,-1,1,-1,1,-1,1};
using namespace std;


struct Node{
    int to,dis;
    Node(){}
    Node(int to,int dis):to(to),dis(dis){}
};
vector<Node> G[N];
int in[N];
int dis[N];
int n,m;

void topSort() {
    stack<int > S;
    for(int i=1; i<=n; i++)
        if(!in[i])
            S.push(i);


    while(!S.empty()) {
        int x=S.top();
        S.pop();
        for(int j=0; j<G[x].size(); j++) {
            int y=G[x][j].to;
            dis[y]=max(dis[y],dis[x]+G[x][j].dis);
            in[y]--;
            if(!in[y])
                S.push(y);

        }
    }
}

int main() {
    int T;
    scanf("%d",&T);
    while(T--){
        scanf("%d%d",&n,&m);
        memset(in,0,sizeof(in));
        memset(dis,0,sizeof(dis));
        for(int i=0; i<=n; i++)
            G[i].clear();

        for(int i=1; i<=m; i++) {
            int x,y,dis;
            scanf("%d%d%d",&x,&y,&dis);

            Node temp;
            temp.to=y;
            temp.dis=dis;
            in[y]++;
            G[x].push_back(temp);
        }
        topSort();

        int res=-INF;
        for(int i=1;i<=n;i++)
            res=max(res,dis[i]);
        printf("%d\n",res);
    }

    return 0;
}

 

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值