题目链接: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<N≤10000 and 0<M≤1000000 < M \leq 1000000<M≤100000 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的最大值即可。 代码实现如下:
 #include <queue>
#include <cstdio>
#include <vector>
#include <cstdlib>
#include <cstring>
#include <algorithm>
using namespace std; const int maxn = 1e4 + ;
int t, n, m, u, v, w;
int dp[maxn], in[maxn];
vector<pair<int, int>> G[maxn]; void topsort() {
queue<int> q;
for(int i = ; i <= n; i++) {
if(in[i] == ) q.push(i);
}
int x;
while(!q.empty()) {
x = q.front(), q.pop();
int k = G[x].size();
for(int i = ; i < k; i++) {
if(--in[G[x][i].first] == ) q.push(G[x][i].first);
dp[G[x][i].first] = max(dp[G[x][i].first], dp[x] + G[x][i].second);
}
}
} int main() {
scanf("%d", &t);
while(t--) {
scanf("%d%d", &n ,&m);
memset(in, , sizeof(in));
memset(dp, , sizeof(dp));
for(int i = ; i <= n; i++) {
G[i].clear();
}
for(int i = ; i < m; i++) {
scanf("%d%d%d", &u, &v, &w);
G[u].push_back(make_pair(v, w));
in[v]++;
}
topsort();
int mx = -;
for(int i = ; i <= n; i++) {
mx = max(mx, dp[i]);
}
printf("%d\n", mx);
}
return ;
}

2017 ACM-ICPC 亚洲区(乌鲁木齐赛区)网络赛 H. Skiing (拓扑排序+假dp)的更多相关文章

  1. Skiing 2017 ACM-ICPC 亚洲区(乌鲁木齐赛区)网络赛H题(拓扑序求有向图最长路)

    参考博客(感谢博主):http://blog.csdn.net/yo_bc/article/details/77917288 题意: 给定一个有向无环图,求该图的最长路. 思路: 由于是有向无环图,所 ...

  2. 2017 ACM-ICPC(乌鲁木齐赛区)网络赛 H.Skiing 拓扑排序+最长路

    H.Skiing In this winter holiday, Bob has a plan for skiing at the mountain resort. This ski resort h ...

  3. HDU 4046 Panda (ACM ICPC 2011北京赛区网络赛)

    HDU 4046 Panda (ACM ICPC 2011北京赛区网络赛) Panda Time Limit: 10000/4000 MS (Java/Others)    Memory Limit: ...

  4. 2017 ACM-ICPC 亚洲区(乌鲁木齐赛区)网络赛 H Skiing【拓扑排序,关键路径】

    2017 ACM-ICPC 亚洲区(乌鲁木齐赛区)网络赛  H Skiing In this winter holiday, Bob has a plan for skiing at the moun ...

  5. 2017 ACM-ICPC 亚洲区(南宁赛区)网络赛 M. Frequent Subsets Problem【状态压缩】

    2017 ACM-ICPC 亚洲区(南宁赛区)网络赛  M. Frequent Subsets Problem 题意:给定N和α还有M个U={1,2,3,...N}的子集,求子集X个数,X满足:X是U ...

  6. 2016 ACM/ICPC亚洲区青岛站现场赛(部分题解)

    摘要 本文主要列举并求解了2016 ACM/ICPC亚洲区青岛站现场赛的部分真题,着重介绍了各个题目的解题思路,结合详细的AC代码,意在熟悉青岛赛区的出题策略,以备战2018青岛站现场赛. HDU 5 ...

  7. ICPC 2018 徐州赛区网络赛

    ACM-ICPC 2018 徐州赛区网络赛  去年博客记录过这场比赛经历:该死的水题  一年过去了,不被水题卡了,但难题也没多做几道.水平微微有点长进.     D. Easy Math 题意:   ...

  8. 【2017 ACM/ICPC 乌鲁木齐赛区网络赛环境测试赛 E】蒜头君的排序

    [链接]h在这里写链接 [题意] 在这里写题意 [题解] 莫队算法+树状数组. 区间增加1或减少1. 对逆序对的影响是固定的. (用冒泡排序变成升序的交换次数,就是逆序对的个数) [错的次数] 0 [ ...

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

    题意:在这个寒假中,鲍勃有计划在山区度假胜地滑雪.这个滑雪胜地有M个不同的滑雪道和N个不同的标志位于那些转弯处点.从第S标记到第T标志的第i路径具有长度L.每个路径必须遵循降低高度的原则,起点必须严格 ...

随机推荐

  1. LintCode-73.前序遍历和中序遍历树构造二叉树

    前序遍历和中序遍历树构造二叉树 根据前序遍历和中序遍历树构造二叉树. 注意事项 你可以假设树中不存在相同数值的节点 样例 给出中序遍历:[1,2,3]和前序遍历:[2,1,3]. 返回如下的树:    ...

  2. javaweb引用jar类库的问题

    JAVAWEB中,需要引用的jar类库必须放在/WebContent/WEB-INF/lib文件夹中.不然就会各种报错.

  3. PHPCMSV9 黄页新闻、产品、商机均无法浏览具体信息,显示您没有访问该信息的权限!

    原帖地址:http://bbs.phpcms.cn/forum.php?mod=viewthread&tid=294956&highlight=%C3%BB%D3%D0%B7%C3%C ...

  4. C# 泛型和委托

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...

  5. C#Color颜色表

    Color.AliceBlue 240,248,255 Color.LightSalmon 255,160,122 Color.AntiqueWhite 250,235,215 Color.Light ...

  6. C语言中printf直接打出2进制数是%什么?16进制是什么?

    #include <iostream> #include <stdio.h> #include <stdlib.h> #include <string.h&g ...

  7. <hx>标签 字体自动加粗 自动换行

    <hx>标签 字体自动加粗 自动换行

  8. cogs1667[SGU422]傻叉小明打字

    其实和CF498bName that Tune差不多 题意: 现在需要依次输入n个字符,第i个字符输入的时候有pi的概率输错,不论是第几次输入(0<=pi<=0.5).每输入一个字符的用时 ...

  9. shell脚本学习—条件测试和循环语句

    条件测试 1. 条件测试:test [ 命令test或[可以测试一个条件是否成立,如果测试结果为真,则该命令的Exit Status为0,如果测试结果为假, 则命令的Exit Status为1(注意与 ...

  10. c# 日志记录 行号

    Console.WriteLine(ex.Message); //通过如下代码来记录异常详细的信息 ); Console.WriteLine("文件名:{0},行号:{1},列号:{2}&q ...