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 道路升级( 最短路 )
最短路...多加一维表示更新了多少条路 -------------------------------------------------------------------------------- ...
随机推荐
- mipi 调试经验【转】
转自:http://blog.csdn.net/g_salamander/article/details/9163455 版权声明:本文为博主原创文章,未经博主允许不得转载. 以下是最近几个月在调试 ...
- getattr的使用
from requests_html import HTMLSession class UrlGenerator(object): def __init__(self, root_url): self ...
- 解决修改表结构,添加外键时出现“约束冲突”的错误
由于表结构更改,使用新建表,现有部分表需要更改外键,将引用更改到新建表的相应字段.在更改过程中,部分表出现如下错误提示: ALTER TABLE 语句与 COLUMN FOREIGN KEY 约束 ' ...
- C# 应用程序配置文件App.Config和web.config
应用程序配置文件,对于asp.net是 web.config,对于WINFORM程序是 App.Config(ExeName.exe.config). 配置文件,对于程序本身来说,就是基础和依据,其本 ...
- docker安装(2018-03-14版本)
[安装] 说明一: CENTOS或RHEL自带的docker源不一定是最新的,所以必须到docker.com去找到最新的yum源进行安装 说明二: docker的安装方式有两种: 1. 从指定网站获取 ...
- windows 下的一些常用命令提示符
windows下dos命令窗口输入 netstat -ano即可查看端口使用情况, 如果要查看指定端口是否被占用 使用命令netstat -ano|findstr 端口号, 例如要查看8080端口号是 ...
- 洛谷P2279消防局的设立
传送门啦 一个很摸不清头脑的树形dp 状态: $ dp[i][0] $ :选自己 $ dp[i][1] $ :选了至少一个儿子 $ dp[i][2] $ :选了至少一个孙子 ------------- ...
- 查找sqlserver数据库中,查询某值所表名和字段名
有时候我们想通过一个值知道这个值来自数据库的哪个表以及哪个字段,通过一个存储过程实现的.只需要传入一个想要查找的值,即可查询出这个值所在的表和字段名. 前提是要将这个存储过程放在所查询的数据库. CR ...
- hdu 3389 阶梯博弈
题意:1-N带编号的盒子,当编号满足A>B && A非空 && (A + B) % 3 == 0 && (A + B) % 2 == 1则可以从A ...
- CCF CSP 201409-4 最优配餐
CCF计算机职业资格认证考试题解系列文章为meelo原创,请务必以链接形式注明本文地址 CCF CSP 201409-4 最优配餐 问题描述 栋栋最近开了一家餐饮连锁店,提供外卖服务.随着连锁店越来越 ...