Problem B:Roadblocks(POJ-3255)

Problem 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

————————————————————————————————————————————————————

题意:给出 r 条路,n 个路口,路是双向的,求从第 1 号路到第 n 号路的路口的次短路

思路:求次短路模版题,具体思路:点击这里

Source Program

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<string>
#include<cstdlib>
#include<queue>
#include<set>
#include<map>
#include<stack>
#include<vector>
#define INF 0x3f3f3f3f
#define PI acos(-1.0)
#define N 10001
#define MOD 123
#define E 1e-6
using namespace std;
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
using namespace std;
int n,r;
int cnt=0;
int d1[N],d2[N],head[N],vis[N],q[N];
struct Edge
{
	int from;
	int next;
	int to;
	int w;
}edge[N*20];

void addedge(int x,int y,int w)
{
	edge[cnt].from=x;
	edge[cnt].to=y;
	edge[cnt].w=w;
	edge[cnt].next=head[x];
	head[x]=cnt++;

	edge[cnt].from=y;
	edge[cnt].to=x;
	edge[cnt].w=w;
	edge[cnt].next=head[y];
	head[y]=cnt++;
}

void spfa(int s,int *d)
{
	memset(vis,0,sizeof(vis));
	memset(q,0,sizeof(q));
	for(int i=1;i<=n;i++)
       d[i]=INF;

	d[s]=0;
	vis[s]=1;

	int headd=0,tail=0;
	q[tail++]=s;
	while(headd!=tail)
	{
		int x=q[headd++];
		vis[x]=0;

		if(headd>=N)
            headd=0;

		for(int i=head[x];i!=-1;i=edge[i].next)
		{
			int y=edge[i].to;
            int w=edge[i].w;
			if(d[y]>d[x]+w)
			{
				d[y]=d[x]+w;

				if(vis[y])
                    continue;
				vis[y]=1;

				q[tail++]=y;
				if(tail>=N)
                    tail=0;
			}
		}
	}

}

int main()
{
    memset(head,-1,sizeof(head));

	scanf("%d%d",&n,&r);
	while(r--)
    {
        int x,y,w;
        scanf("%d%d%d",&x,&y,&w);
        addedge(x,y,w);
    }
    spfa(1,d1);
    spfa(n,d2);
    
    int ans=d1[n],res=INF;
    for(int i=0;i<cnt;i++)
    {
        int x=edge[i].from;
        int y=edge[i].to;
        int w=edge[i].w;

        if(d1[x]+d2[y]+w>ans)
            res=min(res,d1[x]+d2[y]+w);
    }
    printf("%d\n",res);

	return 0;
}

猜你喜欢

转载自blog.csdn.net/u011815404/article/details/81083391
今日推荐