Test instructions: To a non-map, FJ to start from point 1th to the N point, and then return to point 1th, but once the road has been destroyed (that is, can not go back), the length of each road is different, then complete the journey to go how long road? (Note: There will be a heavy edge, order number unordered, no map!) )
Ideas:
There are heavy edges to be used with the adjacency table. Each of the given sides should be turned into 4 sides! Otherwise, you may not get to the finish line in the beginning. Finally, add a source and sink point, the capacity cap (source point, 1) = 2, the specified can only walk two times, and then specify the other side of the capacity is 1 on the line, when the side was passed, the automatic increase of the flow, will not go.
explanation See the code more clearly.
1 //#pragma COMMENT (linker, "/stack:102400000,102400000")2#include <iostream>3#include <stdio.h>4#include <string.h>5#include <vector>6#include <stack>7#include <algorithm>8#include <map>9 //#include <bits/stdc++.h>Ten #defineLL Long Long One #definePII pair<int,int> A #defineINF 0x7f7f7f7f - using namespacestd; - Const intn=10000+5; thevector<int>Vect[n]; - structnode - { - int from; + intto ; - intCost ; + intcap; A intflow; at}edge[n*4];//the number of sides is the size of the 4*m - - intEdge_num;//Upper limit of Edge - intF[n];//Flow - intPath[n];//Record Path - intD[i];//Record Fees in BOOL inch[N];//whether in the queue - to voidAdd_node (intAintBintCintCaintFL) + { -Edge[edge_num]. from=A; theedge[edge_num].to=b; *edge[edge_num].cost=C; $edge[edge_num].cap=CA;Panax Notoginsengedge[edge_num].flow=FL; -Vect[a].push_back (edge_num++); the } + A theLL SPFA (intSinte) + { -deque<int>que; $ Que.push_back (s); $a[1]=0; -f[s]=INF; - inch[s]=1; the while(!que.empty ()) - {Wuyi intx=Que.front (); the Que.pop_front (); - inch[x]=0; Wu for(intI=0; I<vect[x].size (); i++) - { AboutNode t=Edge[vect[x][i]]; $ if(T.cap>t.flow && c[t.to]>c[t. from]+t.cost)//can flow and be updated with less cost. - { -Path[t.to]=vect[x][i];//easy to update flow by remembering the edge number -C[t.to]=c[t. from]+t.cost;//update cost, equivalent to distance AF[t.to]=min (f[t. from], t.cap-t.flow); + if(!inch[t.to]) the { - Que.push_back (t.to); $ inch[t.to]=1; the } the } the } the } - returnC[e]; in } the the intm; About the intMCMF (intSinte) the { theLL ans=0; + while(1) - { theMemset (F,0,sizeof(f));BayiMemset (c,0x7f,sizeof(c)); theMemsetinch,0,sizeof(inch)); thememset (Path,0,sizeof(path)); - - inttmp=SPFA (S, e); the if(Tmp==inf)returnans; theAns+=tmp*f[e];//This is the minimum cost. Note: Each stream may not be 1. the the intEd=e; - while(ed!=s) the { the inten=path[ed]; theedge[en].flow+=F[e];94edge[en^1].flow-=F[e]; theEd=edge[en]. from; the //cout<<edge[en].from<< "-" <<edge[en].to<<endl; the }98 } About } - 101 102 103 104 the intMain ()106 {107Freopen ("Input.txt","R", stdin);108 intN, a, b, C;109 while(~SCANF ("%d%d", &n, &m)) the {111 for(inti=n*2; i>=0; i--) vect[i].clear (); theedge_num=0;113 for(intI=0; i<m; i++) the { thescanf"%d%d%d",&a,&b,&c); theAdd_node (A,b,c,1,0);//4 have a forward edge. 117Add_node (B,a,-c,0,0);118 119Add_node (B,a,c,1,0); -Add_node (A,b,-c,0,0);121 }122Add_node (0,1,0,2,0);//add more 2 edges123Add_node (1,0,0,0,0);//this is meaningless, but it's more convenient to update flow124Add_node (n,n+1,0,2,0); theAdd_node (n+1N0,0,0);126 127printf"%lld\n", MCMF (0, n+1)); - }129 the 131 return 0; the}AC Code
POJ 2135 Farm Tour (minimum cost maximum flow, deformation)