ACM学习感悟——POF3255 (用SPFA求次短路)

Description

Bessie has moved to a small farm and sometimes enjoys returning to visit one of her best friends. She does not want to get to her old home too quickly, because she likes the scenery along the way. She has decided to take the second-shortest rather than the shortest path. She knows there must be some second-shortest path.

The countryside consists of R (1 ≤ R ≤ 100,000) bidirectional roads, each linking two of the N (1 ≤ N ≤ 5000) intersections, conveniently numbered 1..N. Bessie starts at intersection 1, and her friend (the destination) is at intersection N.

The second-shortest path may share roads with any of the shortest paths, and it may backtrack i.e., use the same road or intersection more than once. The second-shortest path is the shortest path whose length is longer than the shortest path(s) (i.e., if two or more shortest paths exist, the second-shortest path is the one whose length is longer than those but no longer than any other path).

Input

Line 1: Two space-separated integers: N and R
Lines 2.. R+1: Each line contains three space-separated integers: A, B, and D that describe a road that connects intersections A and B and has length D (1 ≤ D ≤ 5000)

Output

Line 1: The length of the second shortest path between node 1 and node N

Sample Input

4 4
1 2 100
2 4 200
2 3 250
3 4 100

Sample Output

450

Hint

Two routes: 1 -> 2 -> 4 (length 100+200=300) and 1 -> 2 -> 3 -> 4 (length 100+250+100=450) 
这道题求得是此短路,思路就是再开一个数组用以来记录次短路,但是发现在spfa的方法下比较难以行得通,于是用了大神的方法。以d[i]表示最短路,cd[i]表示反向最短路。对于最短路,只要其中有一条路没有走最短路的路径,那么便可能是次短路。及
i=1--n    d[i]+d[v]+w(i,v)  不断测试这个值就行,直到 找到最小的就是次短路。 如果用Dijkstra的话,只需要在每一次更新是记录一下就行,回头去试试。用的是链式前向星存图,可能有人看不习惯。
AC代码:
/ 
//                           //                        //
//  Created by  Team 3 			                       //
//  Copyright (c) 2015年 Team 3. All rights reserved.  //
/
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <cstdlib>           
#include <algorithm>
#include <cctype>
#include <stack>
#include <queue>
#include <map>
#include <string>
#include <set>
#include <vector>
#define INF 1<<31
#define cir(i,a,b)  for (int i=a;i<=b;i++)
#define CIR(j,a,b)  for (int j=a;j>=b;j--)
#define CLR(x) memset(x,0,sizeof(x))
typedef long long  ll;
using namespace std;
#define maxn 200010

int pnt[maxn],pre[maxn],head[maxn],w[maxn];
long long cnt;
long long R;
int N;
bool vis[maxn];
long long d[maxn],cd[maxn];

void addedge(int u,int v,int weight)
{
	pnt[cnt]=v;w[cnt]=weight;
	pre[cnt]=head[u];head[u]=cnt++;
}

void spfa(int s,long long *dt)
{ 
	queue<int> q;
	memset(vis,false,sizeof(vis));
//	memset(dt,0x3f,sizeof(dt));
	dt[s]=0;
	vis[s]=true;
	q.push(s);
	while(!q.empty())
	{
		int x=q.front();
		q.pop();
		vis[x]=false;
		for (long long i=head[x];i!=-1;i=pre[i])
		{
			int v=pnt[i];
			if (dt[v]>dt[x]+w[i])
			{
				dt[v]=dt[x]+w[i];
				if (!vis[v])
				{
					vis[v]=true;
					q.push(v);
				}
			}
		}
	}
}
int main()
{
	while (cin >> N >> R)
	{
		memset(d,0x3f,sizeof(d));
		memset(cd,0x3f,sizeof(cd));
		cnt=0;
		memset(head,-1,sizeof(head));
		memset(pre,-1,sizeof(pre));
		for (long long i=1;i<=R;i++)
		{
			int u,v,dis;
			cin >> u >> v >>dis;
			addedge(u,v,dis);
			addedge(v,u,dis);
		} 
		spfa(1,d);
		spfa(N,cd);
//		cout << d[N] << endl;
//		cout << cd[N]<< endl;
		long long ans=0x3f3f,temp;
		for (int i=1;i<=N;i++)
		{
			for (long long j=head[i];j!=-1;j=pre[j])
			{
				int v=pnt[j];
				temp=d[i]+cd[v]+w[j];
				if (temp>d[N] && ans>temp)
					ans=temp;
			}
		}
		cout << ans << endl;
	}
	return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值