poj 3255(次短路径)

Roadblocks
Time Limit: 2000MS Memory Limit: 65536K
Total Submissions: 10640 Accepted: 3775

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:  AB, 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)


次短路肯定是最短路中某一条边不走,而走了其他边再回到最短路上,而且不可能绕两个地方,只可能绕一个地方,因为明显绕两个地方比绕一个地方的路径长,明显不是次短路了
所以我们枚举每条边<s,t>
有d[s]---源点到s的最短距离
有dr[t]----t到汇点的最短距离,这样就需要从t到s求一次最短路得到了
len<s,t>表示<s,t>这条边的长度
然后我们枚举每一条边有:tmp=d[s]+dr[t]+len<s,t>
找出其中比最短路小但是比其他路长的一个值就是次短路径了
remark:这样算出的次短路径可能有的边会重复走,来保证是次短路。

AC:
#include<iostream>
#include<cstdio>
#include<cstring>
#include<queue>
using namespace std;

const int maxn = 100000;
const int inf = 0x3f3f3f3f;
struct Edge
{
	int key;
	int next;
	int c;
}edge[maxn<<1];
int pre[5010],df[5010],ds[5010];
int n,r,cnt;
bool vis[5010];

void addedge(int x,int y,int w)
{
	edge[cnt].key = y;
	edge[cnt].c = w;
	edge[cnt].next = pre[x];
	pre[x] = cnt++;
}

void SPFA(int src,int *d)
{
	queue<int> que;
	memset(vis,false,sizeof(vis));
	vis[src] = true; d[src] = 0;
	que.push(src);
	while(!que.empty())
	{
		int v = que.front();
		que.pop();
		vis[v] = false;
		for(int i = pre[v]; i != -1; i = edge[i].next)
		{
			int k = edge[i].key;
			if(d[k] > d[v] + edge[i].c)
			{
				d[k] = d[v] + edge[i].c;
				if(vis[k] == false)
				{
					vis[k] = true;
					que.push(k);
				}
			}
		}
	}
}

void init()
{
	memset(pre,-1,sizeof(pre));
	memset(df,inf,sizeof(df));
	memset(ds,inf,sizeof(ds));
	cnt = 0;
}

int main()
{	
	while(scanf("%d%d",&n,&r)!=EOF)
	{
		init();
		for(int i = 1; i <= r; i++)
		{
			int a,b,d;
			scanf("%d%d%d",&a,&b,&d);
			addedge(a,b,d);
			addedge(b,a,d);
		}
		SPFA(1,df);
		SPFA(n,ds);
		int ans = inf;
		for(int i = 1; i <= n; i++)
		{
			for(int j = pre[i]; j != -1; j = edge[j].next)
			{
				int k = edge[j].key;
				if(df[i] + ds[k] + edge[j].c > df[n] && df[i] + ds[k] + edge[j].c < ans)
					ans = df[i] + ds[k] + edge[j].c;
			}
		}
		printf("%d\n",ans);
	}
	return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值