2020 winter vacation [gmoj1922] [rclock road congestion] [spfa shortest route]

Title description

Every morning, John has to walk from his home to the farm. He may have to pass some other places on his way. We abstract these places and roads into a picture. There are N points in this picture, and there are M sides (each side is a two-way side), each side has a length, and John's home is at the first point. , The farm is at the Nth point, and there are no duplicate edges between the two points, and this graph is a connected graph. Every time John will choose the shortest path from his home to the farm.
But John's cows always messed up John. The cows planned to put some haystacks on one of the roads to hinder John's walking. Which way the haystacks were placed, the length of that side was equivalent to double. Now, the cows want to know how to choose a side haystack in order to maximize John's journey from home to farm.

Input

The first line is two positive integers N and M.
In the next M lines, three integers a, b, and c indicate that the distance from point a to point b is c.

Output

Output the distance that the shortest path from home to farm will increase at most.

Sample input

5 7
2 1 5
1 3 1
3 2 8
3 5 7
3 4 3
2 4 7
4 5 2

Sample output

2

Data range limitation

1<=N<=250,1<=M<=25000。

prompt

[Sample description]
When the cows put hay on the side of 3-4, the length of the side of 3-4 is equivalent to change from 3 to 6, and John ’s shortest path becomes 1-3-5. The distance is equal to 1 + 7 = 8, which is 2 more than the original shortest length.

analysis

This question is very violent, because it is " single source shortest path ", so SPFA is used.
First find the shortest path of 1 ~ n, and then enumerate each road as a place to put haystacks. The biggest difference is the answer.

Code on

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
using namespace std;
int n,m,h,t,a[25001],b[25001],c,f[260][260],dis[260],k,ff,mx,v[100010],q[100010];
int spfa()
{
	memset(v,0,sizeof(v));
	memset(q,0,sizeof(q));
	memset(dis,1,sizeof(dis));
	int x;
	dis[1]=0;
	v[1]=1;
	q[1]=1;
	h=0;
	t=1;
	while(h<t)
	{
		h++;
		x=q[h];
		for(int i=1;i<=n;i++)
		{
			if(dis[x]+f[x][i]<dis[i]&&f[x][i]!=0)
			{
				dis[i]=dis[x]+f[x][i];//松弛 
				if(!v[i])
				{
					t++;
					v[i]=1;
					q[t]=i;
				}
			}
		}
		v[x]=0;
	}
	return dis[n];
} 
int main()
{
	freopen("rblock.in","r",stdin);
	freopen("rblock.out","w",stdout);
    cin>>n>>m;
    for(int i=1;i<=m;i++)
    {
    	scanf("%d%d%d",&a[i],&b[i],&c);
    	f[a[i]][b[i]]=c;
    	f[b[i]][a[i]]=c;
	}
	k=spfa();
	for(int i=1;i<=m;i++)
	{
		f[a[i]][b[i]]*=2;
		f[b[i]][a[i]]*=2;
		ff=spfa();
		f[a[i]][b[i]]/=2;
		f[b[i]][a[i]]/=2;
		if(ff-k>mx) mx=ff-k;
	}
	printf("%d",mx);
	fclose(stdin);
	fclose(stdout);
    return 0;
}

Published 110 original articles · won 100 · visited 8021

Guess you like

Origin blog.csdn.net/dglyr/article/details/104931458