Flowery Trails

题目连接:

https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=4897

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 最短路的更多相关文章

  1. UVALive 6885 Flowery Trails 最短路枚举

    题目连接: http://acm.hust.edu.cn/vjudge/problem/visitOriginUrl.action?id=129723 题意: 给你一个n点m图的边 1到n有多条最短路 ...

  2. UVALive 6885 Flowery Trails

    两次SPFA #include<cstdio> #include<cstring> #include<cmath> #include<vector> # ...

  3. HNU 13375 Flowery Trails (spfa最短路)

    求最短路径覆盖的全部边权值和. 思路:分别从起点和终点两次求最短路,再比较两个点到起点的距离和他们之间的权值相加和是否等于最短路径. 这题很好 #include <cstring> #in ...

  4. UVALive 4128 Steam Roller(最短路(拆点,多状态))

    题意:模拟了汽车的行驶过程,边上的权值为全速通过所消耗的时间,而起步(从起点出发的边).刹车(到终点结束的边).减速(即将拐弯的边).加速(刚完成拐弯的边)这四种不能达到全速的情况,消耗的时间为权值* ...

  5. 洛谷P2939 [USACO09FEB]改造路Revamping Trails(最短路)

    题目描述 Farmer John dutifully checks on the cows every day. He traverses some of the M (1 <= M <= ...

  6. kuangbin带你飞 最短路 题解

    求一个图最短路边的办法.好像下面的那个有问题.单向边和双向边一定是有区别的.这个比较容易.参照该文的最短路网络流题目和连通图题目一题求最短路关节边 另外上述2个题目的代码好像有问题. 在UVALIVE ...

  7. 【ACM】那些年,我们挖(WA)过的最短路

    不定时更新博客,该博客仅仅是一篇关于最短路的题集,题目顺序随机. 算法思想什么的,我就随便说(复)说(制)咯: Dijkstra算法:以起始点为中心向外层层扩展,直到扩展到终点为止.有贪心的意思. 大 ...

  8. 二分+最短路 uvalive 3270 Simplified GSM Network(推荐)

    // 二分+最短路 uvalive 3270 Simplified GSM Network(推荐) // 题意:已知B(1≤B≤50)个信号站和C(1≤C≤50)座城市的坐标,坐标的绝对值不大于100 ...

  9. BZOJ 1579: [Usaco2009 Feb]Revamping Trails 道路升级( 最短路 )

    最短路...多加一维表示更新了多少条路 -------------------------------------------------------------------------------- ...

随机推荐

  1. Android Bander设计与实现 - 设计

    Binder Android IPC Linux 内核 驱动 摘要 Binder是Android系统进程间通信(IPC)方式之一.Linux已经拥有管道,system V IPC,socket等IPC ...

  2. 读书笔记 effective c++ Item 28 不要返回指向对象内部数据(internals)的句柄(handles)

    假设你正在操作一个Rectangle类.每个矩形可以通过左上角的点和右下角的点来表示.为了保证一个Rectangle对象尽可能小,你可能决定不把定义矩形范围的点存储在Rectangle类中,而是把它放 ...

  3. 列表选择Spinner

    1.只用XML配置来显示列表 在res\values中添加一个arrays.xml 1 <?xml version="1.0" encoding="utf-8&qu ...

  4. JavaBean的实用工具Lombok(省去get、set等方法)

    转:https://blog.csdn.net/ghsau/article/details/52334762 背景   我们在开发过程中,通常都会定义大量的JavaBean,然后通过IDE去生成其属性 ...

  5. 09 Command Documentation 命令文档

    Command Documentation 命令文档   There is a suite of programs to build and process Go source code. Inste ...

  6. day09作业

    一.填空题 1.方法 2.堆内存 3.构造方法 4.this 5.this 6.static 7.使用类名进行访问 8.package import class 9.关键字 10.lang 二.选择题 ...

  7. Java 中 日期 时间 加减

    DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); //方法1(推荐,功能强大灵活多变) Ca ...

  8. yum安装Mysql-5.6

    MySQL yum库提供了一个简单的和方便的方法来安装和更新MySQL相关的软件包到最新版本. MySQL yum库文档说明:http://dev.mysql.com/doc/mysql-yum-re ...

  9. sqlserver日期推算(年,季度,月,星期推算)

    DECLARE @dt datetime SET @dt=GETDATE() DECLARE @number int SET @number=3 --1.指定日期该年的第一天或最后一天--第一天为1月 ...

  10. Python学习笔记:个税起征点上调至5000,算一算少交多少税?

    一.旧税率表与新税率表比较 以前起征点是3500,2018年10月1日起起征点正式修改为5000,下面我们用Python来分别计算新旧个人所得税分别为多少? 二.旧的个人所得税 import sys ...