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<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)的更多相关文章
- Skiing 2017 ACM-ICPC 亚洲区(乌鲁木齐赛区)网络赛H题(拓扑序求有向图最长路)
参考博客(感谢博主):http://blog.csdn.net/yo_bc/article/details/77917288 题意: 给定一个有向无环图,求该图的最长路. 思路: 由于是有向无环图,所 ...
- 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 ...
- HDU 4046 Panda (ACM ICPC 2011北京赛区网络赛)
HDU 4046 Panda (ACM ICPC 2011北京赛区网络赛) Panda Time Limit: 10000/4000 MS (Java/Others) Memory Limit: ...
- 2017 ACM-ICPC 亚洲区(乌鲁木齐赛区)网络赛 H Skiing【拓扑排序,关键路径】
2017 ACM-ICPC 亚洲区(乌鲁木齐赛区)网络赛 H Skiing In this winter holiday, Bob has a plan for skiing at the moun ...
- 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 ...
- 2016 ACM/ICPC亚洲区青岛站现场赛(部分题解)
摘要 本文主要列举并求解了2016 ACM/ICPC亚洲区青岛站现场赛的部分真题,着重介绍了各个题目的解题思路,结合详细的AC代码,意在熟悉青岛赛区的出题策略,以备战2018青岛站现场赛. HDU 5 ...
- ICPC 2018 徐州赛区网络赛
ACM-ICPC 2018 徐州赛区网络赛 去年博客记录过这场比赛经历:该死的水题 一年过去了,不被水题卡了,但难题也没多做几道.水平微微有点长进. D. Easy Math 题意: ...
- 【2017 ACM/ICPC 乌鲁木齐赛区网络赛环境测试赛 E】蒜头君的排序
[链接]h在这里写链接 [题意] 在这里写题意 [题解] 莫队算法+树状数组. 区间增加1或减少1. 对逆序对的影响是固定的. (用冒泡排序变成升序的交换次数,就是逆序对的个数) [错的次数] 0 [ ...
- 2017 ACM-ICPC 亚洲区(乌鲁木齐赛区)网络赛 H. Skiing
题意:在这个寒假中,鲍勃有计划在山区度假胜地滑雪.这个滑雪胜地有M个不同的滑雪道和N个不同的标志位于那些转弯处点.从第S标记到第T标志的第i路径具有长度L.每个路径必须遵循降低高度的原则,起点必须严格 ...
随机推荐
- "亿家App"问卷调查分析结果及心得体会
一.问卷问题设计 调查背景:随着现代社会互联网的发展,基于家庭产生的服务项目也越来越多.为增加家庭之间的交流和互助,增加家庭内部.家庭与家庭之间的沟通互助,并利用互联网便捷交流的优势,使家庭在享受服务 ...
- UVA 167 R-The Sultan's Successors
https://vjudge.net/contest/68264#problem/R The Sultan of Nubia has no children, so she has decided t ...
- 【linux】- nohup 和 &
&的意思是在后台运行, 什么意思呢? 意思是说,当你在执行 ./a.out & 的时候,即使你用ctrl C,那么a.out照样运行(因为对SIGINT信号免疫).但是要注意,如果你直 ...
- veeValidate
网站 http://vee-validate.logaretm.com/index.html#about 自定义为空时候的提示 Field-specific Custom Messages You m ...
- java获取时间整点工具代码
/**获取上n个小时整点小时时间 * @param date * @return */ public static String getLastHourTime(Date date,int n){ C ...
- Java线程常用方法详解
线程的常用方法 1.start() : 线程调用该方法将启动线程,使之从新建状态进入就绪队列排队,一旦轮到它来享用CPU资源时,就可以脱离创建它的线程独立开始自己的生命周期了. 2.run(): Th ...
- C++中Set的使用
/* #include <string> // 使用 string 类时须包含这个文件 #include <iostream> // 这个就加上去吧.c++的输入和输出. us ...
- lalala
<script type="text/javascript"> var a_idx = 0; var b_idx = 0; var a = new Array(&quo ...
- [NOI2009]诗人小G 决策单调性优化DP
第一次写这种二分来优化决策单调性的问题.... 调了好久,,,各种细节问题 显然有DP方程: $f[i]=min(f[j] + qpow(abs(sum[i] - sum[j] - L - 1))); ...
- Android ListView 显示多种数据类型
ListView往往可能会有不同的数据类型,单类型的数据可能运用会比较少些,这也是最近项目中的一个需求{在发送消息的时候,需要选择联系人,而联系人列表由英文字母索引+联系人组成},上一篇文章只是一个基 ...