zoukankan      html  css  js  c++  java
  • 2017 ACM-ICPC 亚洲区(乌鲁木齐赛区)网络赛 H. Skiing (拓扑排序+假dp)

    题目链接:https://nanti.jisuanke.com/t/16957

    题目:

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

    This ski resort has MMM different ski paths and NNN different flags situated at those turning points.

    The iii-th path from the SiS_iSi-th flag to the TiT_iTi-th flag has length LiL_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 TTT, indicating that there are TTT cases.

    In each test case, the first line contains two integers NNN and MMM where 0<N≤100000 < N leq 100000<N10000 and 0<M≤1000000 < M leq 1000000<M100000 as described above.

    Each of the following MMM lines contains three integers SiS_iSi, TiT_iTi, and Li (0<Li<1000)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
    题意:在这个寒假中,鲍勃有计划在山区度假胜地滑雪。这个滑雪胜地有M个不同的滑雪道和N个不同的标志位于那些转弯处点。从第S标记到第T标志的第i路径具有长度L。每个路径必须遵循降低高度的原则,起点必须严格高于终点。 现在,你应该帮助鲍勃找到最长的滑雪道。
    思路:本题的思路是用拓扑排序进行判断路径,因为入度为0的点一定是一个起点,然后逐渐往后推,然后用dp数组来储存所有可以到达改点的最远距离,虽然我用的是dp数组,但是这个其实不是dp。不过为了储存两个标志之间的距离,我们将通常的topsort的vector稍微改一下,下标不储存一个值,而是储存起点和长度,因为开个1w*1w的二维数组空间+时间都有点炸。最后遍历一遍,求出所有标志的dp的最大值即可。

    代码实现如下:
     1 #include <queue>
     2 #include <cstdio>
     3 #include <vector>
     4 #include <cstdlib>
     5 #include <cstring>
     6 #include <algorithm>
     7 using namespace std;
     8 
     9 const int maxn = 1e4 + 7;
    10 int t, n, m, u, v, w;
    11 int dp[maxn], in[maxn];
    12 vector<pair<int, int>> G[maxn];
    13 
    14 void topsort() {
    15     queue<int> q;
    16     for(int i = 1; i <= n; i++) {
    17         if(in[i] == 0) q.push(i);
    18     }
    19     int x;
    20     while(!q.empty()) {
    21         x = q.front(), q.pop();
    22         int k = G[x].size();
    23         for(int i = 0; i < k; i++) {
    24             if(--in[G[x][i].first] == 0) q.push(G[x][i].first);
    25             dp[G[x][i].first] = max(dp[G[x][i].first], dp[x] + G[x][i].second);
    26         }
    27     }
    28 }
    29 
    30 int main() {
    31     scanf("%d", &t);
    32     while(t--) {
    33         scanf("%d%d", &n ,&m);
    34         memset(in, 0, sizeof(in));
    35         memset(dp, 0, sizeof(dp));
    36         for(int i = 0; i <= n; i++) {
    37             G[i].clear();
    38         }
    39         for(int i = 0; i < m; i++) {
    40             scanf("%d%d%d", &u, &v, &w);
    41             G[u].push_back(make_pair(v, w));
    42             in[v]++;
    43         }
    44         topsort();
    45         int mx = -1;
    46         for(int i = 1; i <= n; i++) {
    47             mx = max(mx, dp[i]);
    48         }
    49         printf("%d
    ", mx);
    50     }
    51     return 0;
    52 }
  • 相关阅读:
    HDU1069:Monkey and Banana(DP+贪心)
    hdu 4497 GCD and LCM(2013 ACM-ICPC吉林通化全国邀请赛——题目重现)
    vb6.0 倒计时
    硬盘分区表
    踽踽独行的岁月,感谢与你的相遇
    每天学点Linux:二
    windows下Qt Creator5.1.0编写程序以及调用OpenCV库
    【每周一译】愚蠢的指标:Java中使用最多的关键字
    centos 7 没有ifconfig 命令
    About stats collected
  • 原文地址:https://www.cnblogs.com/Dillonh/p/9005170.html
Copyright © 2011-2022 走看看