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 道路升级( 最短路 )
最短路...多加一维表示更新了多少条路 -------------------------------------------------------------------------------- ...
随机推荐
- Shell脚本中字符串判空:使用-z 字符串长度为0时,为真,-n字符串长度不为0,为真。这两个都不靠谱【转】
最近发现使用 -z 和 -n 来判断字符串判空,或不空时,很不靠谱. 使用下面的方法最可靠: if [ "x${value}" == "x" ] ...
- 双机/RAC/Dataguard的区别【转】
本文转自 双机/RAC/Dataguard的区别-jasoname-ITPUB博客 http://blog.itpub.net/22741583/viewspace-684261/ Data Guar ...
- javascript按照指定格式获取上一个月的日期
//get pre month//get pre month function getPreMonth() { var date=new Date().Format("yyyy-MM-dd& ...
- linux终端操作快捷键
终端操作快捷键: 新建家目录下终端窗口:Ctrl+Alt+t在当期当前路径下新建终端窗口:Ctrl+Shift+n退出终端窗口:Ctrl+Shift+q 多个终端窗口之间相互切换:Tab+Alt 终端 ...
- centos7的防火墙(firewalld)
Centos7中默认将原来的防火墙iptables升级为了firewalld,firewalld跟iptables比起来至少有两大好处: 1.firewalld可以动态修改单条规则,而不需要像ipta ...
- LCT解读(1)
蒟蒻的LCT解读(1) 前段时间本蒟蒻自学了一下LCT,但是网上的很多资料并不很全,而且作为一个数组选手,我看指针代码真的很麻烦,所以就在这里写一篇数组选手能看懂的代码. LCT的初步了解 LCT全称 ...
- 洛谷P2886牛继电器
传送门啦 倍增 $ Floyd $ 注意结构体里二维数组不能开到 $ 2000 $ #include <iostream> #include <cstdio> #include ...
- Ubuntu CEPH快速安装
一.CEPH简介 不管你是想为云平台提供Ceph 对象存储和/或 Ceph 块设备,还是想部署一个 Ceph 文件系统或者把 Ceph 作为他用,所有 Ceph 存储集群的部署都始于部署一个个 Cep ...
- Windbg在应用层调试漏洞时的应用
主要记录一些在应用层调试漏洞的技巧,不会写一些基本的命令,只记录比较有用的平时难以想到的调试方法. 1.!address eax 查看对应内存页的属性,如果poc触发异常之后就可以用这个指令看一下触发 ...
- C语言:打印A-Z字母组合的菱形图案
题目: +++++++++A+++++++++++++++++BCD+++++++++++++++EFGHI+++++++++++++JKLMNOP+++++++++++QRSTUVWXY++++++ ...