洛谷 P2865 [USACO06NOV]路障Roadblocks

题目描述

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).
 

输入

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)
 

输出

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

样例输入 

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

样例输出 

450

提示:

来源

USACO 2006 November Gold

题解:我们只要将1号点到各点的距离,n号点到各点的距离求出,然后枚举每条边,判断1号点到左边点的距离+当前边的权值+n号点到右边点的距离>1号点到n号点的距离时,且为最小值,则输出。

#include <cstdio>
#include <cstring>
#define N 200005
#define M 5005
int n,r,x,y,z,first[N],next[N],from[N],v[N],w[N];
int head,tail,cnt,minn=0x3f3f3f3f;
int dis[M],dis1[M],q[50005];
bool vis[M];
using namespace std;
inline int read()    
{    
    int f=1,x=0;    
    char ch=getchar();    
    if (ch=='-')    
    {    
        f=-1;    
        ch=getchar();    
    }    
    while ((ch<'0')||(ch>'9')) ch=getchar();    
    while ((ch>='0')&&(ch<='9'))    
    {    
        x=x*10+ch-48;    
        ch=getchar();    
    }    
    return f*x;    
}
inline void spfa(int x,int *d)
{
	memset(vis,0,sizeof(vis));
	d[x]=0;
	head=0;
	tail=1;
	q[1]=x;
	vis[x]=1;
	while (head<tail)
	{
		head++;
		vis[q[head]]=0;
		for (int i=first[q[head]];i;i=next[i])
		{
			int k=v[i];
			if (d[k]>d[q[head]]+w[i])
			{
				d[k]=d[q[head]]+w[i];
				if (!vis[k])
				{
					vis[k]=1;
					tail++;
					q[tail]=k;
				}
			}
		}
	}
}
int main()
{
	n=read(),r=read();
	for (int i=1;i<=r;i++)
	{
		x=read(),y=read(),z=read();
		next[++cnt]=first[x];
		first[x]=cnt;
		from[cnt]=x;
		v[cnt]=y;
		w[cnt]=z;
		next[++cnt]=first[y];
		first[y]=cnt;
		from[cnt]=y;
		v[cnt]=x;
		w[cnt]=z;
	}
	memset(dis,0x3f3f3f3f,sizeof(dis));
	spfa(1,dis);
	memset(dis1,0x3f3f3f3f,sizeof(dis1));
	spfa(n,dis1);
	for (int i=1;i<=cnt;i++)
	  if (dis[from[i]]+w[i]+dis1[v[i]]>dis[n])
	    if (dis[from[i]]+w[i]+dis1[v[i]]<minn)
	      minn=dis[from[i]]+w[i]+dis1[v[i]];
	printf("%d\n",minn); 
	return 0;
}


猜你喜欢

转载自blog.csdn.net/zhouhongkai06/article/details/80723303