Flowery Trails (SPFA)

In order to attract more visitors, the manager of a na-

tional park had the idea of planting 
owers 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 breathtak-
ing, by a shortest path. So, he wants to know how many
metres of 
owers are needed to materialize his idea.
For instance, in the park whose map is depicted in
the gure, 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 
owers 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 nd
out the extent of 
owers 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 le contains several test cases, each of them as described below.
The rst 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 identied 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 
owers (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

Time limit3000 ms

题意:有n个点,m条路,求0和n-1两点之间,所有在最短路路径上的边权和的两倍(每条在最短路上的边计算只一次)


思路:两次SPFA,第一次找到最短路标记,第二次枚举判断每条边是否在最短路路径上。

#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<iostream>
#include<map>
#include<queue>
#include<vector>
using namespace std;

typedef long long LL;
typedef unsigned long long ULL;
const int MaxN = 3e5;
int dis[2*MaxN + 5],pre[2*MaxN + 5],last[2*MaxN + 5],other[2*MaxN + 5],val[2*MaxN + 5];
bool flag[MaxN + 5];
int P,T,ans,all;
queue<int>q;

void Build(int u,int v,int w){
	pre[++all] = last[u];
	last[u] = all;
	other[all] = v;
	val[all] = w;
}

void BFS1(int now){                        //SPFA找到每一条最短路并标记
	int dr,dt;
	for(int i = 0;i <= P;i++)dis[i] = 1<< 30;
	for(int i = 0;i <= P;i++)flag[i] = false;
	dis[now] = 0;
	q.push(now);
	flag[now] = true;
	while(!q.empty()){
		now = q.front();
		q.pop();
		dt = last[now];
		while(dt != -1){
			dr = other[dt];
			if(dis[dr] > dis[now] + val[dt]){
				dis[dr] = dis[now] + val[dt];
				if(!flag[dr]){
					flag[dr] = true;
					q.push(dr);
				}
			}
			dt = pre[dt];
		}
		flag[now] = false;
	}
}

void BFS2(int now){                          //枚举每一条边判断是否在最短路路径上
	int dr,dt;
	q.push(now);
	ans = 0;
	while(!q.empty()){	
    	now = q.front();
    	q.pop();
    	dt = last[now];
    	while(dt != -1){
    		dr = other[dt];
    		if(dis[dr] == dis[now] - val[dt]){
    			ans += val[dt];
    			if(!flag[dr]){
    				flag[dr] = true;
    				q.push(dr);
				}
			}
			dt = pre[dt];
		}
	}	
}


int main(){
	while(~scanf("%d%d",&P,&T)){
		all = -1;
		memset(last,-1,sizeof(last));
		memset(flag,false,sizeof(flag));
		for(int i = 0;i < T;i++){
			int u,v,w;
			scanf("%d%d%d",&u,&v,&w);
			Build(u,v,w);Build(v,u,w);    //建双向边
		}
		BFS1(0);
		BFS2(P - 1);
		if(dis[P - 1] == 1 << 30)ans = 0;
     	printf("%d\n",2*ans);
	}
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值