Codevs 1021 Marika (SPFA)

Source: Internet
Author: User

Title Description Description

Mike got a new girlfriend and Marika was very angry with him and was looking for revenge.

Because she and they live in the same city, she began to prepare for her long journey.

There is a maximum of one route between every two cities in this country, and we know the time it takes to get from one city to another.

Mike overheard in the car that there was a road being repaired, and there was a traffic jam, but didn't hear exactly which way. No matter which route is being repaired, the city where Mike is located can be reached from the city where Marika is located.

Marika will only pass on the road from traffic jams, and she will be driving on the shortest route. Mike wants to know how long it will take for Marika to reach his city in the worst case, so he can make sure his girlfriend is far enough away from the city.

Write a program to help Mike figure out the maximum time (in minutes) that Marika will need to reach his city by the shortest route through the road without traffic jams.

Enter a description Input Description

The first line has two numbers of N and M separated by spaces, each representing the number of cities and the number of roads between cities. 1≤n≤1000,1≤m≤n* (N-1)/2. The city is labeled with numbers 1 to N, and Mike is in City 1, Marika in city N.

Each row in the next m row contains three numbers, a, B, and V, separated by spaces. Which 1≤a,b≤n,1≤v≤1000. These numbers indicate that there is a two-way road between A and City B and can be passed within V minutes.

Output description Output Description

Output file in the first line of the maximum time in minutes, in this time, no matter which road in the traffic jam, Marika should be able to reach Mike, if less than this time, there must be a road, the road once the traffic jam, Marika will not be able to arrive at Mike.

Sample input Sample Input

5 7

1 2 8

1 4 10

2 3 9

2 4 10

2 5 1

3 4 7

3 5 10

Sample output Sample Output

27

We'll run through the SPFA to find the shortest-circuiting if the problem is solved with SPFA. In this process, we want to record at each point the shortest path is from which point came over (meaning that the minimum distance from a point is not updated, then the point of the most short-circuit source for the current point).

And then we'll start looking back from point N.

 for (i=n;i!=1; i=p[i])//p[i] record is I this point of the shortest path is from which point passed  q[++c]= i;q[++c]=1;

This code records all the points on the shortest path.

We then enumerate, enumerating the shortest paths of every road in a traffic jam 1--n

Then find the longest path in the road (because we are considering the worst of the shortest path)

#include <stdio.h>#include<string.h>#include<algorithm>using namespacestd;structnode{intNext,z,v;} b[1100000];intn,m,l[1010],d[100000],h,t,i,j,e,p[10000],q[1001],cnt,c,x,y,w,hh[ -],u,vv,ans;BOOLdd[100000];voidAddintAaintBbintcc) {b[++cnt].v=BB; B[cnt].next=HH[AA]; b[cnt].z=cc; HH[AA]=CNT; }intMain () {scanf ("%d%d",&n,&m);  for(i=1; i<=m;i++) {scanf (" %d%d%d",&x,&y,&W); Add (x,y,w); add (y,x,w);//adjacency table used to save Edge} memset (L,0x3f,sizeof(l));//The shortest distance of each road is assigned to the maximum value l[1]=0;//Start point is 1, so the shortest distance to point 1 is 0 x=1;  while(1) {  if(h>t) Break; J=Hh[x];  while(1) {e=b[j].v; if(l[x]+b[j].z<L[e]) {L[e]=l[x]+b[j].z; P[e]=x; if(dd[e]==false) dd[e]=true, d[++t]=the E;//DD array records whether the current point is in the queue, and if it is not, enqueue it;if(b[j].next==0) Break; J=B[j].next; } Dd[x]=false; X=d[++h]; }  for(i=n;i!=1; i=P[i]) q[++c]=i; Ans=0; q[++c]=1;
while(1) {memset (L,0x3f,sizeof(l)); l[1]=0; U=Q[i]; VV=q[++I];//U,VV recorded two points of the road to the traffic jam H=0; t=0; X=1; while(1) { if(h>t) Break; J=Hh[x]; while(1) {e=b[j].v; if((u==x&&vv==e) | | (vv==x&&u==e)) {if(b[j].next==0) Break; J=B[j].next; Continue; } if(l[x]+b[j].z<L[e]) {L[e]=l[x]+b[j].z; P[e]=x; if(dd[e]==false) dd[e]=true, d[++t]=e; } if(b[j].next==0) Break; J=B[j].next; } Dd[x]=false; X=d[++h]; } ans=Max (Ans,l[n]); if(I==C) Break; When all the edges on the shortest path are enumerated, break} printf ("%d", ans); return 0;}

Codevs 1021 Marika (SPFA)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.