BZOJ1266 [AHOI2006] School Route route

Source: Internet
Author: User

Title Link: http://www.lydsy.com/JudgeOnline/problem.php?id=1266

Description cocoa and Kaka lives in the eastern suburbs of Hefei, every day to school they have to change several times to reach the West end of the city school. Until one day the two of them took part in the school's Informatics Olympiad to find that the bus route to school every day is not necessarily optimal. Coco: "It is possible that we wasted a lot of time on our way to school, so let's write a program to calculate the minimum time required for school." "Hefei has a total of n bus stops, so you might want to number them at 1 ... n the natural number and think cocoa and Kaka live near bus station 1th, while their school is at bus N. The city has m direct bus route, the bus to carry out route I routes between the site Pi and Qi, from the starting point to the end of time to take ti. (1<=i<=m, 1<=pi, qi<=n) Two people sitting in front of the computer, according to the above information is quickly programmed to calculate the optimal ride plan. However, cocoa suddenly had a tricks, he wanted to take advantage of Kaka unprepared, in Kaka's input data to delete some routes, so that Kaka's program to obtain the answer is greater than the actual shortest time. And for each route I actually have a cost CI: the easier it is to delete the CI card from the route, the more likely it is to discover this joke, and cocoa wants to know what kind of removal scheme can achieve his purpose and let the bus route be removed to the minimum of CI. [Task] Write a program:? From the input file to read Hefei city bus route information;? Calculate the minimum time it takes for cocoa and Kaka to go to school. To help cocoa design a scheme, delete some of the bus routes in the input information, make the deletion from home to school the minimum time required to become larger, and the deletion of the route of CI and the smallest; output the answer to the output file. The first line in the input file has two positive integers n and m, respectively, indicating the number of Hefei bus and bus routes. The following m lines, each line (line I, total (i+1) line) are described in four positive integers as the route I: Pi, qi, ti, ci; the specific meaning is described above. There are up to two rows of output files. There is only one integer in the first line, which represents the shortest time required from cocoa and Kaka home to school. The second line outputs an integer c, which indicates the sum of CI

Run the shortest way first, and then run the shortest way to get the answer to the smallest cut.

1#include <iostream>2#include <cstdio>3#include <algorithm>4#include <cstring>5#include <queue>6 #defineRep (i,l,r) for (int i=l; i<=r; i++)7 #defineCLR (x, y) memset (x,y,sizeof (×))8 #defineTravel (x) for (Edge *p=last[x]; p; p=p->pre)9typedefLong Longll;Ten using namespacestd; One Const intINF =0x3f3f3f3f; A Const intMAXN =510; - structedge{ -Edge *pre,*rev;intTo,cost; the}edge[250010]; -Edge *last[maxn],*cur[maxn],*pt; - intn,m,ans=0, a[124760],b[124760],c[124760],t[124760],d[2][MAXN]; - BOOLISIN[MAXN]; +Queue <int>Q; -InlineintRead () { +     intAns =0, F =1; A     Charc =GetChar (); at      while(!IsDigit (c)) { -         if(c = ='-') F =-1; -c =GetChar (); -     } -      while(IsDigit (c)) { -Ans = ans *Ten+ C-'0'; inc =GetChar (); -     } to     returnAns *F; + } -InlinevoidAddedge (intXintY,ll z) { thePt->pre = Last[x]; Pt->to = y; Pt->cost = Z; LAST[X] = pt++; * } $InlinevoidAddintXintY,ll z) {Panax NotoginsengAddedge (x, y, z); Addedge (Y,x,0); Last[x]->rev = Last[y]; Last[y]->rev =Last[x]; - } the voidSPFA (intXintt) { +CLR (D[t],inf); D[T][X] =0; ACLR (Isin,0); ISIN[X] =1; Q.push (x); the      while(!Q.empty ()) { +         intnow = Q.front (); Q.pop (); Isin[now] =0; - Travel (now) { $             if(D[t][p->to] > D[t][now] + p->Cost ) { $D[t][p->to] = D[t][now] + p->Cost ; -                 if(!isin[p->to ]) { -Isin[p->to] =1; theQ.push (p->to ); -                 }Wuyi             } the         } -     } Wu } - BOOLBFs () { About      while(!q.empty ()) Q.pop (); $CLR (d[0],-1); d[0][1] =0; Q.push (1); -      while(!Q.empty ()) { -         intnow =Q.front (); Q.pop (); - Travel (now) { A             if(d[0][p->to] = =-1&& p->cost >0){ +d[0][p->to] = d[0][now] +1; theQ.push (p->to ); -                 if(P->to = = N)return 1; $             } the         } the     } the     return 0; the } - intDfsintXintflow) { in     if(x = = N | | (!flow))returnFlowintW =0; the      for(Edge *p=cur[x]; p && W < flow; p=p->pre) { the         if(d[0][p->to] = = d[0][X] +1&& p->cost >0){ About             intDelta = DFS (p->to,min (p->cost,flow-W)); theP->cost-=Delta; theP->rev->cost + =Delta; theW + =Delta; +             if(p->cost) Cur[x] =p; -         } the     }Bayi     if(W < Flow) d[0][X] =-1; the     returnW; the } - intMain () { -n = read (); m = read (); CLR (Last,0); PT =Edge; theRep (I,1, M) { theA[i] = read (); B[i] = read (); T[i] = read (); C[i] =read (); the Addedge (A[i],b[i],t[i]); Addedge (B[i],a[i],t[i]); the     } -SPFA (1,0); SPFA (N,1); theprintf"%d\n"-D:0][n]); theCLR (Last,0); PT =Edge; theRep (I,1, M) {94         if(d[0][a[i]] + t[i] + d[1][b[i]] = = d[0][n]) { the Add (A[i],b[i],c[i]); the         } the         if(d[0][b[i]] + t[i] + d[1][a[i]] = = d[0][n]) {98 Add (B[i],a[i],c[i]); About         } -     }101      while(BFS ()) {102Rep (I,1, n) cur[i] =Last[i];103Ans + = DFS (1, INF);104     } theprintf"%d\n", ans);106     return 0;107}
View Code

BZOJ1266 [AHOI2006] School Route route

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.