Test instructions: To a number of attractions, each attraction has a number of one-way side to other attractions, require planning for bus routes, so that each attraction has a car to reach, and each attraction can only have 1 cars through 1 times, the bus must go back to the starting point of the ring (starting point 2 times). Ask if there is such a line? If there is the length of the road that all the buses need to walk, the length of the request should be as small as possible.
Analysis: This super hard to find is the network flow to do. To attribute each point to a ring, the points on the ring are only 1 precursors and 1 successors. If the 1 precursors are matched with 1 successors, it is the matching problem. But this kind of match is a bit mixed, so to split the point, the 1 points are split into 2, respectively, in the X and Y sets, and then based on the forward side of the map. has been matched with a weighted dichotomy, with only the least power required.
To build the diagram step, for each of the forward Edge a->b, because b in the Y-set, the number is changed to 2*b+1, and a on the left, change to a*2, the capacity is 1, because only one time, the cost is the length. Of course you need to reverse the side! Then add the meeting point, from the Y set to the sink point has the edge, and then add the source point, the source point to the X-set have edges, they are 1 cost 0.
1#include <bits/stdc++.h>2 #defineLL Long Long3 #definePII pair<int,int>4 #defineINF 0x7f7f7f7f5 using namespacestd;6 Const intn= $+ -;7 8 structnode9 {Ten int from; One intto ; A intVal; - intcap; - intflow; the}edge[n*N]; - intedge_cnt, Ans_cost; - intFlow[n], cost[n], path[n], inq[n]; - +vector<int>Vect[n]; - + voidAdd_node (int from,intTo,intValintCapintflow) A { atEDGE[EDGE_CNT]. from= from; -edge[edge_cnt].to=to ; -Edge[edge_cnt].val=Val; -edge[edge_cnt].cap=cap; -edge[edge_cnt].flow=flow; -vect[ from].push_back (edge_cnt++); in } - to + intSPFA (intSinte) - { thedeque<int> Que (1, s); *inq[s]=1; $flow[s]=INF;Panax Notoginsengcost[s]=0; - while(!que.empty ()) the { + intx=Que.front (); Que.pop_front (); Ainq[x]=0; the for(intI=0; I<vect[x].size (); i++) + { -Node e=Edge[vect[x][i]]; $ if(E.cap>e.flow && cost[e.to]>cost[e. from]+e.val) $ { -Flow[e.to]=min (flow[e. from], e.cap-e.flow); -Cost[e.to]=cost[e. from]+E.val; thepath[e.to]=Vect[x][i]; - if(!inq[e.to])Wuyi { theinq[e.to]=1; - Que.push_back (e.to); Wu } - } About } $ } - returnFlow[e]; - } - A + intCalintSinte) the { - intans_flow=0; $ while(true) the { thememset (Flow,0,sizeof(flow)); thememset (Path,0,sizeof(path)); thememset (Cost,0x7f,sizeof(cost)); -memset (INQ,0,sizeof(INQ)); in the inttmp=SPFA (s,e); the if(!tmp)returnAns_flow; Aboutans_flow+=tmp; theAns_cost+=cost[e];//length the the intEd=e; + while(ed!=s) - { the intt=path[ed];Bayiedge[t].flow+=Flow[e]; theedge[t^1].flow-=Flow[e]; theEd=edge[t]. from; - } - } the } the the the intMain () - { theFreopen ("Input.txt","R", stdin); the intN, b, v; the while(SCANF ("%d",&N), N)94 { theAns_cost=edge_cnt=0; the for(intj=m-1; i>=0; i--) vect[i].clear (); theMemset (Edge,0,sizeof(Edge));98 About for(intI=1; i<=n; i++) - {101 while(SCANF ("%d",&b), b)102 {103scanf"%d",&v);104Add_node (i*2, b*2+1V1,0);//numbered from 2~n*2+1. theAdd_node (b*2+1, i*2,-V,0,0 );106 }107 }108 //Add meeting point n*2+2, source point 0109 for(intI=1; i<=n; i++) the {111Add_node (0, i*2,0,1,0); theAdd_node (i*2,0,0,0,0);113 } the the for(intI=1; i<=n; i++) the {117Add_node (i*2+1N2+2,0,1,0);118Add_node (n2+2, i*2+1,0,0,0);119 } - 121 if(Cal (0N2+2)!=n) puts ("N");122 Elseprintf"%d\n", ans_cost);123 124 } the return 0;126}
AC Code
UVA 1349 Optimal bus route Design Optimal bus route (minimum cost flow)