Codevs 1021 (Marika)

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

time limit: 2 s space limit: 128000 KB title Source: Codevs The problem let us ask is the worst case, we can use SPFA to run along the shortest way, and then record the path of the most short circuit and then enumerate the edge, the edge is marked as a traffic jam, and then the shortest path, we can get each group from N to 1 city of the minimum time is how much, When we compare the maximum with ans, we can get the worst-case time after enumerating through the edges of the original shortest path lock. (In the example, we can go from 5 to 2 takes 1 of the time, and then from 2 to 1, 8 of the time, in the case of no traffic jams 9 is the shortest time spent, at this point we will update ans to 9.) Then the first edge of the optimal solution is set to traffic jam, we have to go from 5 to 3, consuming 10, then from 3 to 2 cost 9, then from 2 to 1 cost 8, the total cost is 27, more than 9, so ans updated to 27. In dealing with the second edge of the optimal solution, first from 5 to 2 consumes 1, then from 2 to 4 cost 10, then from 4 to 1 cost 10, total cost is 21, but no greater than the value of ans, so not the worst case, so Output27 ) because the time limit is 2s, the first to think the best way to record the edge is too troublesome, so it is intended to each side is set as a traffic jam once, violence down this problem, but the number of sides in the title according to the formula is about 1e7, if so, 2s is not enough, so we must record the edge of the optimal solution, The record edge is the focus of the AC problem. for this we can open an F-array because of the SPFA, so F[i] represents the starting point of the previous path to the optimal solution to I in the present case F[i]. We can have the if (D[queue[head]]+e[i].aim<d[e[i].lon]) This statement after the establishment, find the new shortest, we can update the f[i] to Queue[head].

For example, in this city, we start from a city to find, a city after the f[2]=1,f[4]=1,f[3]=1. Then the root is queued in order to find the next edge (this assumes the queue is 2,4,3). Then began to search from the city of second, searched for 3 and 5,f[5]=2, because 2+4>2 is not 1th City to 3rd City of the optimal solution, so f[3] value is not updated, went to the city of fourth, the search 1,4,6 the same f[3]=1,f[5]=2,f[6]=4. Next to the team is the third city, will find 2+5<11, explain from City 1th to 3rd to 4th city for 1 to 4 of the optimal solution, so the f[4] updated to 3, leaving the process omitted. So when SPFA is finished, use for (i=n;i! =0;i=f[i]) to search out the path through which the optimal solution passes. The focus is finished, and the following begins to stick the code.

Note that the array must be open enough that the array on the Codevs does not open enough to display the RE, and the tle is displayed.

1#include <cstdio>2#include <algorithm>3 using namespacestd;4 #defineMAXN 21474836475 intPoint ;6 structnode{7  intAim,lon,next,ex;8 };9Node e[1000005];Ten inthead[1005],head,tail,f[1005],queue[30005],d[1005],m,n; One BOOLdui[1000005],flag[1000005]; A voidAddintXintYintZintt) - { -Point + +; thee[point].next=Head[x]; -head[x]=Point ; -e[point].lon=y; -e[point].aim=Z; +e[point].ex=T; - } +  A voidSPFA (inta) at { -   while(head<tail) -  { -    for(inti=head[queue[head]];i!=0; i=e[i].next) -   { -     if(flag[e[i].ex]==true)Continue; in    if(d[queue[head]]+e[i].aim<D[e[i].lon]) -    { to        if(a==0) f[e[i].lon]=Queue[head]; +d[e[i].lon]=d[queue[head]]+E[i].aim; -     if(!dui[e[i].lon]) {queue[tail++]=e[i].lon;dui[e[i].lon]=true;} the    } *   } $dui[queue[head]]=false;Panax Notoginsenghead++; -  } the } +  A voidPrepare () the { +   for(intI=1; i<=n;i++) -d[i]=MAXN; $Head=1; $Tail=2; -dui[1]=true; -queue[1]=1; thed[1]=0; - }Wuyi intMain () the { -scanf"%d%d",&n,&m); Wu   for(intI=1; i<=m;i++) -   { About   intx, y, z $scanf"%d%d%d",&x,&y,&z); -Add (x,y,z,point+2); - Add (y,x,z,point); -    } A prepare (); +SPFA (0); the   intans=D[n]; - prepare (); $   intt=N; the    for(inti=f[n];i!=0; i=F[i]) the   { the     intq,p; the    for(intj=head[i];j!=0; j=e[j].next) -   if(e[j].lon==t) { inq=J; theflag[e[j].ex]=true; the     if(j%2) {flag[e[j+1].ex]=true;p =j+1;} About     Else{flag[e[j-1].ex]=true; thep=j-1;}  the      Break; the   } +SPFA (1); -   if(D[n]>ans) ans=D[n]; theflag[e[p].ex]=flag[e[q].ex]=false;Bayi prepare (); thet=i; the  } -printf"%d", ans); -  return 0; the}
View Code

Codevs 1021 (Marika)

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.