War
Time limit:2000/1000 MS (java/others) Memory limit:65536/32768 K (java/others)
Total submission (s): 1545 Accepted Submission (s): 338
Problem DescriptionIn the war between ANIHC and Bandzi, ANIHC has won Bangzi. At that time, Lvbin, the king of ANIHC, want-to-start from Bangzi to beat Nebir across the channel between them. He let him army get to there as soon as possible, and there located many islands which can is used to has a break. In order to save time, the king forbid the army getting through the same pass for more than one time, but they can reach T He same island for as many as times they want.
Yunfeng, the general of the army, must-many optimal ship routes is there to the king as soon as possible, or he would be killed. Now he asks-your help. You must the help Yunfeng to save his life.
He tells you there is N Island. The islands is numbered from 1 to N (1 was bangzi and N is Nebir, others is many islands). And there is many ways, each is contain the islands number U and V and the length W. Please output your answer.
Inputthe first line with the input file contains a single integer number T means the case number.
Each case contains a number N (n<=1500) means the number of the islands.
And then many lines follow. Each line contains three numbers:u V W (w<10000), means the distance between island U and V are W. The input of ways is terminated by "0 0 0".
Outputprint the number of the optimal ship routes is there after each case in a line.
Sample Input161 2 13 2 13 4 11 3 24 2 24 5 15 6 14 6 20 0 0
Sample Output2
Baidu Online The problem of the solution, as if it is the maximum flow of the method, do not know how efficient, anyway, I think it is very troublesome, I use this algorithm is my own thinking, is always repeated SPFA to seek the minimum flow of the process, when the next minimum flow is found to be larger than the last time, the end of the algorithm, Feel the efficiency of this algorithm is still good, specially posted out to share.
#include <iostream>#include<cstdio>#include<queue>#include<cstring>#include<climits>#defineMAXN 1500*1500using namespacestd;structedge{intS,t,f,c,next;} EDGE[MAXN];inthead[1510];intpre[1510];intdist[1510];BOOLisq[1510];intn,s,t,u,v,w,ent;voidAddintSintTintFintc) {EDGE[ENT].S=S; EDGE[ENT].T=T; EDGE[ENT].F=F; EDGE[ENT].C=C; Edge[ent].next=Head[s]; Head[s]=ent++; Edge[ent].s=T; EDGE[ENT].T=S; EDGE[ENT].F=0; EDGE[ENT].C=-C; Edge[ent].next=Head[t]; Head[t]=ent++;}intMIN (intAintb) { returnA<b?a:b;}voidinit () {ent=0; memset (Head,-1,sizeof(head)); while(SCANF ("%d%d%d", &u,&v,&w), u+v+W) {Add (U,v,1, W); Add (V,u,1, W); }}BOOLSPFA () {memset (PRE,-1,sizeof(pre));//initialization path is-1 for(intk=1; i<=t; i++) {Isq[i]=false;//at the beginning of each point is not in the queueDist[i]=int_max;//the minimum cost to initialize to each point is Int_max} queue<int>Q; Q.push (s); Isq[s]=true;//The source point S has been put into the queue.dist[s]=0;//distance from source point to Source point is 0 while(!q.empty ())//The optimization process ends when the queue is empty, exiting the loop { inttemp1=Q.front (); Q.pop (); ISQ[TEMP1]=false;//the point has exited the queue for(intI=HEAD[TEMP1]; i!=-1; I=edge[i].next)//from this point, find all the edges that start with that point from the adjacency table and find the points that can be optimized. { intTemp2=edge[i].t; if(edge[i].f&&dist[temp2]>dist[temp1]+edge[i].c) {DIST[TEMP2]=dist[temp1]+edge[i].c; PRE[TEMP2]=i; if(!ISQ[TEMP2])//If the point is not in the queue, the point is placed in the queue{Q.push (TEMP2); ISQ[TEMP2]=true; } } } } returnpre[t]!=-1;//If the pre[t]==-1 words indicate that no path from S to T is found, that all paths have been found, end loop}voidMCMF () {inttot=0; intmincost=Int_max; intminn=Int_max; while(SPFA ()) {if(dist[t]<=mincost) {Tot++; Mincost=Dist[t]; } Else Break;//when the newly-calculated minimum cost is greater than the initial one, the end algorithm for(inti=pre[t];i!=-1; i=Pre[i]//minimum cost the process of reducing the flow in the maximum flow {edge[i].f--; Edge[i^1].f++; I=Edge[i].s; }} printf ("%d\n", tot);}intMain () {intCAs; scanf ("%d",&CAs); while(cas--) {scanf ("%d",&N); S=1; t=N; Init (); MCMF (); }}
Hdoj 3599 minimum cost maximum flow