zoukankan      html  css  js  c++  java
  • 2017 ACM-ICPC 亚洲区(乌鲁木齐赛区)网络赛 H Skiing【拓扑排序,关键路径】

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

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

    This ski resort has MM different ski paths and NN different flags situated at those turning points.

    The ii-th path from the S_iSi​​-th flag to the T_iTi​​-th flag has length L_iLi​​.

    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 Format

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

    In each test case, the first line contains two integers NN and MM where 0 < N leq 100000<N10000 and 0 < M leq 1000000<M100000as described above.

    Each of the following MM lines contains three integers S_iSi​​, T_iTi​​, and L_i~(0 < L_i < 1000)Li​​ (0<Li​​<1000) describing a path in the ski resort.

    Output Format

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

    样例输入

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

    样例输出

    6

    http://blog.csdn.net/yo_bc/article/details/77917288

    给定一个有向无环图,求最长路径。
    由于是有向无环图,所以最长路肯定是一个入度为0到出度为0的路径,拓扑排序在确定当前点之前能够考虑到所有到它的情况,所以最后取个最值即可。
     1 #include<iostream>
     2 #include<cstdio>
     3 #include<cstring>
     4 #include<algorithm>
     5 #include<queue>
     6 using namespace std;
     7 const int maxn=1e4+10;
     8 vector<pair<int,int> >edge[maxn];
     9 int T,n,m;
    10 int deg[maxn],val[maxn];
    11 queue<int> q;
    12 
    13 void init()
    14 {
    15     for(int i=0;i<=n;i++){
    16         edge[i].clear();
    17     }
    18     memset(val,0,sizeof(val));
    19     memset(deg,0,sizeof(deg));
    20 }
    21 
    22 void toposort()
    23 {
    24     while(!q.empty()) q.pop();
    25     for(int i=0;i<=n;i++){
    26         if(!deg[i]) q.push(i);
    27     }
    28     while(!q.empty())
    29     {
    30         int u=q.front();q.pop();
    31         for(int i=0;i<edge[u].size();i++){
    32             int v=edge[u][i].first;
    33             val[v]=max(val[v],val[u]+edge[u][i].second);
    34             if(--deg[v]==0) q.push(v);
    35         }
    36     }
    37     printf("%d
    ",*max_element(val+1,val+1+n));
    38 }
    39 
    40 int main()
    41 {
    42     scanf("%d",&T);
    43     while(T--)
    44     {
    45         scanf("%d%d",&n,&m);
    46         init();
    47         int u,v,w;
    48         for(int i=0;i<m;i++){
    49             scanf("%d%d%d",&u,&v,&w);
    50             edge[u].push_back(make_pair(v,w));
    51             deg[v]++;
    52         }
    53         toposort();
    54     }
    55     return 0;
    56 }





  • 相关阅读:
    Vue中过度动画效果应用
    最小公倍数和最大公约数求解方法
    Vue实现双向绑定的原理以及响应式数据
    Hive SQL语法总结
    java安装配置
    Ubuntu vmware补丁
    centos6 安装tensorflow
    python hive
    python 连接 hive
    ubuntu下python通过cx_Oracle库访问oracle数据库
  • 原文地址:https://www.cnblogs.com/zxhyxiao/p/7515757.html
Copyright © 2011-2022 走看看