(Winter Camp) roadblock (Shortest way)

Source: Internet
Author: User

Roadblock

Time limit: 1 Sec memory limit: up to MB
Submissions: 9 resolution: 5
Submitted State [Discussion Version]

Title DescriptionEvery morning, FJ wakes up and walks across the farm from the He house to the barn. The farm is a collection of n fields (1 <= n <=) connected by M bidirectional pathways (1 <= m <= 25,000), Each with a associated length.
FJ ' s house was in field 1, and the barn are in field N. No pair of fields are joined by multiple redundant pathways, and it's possible to travel between any pair of fields in the  Farm by walking along an appropriate sequence of pathways. When traveling from one field to another, FJ always selects a route consisting of a sequence of pathways have minimum to Tal length.

Farmer John's cows, up to no good as always, has decided to interfere with his morning routine.  They plan to build a pile of hay bales in exactly one of the M pathways on the farm, and doubling its length. The cows wish to select a pathway to block so, they maximize the increase in FJ ' s distance from the house to the B  Arn. Cows determine by how much they can lengthen FJ ' s route.

Input* Line 1:two space-separated integers, N and M.
* Lines 2..1+m:line j+1 describes the jth bidirectional pathway in terms of three space-separated Integers:a_j B_j L _j, where A_j and B_j are indices in the range 1..N indicating the fields joined by the pathway, and L_j is the length O f the pathway (in the range 1...1,000,000). Output* Line 1:the maximum possible increase in the total length of FJ ' s shortest route made possible by doubling the lengt H of a single pathway.Sample input
5 72 1 51 3 13 2 83 5 73 4 32 4 74 5 2
Sample output
2
Tips

There is 5 fields and 7 pathways. Currently, the shortest path from the House (field 1) to the Barn (field 5) are 1-3-4-5 of total length 1+3+2=6.if the cows Double the length of the pathway from Field 3 to field 4 (increasing it length from 3 to 6), then FJ's shortest route is Now 1-3-5, of total length 1+7=8, larger by and than the previous shortest route length.

"Analysis" the farmer had to go from 1 to N, and he only took the shortest way. Now his cows want to be bad, to set obstacles on some roads, to make the length of the road twice times, to find the maximum increment of the shortest path (the farmer chooses). First run one side the most part of the road, find a precursor, and then the precursor of each side of the practice of the loop to expand it by twice times, and then only the edge expansion twice times the original image to run the shortest way, find d[n] the maximum value. There may be a lot of short-circuiting, but only with one, because even if there are two non-intersecting shortest, then after running D[n] is the same as the first run, does not affect.

#include <iostream>#include<cstring>#include<cstdio>#include<algorithm>#include<cmath>#include<string>#include<map>#include<stack>#include<queue>#include<vector>#defineINF 0x3f3f3f3f#defineMet (b) memset (a,b,sizeof a)#definePB Push_backtypedefLong Longll;using namespacestd;Const intN =1e3;Const intM =24005;intn,m,k;intEdgg[n][n];intEdg[n][n],vis[n],d[n],pre[n];voidSPFA (intx) {Met (Vis,0);    Met (D,inf); d[1]=0; vis[1]=1; Queue<int>Q; Q.push (1);  while(!Q.empty ()) {        intt=Q.front (); Q.pop (); Vis[t]=0;  for(intI=0; i<=n+1; i++){            if(d[i]>d[t]+Edg[t][i]) {D[i]=d[t]+Edg[t][i]; if(!x) pre[i]=T; if(!vis[i]) Q.push (i), vis[i]=1; }        }    }}intMain () {met (Edg,inf); Met (Pre,-1); intAns1,ans2=0; scanf ("%d%d",&n,&m); intu,v,w;  while(m--) {scanf ("%d%d%d",&u,&v,&W); EDG[U][V]=edg[v][u]=W; } SPFA (0); Ans1=D[n];  for(inti=n;pre[i]!=-1; i--){        intv=Pre[i]; EDG[I][V]*=2; Edg[v][i]*=2; SPFA (1); Ans2=Max (Ans2,d[n]); EDG[I][V]/=2; Edg[v][i]/=2; } printf ("%d\n", ans2-ans1); return 0;}

(Winter Camp) roadblock (Shortest way)

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.