code[vs]1021 Marika

Source: Internet
Author: User

code[vs]1021 MarikaSPFA AlgorithmTopic Portal: http://codevs.cn/problem/1021/ 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

Analysis

Solving the shortest route through the traffic Jam Road to reach his city the longest time required, it is not difficult to think, traffic jams must be in the shortest way to the maximum value, otherwise no matter where the road will not affect the original value.

First go through the SPFA, find the shortest path in the original image, and record the paths. Then enumerate all the points on the path, delete this edge, and then SPFA. Find it over and over, and finally find the maximum value.

Comments & Codes

1#include"bits/stdc++.h"2 3 using namespacestd;4 Const intMAXN =210000 ;5 Const intINF =1000000000;6 structEdge7 {8     int     from, to, Next, W;9}e[5000010 ];Ten  One intn,m,cnt,p[maxn],dis[MAXN]; A intin[maxn],pre[MAXN]; - BOOLvisited[MAXN]; -  the voidAdd_edge (Const intXConst intYConst intz) - { -e[++cnt].to =y; -e[cnt].next =p [x]; +e[cnt].W =Z; -e[CNT]. from=x; +p[x] =CNT; A     return ; at } -  - voidSPFA (Const intS) - { -     intI, t, temp; -Queue <int>Q; inMemset (visited,0,sizeof(visited)); -memset (Dis,0x3f,sizeof(Dis)); toMemset (In,0,sizeof(in)); +      - Q.push (S); thevisited[S] =true; *dis[S] =0; $ Panax Notoginseng      while(! Q.empty ())//SPFA Find the shortest way -     { theT=q.front (); Q.pop (); visited[t]=false; +          for(i=p[t];i;i=e[i].next) A         { thetemp=e[i].to; +             if(dis[temp]>dis[t]+E[I].W) -             { $dis[temp]=dis[t]+E[I].W; $Pre [temp] = i;//Record Path -                 if(!Visited[temp]) -                 { the Q.push (temp); -visited[temp]=true;Wuyi                     if(++in[temp]>n)return ; the                 } -                  Wu             } -         } About     } $ } -  - voidWork (intx) - { A     inti,t,temp; +queue<int>Q; theMemset (visited,0,sizeof(visited)); -memset (Dis,0x3f,sizeof(Dis)); $Memset (In,0,sizeof(in)); the     intS =1, haha; the      thehaha = e[x].w, e[x].w = INF;//Delete Edge the Q.push (S); -visited[S] =true; indis[S] =0; the  the      while( ! Q.empty ())//SPFA About     { theT=q.front (); Q.pop (); visited[T] =false; the          for(i=p[t]; i; i =e[i].next) the         { +temp =e[i].to; -             if(dis[temp] > dis[T] +e[i].w) the             {Bayidis[temp] = dis[t]+e[i].w; the                 if( !visited[temp]) the                 { - Q.push (temp); -visited[Temp] =true; the                     if(++in[temp] > N)return ; the                 } the             } the         } -     } thee[x].w = haha;//Recovery the } the 94 intMain () the { the     intx, Y, _; the 98scanf ("%d%d", &n, &m); About      for(intI=1; I<=m; ++i) -     {101scanf ("%d%d%d", &x, &y, &_ ) ;102 Add_edge (x, Y, _);103 Add_edge (y, X, _);104     } the 106SPFA (1 ) ;107     intAns =Dis [n];108     109      for(intI=n; i!=1; I =e[pre[i]]. from ){ the Work (pre[i]);111ans = max (ans, dis[n]);//Comparing maximum values the     }113printf ("%d", ans); the     return 0 ; the}

Finish

code[vs]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.