UVALive 6885 Flowery Trails 最短路
Flowery Trails
题目连接:
Description
In order to attract more visitors, the manager of a national
park had the idea of planting flowers along both
sides of the popular trails, which are the trails used by
common people. Common people only go from the park
entrance to its highest peak, where views are breathtaking,
by a shortest path. So, he wants to know how many
metres of flowers are needed to materialize his idea.
For instance, in the park whose map is depicted in
the figure, common people make only one of the three
following paths (which are the shortest paths from the
entrance to the highest peak).
• At the entrance, some start in the rightmost trail
to reach the point of interest 3 (after 100 metres),
follow directly to point 7 (200 metres) and then pick
the direct trail to the highest peak (620 metres).
• The others go to the left at the entrance and reach
point 1 (after 580 metres). Then, they take one of
the two trails that lead to point 4 (both have 90
metres). At point 4, they follow the direct trail to the highest peak (250 metres).
Notice that popular trails (i.e., the trails followed by common people) are highlighted in yellow. Since
the sum of their lengths is 1930 metres, the extent of flowers needed to cover both sides of the popular
trails is 3860 metres (3860 = 2 × 1930).
Given a description of the park, with its points of interest and (two-way) trails, the goal is to find
out the extent of flowers needed to cover both sides of the popular trails. It is guaranteed that, for the
given inputs, there is some path from the park entrance to the highest peak.
Input
The input file contains several test cases, each of them as described below.
The first line of the input has two integers: P and T. P is the number of points of interest and T
is the number of trails. Points are identified by integers, ranging from 0 to P − 1. The entrance point
is 0 and the highest peak is point P − 1.
Each of the following T lines characterises a different trail. It contains three integers, p1, p2, and
l, which indicate that the (two-way) trail links directly points p1 and p2 (not necessarily distinct) and
has length l (in metres).
Integers in the same line are separated by a single space.
Constraints:
2 ≤ P ≤ 10 000 Number of points.
1 ≤ T ≤ 250 000 Number of trails.
1 ≤ l ≤ 1 000 Length of a trail
Output
For each test case, the output has a single line with the extent of flowers (in metres) needed to cover
both sides of the popular trails.
Sample Input
10 15
0 1 580
1 4 90
1 4 90
4 9 250
4 2 510
2 7 600
7 3 200
3 3 380
3 0 150
0 3 100
7 8 500
7 9 620
9 6 510
6 5 145
5 9 160
4 7
0 1 1
0 2 2
0 3 10
0 3 3
1 3 2
2 3 1
1 1 1
Sample Output
3860
18
Hint
题意
求在最短路上的边的长度和
题解:
枚举边,如果边起点到一端的距离+终点到一端的距离+这条边的长度,那么这条边就在最短路上。
代码
#include<bits/stdc++.h>
using namespace std;
const int maxn = 10005;
const int Maxn = 250005;
int d1[maxn],d2[maxn];
int n,m;
struct node{
int x,y;
node(int X,int Y):x(X),y(Y){};
};
vector<node> E[maxn];
int a[Maxn],b[Maxn],c[Maxn];
priority_queue<pair<int,int> >Q;
void init(){
while(!Q.empty())Q.pop();
for(int i=0;i<maxn;i++)E[i].clear();
}
int main(){
while(scanf("%d%d",&n,&m)!=EOF){
init();
for(int i=1;i<=m;i++){
scanf("%d%d%d",&a[i],&b[i],&c[i]);
E[a[i]].push_back(node{b[i],c[i]});
E[b[i]].push_back(node(a[i],c[i]));
}
for(int i=0;i<maxn;i++)d1[i]=1000000005;
for(int i=0;i<maxn;i++)d2[i]=1000000005;
Q.push(make_pair(0,0));
d1[0]=0;
while(!Q.empty()){
int now=Q.top().second;
Q.pop();
for(int i=0;i<E[now].size();i++){
int v=E[now][i].x;
int sp=E[now][i].y;
if(d1[v]>d1[now]+sp){
d1[v]=d1[now]+sp;
Q.push(make_pair(-d1[v],v));
}
}
}
Q.push(make_pair(0,n-1));
d2[n-1]=0;
while(!Q.empty()){
int now=Q.top().second;
Q.pop();
for(int i=0;i<E[now].size();i++){
int v=E[now][i].x;
int sp=E[now][i].y;
if(d2[v]>d2[now]+sp){
d2[v]=d2[now]+sp;
Q.push(make_pair(-d2[v],v));
}
}
}
long long ans = 0;
for(int i=1;i<=m;i++){
if(d1[n-1]==d1[a[i]]+d2[b[i]]+c[i])
ans+=2ll*c[i];
else if(d1[n-1]==d2[a[i]]+d1[b[i]]+c[i])
ans+=2ll*c[i];
}
cout<<ans<<endl;
}
}
UVALive 6885 Flowery Trails 最短路的更多相关文章
- UVALive 6885 Flowery Trails 最短路枚举
题目连接: http://acm.hust.edu.cn/vjudge/problem/visitOriginUrl.action?id=129723 题意: 给你一个n点m图的边 1到n有多条最短路 ...
- UVALive 6885 Flowery Trails
两次SPFA #include<cstdio> #include<cstring> #include<cmath> #include<vector> # ...
- HNU 13375 Flowery Trails (spfa最短路)
求最短路径覆盖的全部边权值和. 思路:分别从起点和终点两次求最短路,再比较两个点到起点的距离和他们之间的权值相加和是否等于最短路径. 这题很好 #include <cstring> #in ...
- UVALive 4128 Steam Roller(最短路(拆点,多状态))
题意:模拟了汽车的行驶过程,边上的权值为全速通过所消耗的时间,而起步(从起点出发的边).刹车(到终点结束的边).减速(即将拐弯的边).加速(刚完成拐弯的边)这四种不能达到全速的情况,消耗的时间为权值* ...
- 洛谷P2939 [USACO09FEB]改造路Revamping Trails(最短路)
题目描述 Farmer John dutifully checks on the cows every day. He traverses some of the M (1 <= M <= ...
- kuangbin带你飞 最短路 题解
求一个图最短路边的办法.好像下面的那个有问题.单向边和双向边一定是有区别的.这个比较容易.参照该文的最短路网络流题目和连通图题目一题求最短路关节边 另外上述2个题目的代码好像有问题. 在UVALIVE ...
- 【ACM】那些年,我们挖(WA)过的最短路
不定时更新博客,该博客仅仅是一篇关于最短路的题集,题目顺序随机. 算法思想什么的,我就随便说(复)说(制)咯: Dijkstra算法:以起始点为中心向外层层扩展,直到扩展到终点为止.有贪心的意思. 大 ...
- 二分+最短路 uvalive 3270 Simplified GSM Network(推荐)
// 二分+最短路 uvalive 3270 Simplified GSM Network(推荐) // 题意:已知B(1≤B≤50)个信号站和C(1≤C≤50)座城市的坐标,坐标的绝对值不大于100 ...
- BZOJ 1579: [Usaco2009 Feb]Revamping Trails 道路升级( 最短路 )
最短路...多加一维表示更新了多少条路 -------------------------------------------------------------------------------- ...
随机推荐
- C# Java 加密解密
C# AES加密解密 public static string Encrypt(string key, string clearText) { byte[] clearBytes = Encoding ...
- [转载]Windows服务编写原理及探讨(3)
(三)对服务的深入讨论之下 现在我们还剩下一个函数可以在细节上讨论,那就是服务的CtrlHandler函数. 当调用RegisterServiceCtrlHandler函数时,SCM得到并保存这个回调 ...
- Python_oldboy_自动化运维之路_全栈考试(七)
1. 计算100-300之间所有能被3和7整除的所有数之和 # -*- coding: UTF-8 -*- #blog:http://www.cnblogs.com/linux-chenyang/ c ...
- file.getPath() getAbsolutePath() getCanonicalPath()区别
package file; import java.io.File; import java.io.IOException; public class getFilePath { public sta ...
- SQLSERVER 2008 编辑所有或者任意行
选中表 右键选择“编辑前200行”,然后选择左上角的 sql图标,然后在右侧的SQL语句去掉 top 200 然后执行查询 就可以编辑所有的行了,可以选择自己需要写SQL,然后查询编辑. 第二种方法: ...
- Linux学习笔记:pwd与dirs的区别
在Linux中可以使用pwd和dirs进行当前目录查看,分别用于显示当前目录和显示完整目录记录.具体如下: 1.pwd 显示当前目录 2.dirs 显示目录堆叠中的记录 END 2018-08-21 ...
- 【考古向翻译】Pwn2Own 2010 Windows 7 Internet Explorer 8 exploit
正好在Google搜到了这篇文章,就打算自己翻译一下,也不清楚国内是否有人已经翻译过了.作者是Pwn2Own 2010的获奖者来自荷兰的皮特·维莱格登希尔(Peter Vreugdenhil). 20 ...
- 一步一步学习IdentityServer3 (12) 授权模式
Idr3中的授权模式也是基于OAuth2 来看看例子中的说明 // // 摘要: // OpenID Connect flows. public enum Flows { // // 摘要: // a ...
- JavaScript中的数据结构及实战系列
本系列主要是讲解JavaScript中的数据结构及在实际项目中遇到的地方 JavaScript中的数据结构及实战系列(1):队列 JavaScript中的数据结构及实战系列(2):栈
- 【LOJ】 #2305. 「NOI2017」游戏
题解 枚举x所在的地图的颜色,然后2-SAT建边 如果v所在的地图刚好是不能选的,那么u这边只能选另一种颜色 否则就是u的颜色到v的颜色 v的另一种颜色到u的另一种颜色 代码 #include < ...