Morning Run
Description
Elaxia recently had a crush on karate, and he set up a fitness program for himself, such as push-ups, sit-ups and so on, but so far, he has only survived the morning run. Now give a map near the school, which contains n intersections and M streets, Elaxia can only run from one intersection to another, and the streets intersect only at intersections. Elaxia every day from the dormitory run to school, to ensure that the dormitory number 1, the school number is n. Elaxia's morning running plan is carried out by cycle (including several days), because he does not like to follow the repeated route, so in a cycle, the daily morning running route will not intersect (at the crossroads), bedroom and school is not a crossroads. Elaxia stamina is not very good, he hopes to run in a cycle as short as possible, but also want the training cycle to contain as long as possible. In addition to practicing karate, Elaxia other time spent studying and looking for mm above, all he would like to ask you to help him design a suit to meet his requirements of the morning run plan.
Input
First line: two number n,m. Indicates the number of intersections and streets. The next M-line, 3 numbers per line, indicates that there is a street (unidirectional) with a length of c between junction A and intersection B (a,b,c).
Output
Two number, the first number is the longest period of the number of days, the second number to meet the maximum number of days of the shortest distance length.
Sample Input
7 10
1 2 1
1 3 1
2 4 1
3 4 1
4 5 1
4 6 1
2 5 5
3 6 6
5 7 1
6 7 1
Sample Output
2 11
HINT
For 30% of data, n≤20,m≤120.
For 100% of data, n≤200,m≤20000
Analysis:
Split cost flow, each point is split into two and a side, the capacity of 1 (starting point is the end of M), the cost is 0, the other side capacity of 1, the cost of its length, run the minimum cost maximum flow can.
Code:
Programbzoj1877;Constnn= +;type Point=RecordNext,f,t,cap,v:longint; End;vare:Array[0..400001] ofPoint ; Head,p,d:Array[0..401] ofLongint; Q:Array[0.. nn+1] ofLongint; G:Array[0..401] ofBoolean; N,i,m,x,y,v,tot,s,t:longint;functionmin (x,y:longint): Longint;begin ifX<y ThenMin:=xElsemin:=y;End;procedureAdd (x,y,c,v:longint);beginInc (TOT); E[tot].next:=head[x];head[x]:=tot; E[tot].f:=x;e[tot].t:=y;e[tot].cap:=c; e[tot].v:=v; Inc (TOT); E[tot].next:=head[y];head[y]:=tot; E[tot].f:=y;e[tot].t:=x; e[tot].cap:=0; e[tot].v:=-v;End;functionSPFA (S,t:longint): boolean;varI,x,y,h,tail:longint;begin fori:=1 toN Do beginD[i]:=maxlongintDiv 3; G[i]:=false;End; G[s]:=true; d[s]:=0; h:=0; tail:=1; q[tail]:=s; whileH<>tail Do beginInc (H); x:=q[h];g[x]:=false; I:=HEAD[X];ifH=nn Thenh:=0; whileI<>0 Do beginy:=e[i].t; if(e[i].cap>0) and(D[Y]>D[X]+E[I].V) Then beginD[y]:=D[X]+E[I].V; p[y]:=i; if notG[y] Then beginG[y]:=true; Inc (tail); Q[tail]:=y;ifTail=nn Thentail:=0; End; End; I:=E[i].next; End; End; Exit (D[t]<maxlongintDiv 3);End;procedureMCF (s,t:longint);varAns,f,x,flow:longint;beginans:=0; f:=0; flow:=0; x:=0; whileSPFA (s,t) Do beginF:=maxlongint; x:=T; whileX<>s Do beginF:=min (e[p[x]].cap,f); x:=e[p[x]].f;End; X:=T; whileX<>s Do beginDec (e[p[x]].cap,f); Inc (E[p[x]-ord (P[X)MoD 2=0) +ord (P[x]MoD 2>0)].cap,f); x:=e[p[x]].f;End; Inc (ANS,D[T)*f); Inc (FLOW,F); End; Writeln (Flow,' ', ans);End;beginreadln (n,m); Fillchar (Head,sizeof (head),0); S:=1; T:=n; Add (S,s+n,m,0); Add (T,t+n,m,0); fori:=2 toN-1 DoAdd (I,i+n,1,0); fori:=1 toM Do beginreadln (X,Y,V); Add (x+n,y,1, V); End; N:=n*2; MCF (s,t+nDiv 2);End.
View Code
Bzoj 1877:[sdoi2009] Morning run (minimum cost maximum flow)