Codevs1021---spfa+ path record

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.

outputs 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 to sample

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 outputs

27

This problem at first glance is a single source shortest, only 1000 of the data is almost to do.

But a closer look at the output description, only to find the situation to deal with traffic jams , think out of the solution is violent, first SPFA and record the path, and then delete each side of the path in turn SPFA, take the maximum value.

So how do you record the path?

My approach is to use a pre array to record the most advantageous/edge of the current node.

1,pre[i] Represents the previous node on the shortest path from 1 to I,

Pre[i] Indicates the number of the last point of the shortest 1 points I, just keep x=pre[x] and then find the corresponding side delete.

2,pre[i] Represents the number of an edge on the shortest path from 1 to I,

Pre[i] Indicates the number of the first edge of the shortest path to 1 point I, int edge=pre[x]; recycle edge=pre[e[edge].u]; Simply delete the edges that are numbered edge.

The subject is a non-directional edge, you need to delete the other direction of the edge, but in the adjacent table storage edge when a non-directed edge is split into two has a forward edge, and numbering adjacent, so find an edge and then delete the edge of +1 or-1.

In the process of SPFA, you can record the next path whenever you update the pre array while relaxing one edge.

The code below uses the pre array to record the number of edges.

1#include <cstdio>2#include <cstring>3#include <algorithm>4 using namespacestd;5 #defineINF 1000000006 intd[1001],q[100001],head[1001],pre[1001],n,m,cnt,h,t,ans;7 BOOLvis[10001];8 structEdge9 {Ten     intU,v,val,next; One     BOOLno;//whether traffic jam A}e[5000050]; - voidSPFA (BOOLre)//re true to record the path - { theh=t=1; -q[t++]=1; -      while(h<t) -     { +          for(inti=head[q[h]];i!=0; i=e[i].next) -         { +             if(e[i].no)Continue; A             if(d[q[h]]+e[i].val<D[E[I].V]) at             { -d[e[i].v]=d[q[h]]+E[i].val; -                 if(RE) pre[e[i].v]=i;//number of record edges I -                 if(!VIS[E[I].V]) -                 { -q[t++]=e[i].v; invis[e[i].v]=true; -                 } to             } +         } -vis[q[h]]=false; the++h; *     } $ }Panax Notoginseng voidAddintUuintvvintww)//A non-cnt,cnt+1 side is split into two forward-side storage with a number of separate lines - { thee[++cnt].next=Head[uu]; +head[uu]=CNT; Ae[cnt].u=UU; thee[cnt].v=VV; +E[cnt].val=ww; -e[++cnt].next=HEAD[VV]; $head[vv]=CNT; $e[cnt].u=VV; -e[cnt].v=UU; -E[cnt].val=ww; the } - intMain ()Wuyi { the     intX,y,w,now; -scanf"%d%d",&n,&m); Wu      for(intI=2; i<=n;i++) -d[i]=inf; About      for(intI=1; i<=m;i++) $     { -scanf"%d%d%d",&x,&y,&W); - Add (x,y,w); -     } ASPFA (true); +ans=D[n]; thenow=Pre[n]; -      while(1) $     { the         if(now+1<=cnt&&e[now+1].u==e[now].v&&e[now+1].v==e[now].u)//Find another side to be deleted the         { thee[now+1].no=e[now].no=true; thememset (Vis,false,sizeof(Vis)); -              for(intI=2; i<=n;i++) d[i]=inf; inSPFA (false); thee[now+1].no=e[now].no=false; theans=Max (ans,d[n]); About         } the         Else if(now-1>=1&&e[now-1].u==e[now].v&&e[now-1].v==e[now].u) the         { thee[now-1].no=e[now].no=true; +memset (Vis,false,sizeof(Vis)); -              for(intI=2; i<=n;i++) d[i]=inf; theSPFA (false);Bayie[now-1].no=e[now].no=false; theans=Max (ans,d[n]); the         } -         if(e[now].u==1) Break;//If the starting point of the edge is 1, exit -now=pre[e[now].u];//Next Edge the     } theprintf"%d", ans); the     return 0; the}

Codevs1021---spfa+ path record

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.