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 道路升级( 最短路 )
最短路...多加一维表示更新了多少条路 -------------------------------------------------------------------------------- ...
随机推荐
- Linux移植随笔:对tslib库的ts_test测试程序代码的一点分析【转】
转自:http://www.latelee.org/embedded-linux/porting-linux-tstest-code.html 本文是作者对tslib库的ts_test.c文件进行分析 ...
- Linux下MySQL/MariaDB Galera集群搭建过程【转】
MariaDB介绍 MariaDB是开源社区维护的一个MySQL分支,由MySQL的创始人Michael Widenius主导开发,采用GPL授权许可证. MariaDB的目的是完全兼容MySQL,包 ...
- poj1102
模拟 #include <iostream> #include <string> using namespace std; ][][] = { { ' ', '-', ' ', ...
- 关于 JVM 内存的 N 个问题(转)
JVM的内存区域是怎么划分的? JVM的内存划分中,有部分区域是线程私有的,有部分是属于整个JVM进程:有些区域会抛出OOM异常,有些则不会,了解JVM的内存区域划分以及特征,是定位线上内存问题的基础 ...
- python面向对象(四)之抽象类与接口
学过java的应该知道java有抽象类和接口的那么python呢?(以前写的关于java抽象类的笔记java抽象类与接口) python作为一个动态语言,没有强类型的检查,而是以鸭子类型的方式提现 ...
- Oracle 函数 “判断数据表中不存在的数据,才允许通过”
create or replace function mca_detail_material_val(p_material_code VARCHAR2, --实参 p_material_name VA ...
- SQLServer判断指定列的默认值是否存在,并修改默认值
SQLServer判断指定列的默认值是否存在,并修改默认值 2008年10月21日 星期二 下午 12:08 if exists(select A.name as DefaultName,B.name ...
- SQL中rownum和order by的执行顺序的问题
在一个SQL中,如果同时使用rownum和order by,会有一个先后顺序的问题. 比如select id1,id2 from t_tablename where rownum<3 order ...
- 20165203&20165206结对创意感想
一.结对学习过程 我和我的搭档性格志趣相投,而且各有所长,我们两个均属于一丝不苟的人,做一件事就要把它做好.因此,我们学习理念相同,志趣相投,这可能会占很大的优势.首先,我们会利用一周的前几天看课本, ...
- 参数化SQL语句
避免SQL注入的方法有两种:一是所有的SQL语句都存放在存储过程中,这样不但可以避免SQL注入,还能提高一些性能,并且存储过程可以由专门的数据库管理员(DBA)编写和集中管理,不过这种做法有时候针对相 ...