HDU 1853 Cyclic Tour minimum cost maximum flow

Source: Internet
Author: User

Title Link: http://acm.hdu.edu.cn/showproblem.php?pid=1853

There is N cities in our country, and M one-way roads connecting them. Now Little Tom wants to make several cyclic tours, which satisfy so, each cycle contain at least both cities, and each CI Ty belongs to one cycle exactly. Tom wants the total length of the "all" tours minimum, but he's too lazy to calculate. Can you help him?Test Instructions Description: N cities have m one-way path, each path has a weight, each city belongs to and belong to only one Ring, Tom plans to travel around the N cities, and each city can only pass once. Ask the last round of N cities after the smallest weights and how much. algorithm Analysis: The common model of cost flow, for the beginner cost of the flow of my, is quite fresh, but also refer to other Acmer blog. first, the model is extracted: given a forward graph, it is required to cover the entire graph with several rings, and the weights and minimums of these rings are covered. Modeling: Source point from and sink to, split i is I and i+n. From->i (W is 1,cost 0) (W is 1 because each city can travel only once), I+n->to (W is 1,cost 0), and for U->v, the connection u->v+n (W is 1,cost is the weight on the path).
1#include <iostream>2#include <cstdio>3#include <cstring>4#include <cstdlib>5#include <cmath>6#include <algorithm>7#include <queue>8 #defineINF 0x7fffffff9 using namespacestd;Ten Const intmaxn= $+Ten; One Const intM =40000+ -; A  - intNm from, to; - structnode the { -     intV,flow,cost; -     intNext; -}edge[m*3]; + intHead[maxn],edgenum; - intDIS[MAXN],PRE[MAXN],PID[MAXN],VIS[MAXN]; + intMaxflow; A  at voidAddintUintVintFlowintCost ) - { -Edge[edgenum].v=v; edge[edgenum].flow=flow; -Edge[edgenum].cost=cost; edge[edgenum].next=Head[u]; -head[u]=edgenum++; -  inEdge[edgenum].v=u; edge[edgenum].flow=0; -Edge[edgenum].cost=-cost; edge[edgenum].next=Head[v]; tohead[v]=edgenum++; + } -  the intSPFA () * { $      for(intI=1; i<=to; i++) dis[i]=inf;Panax Notoginsengmemset (Vis,0,sizeof(Vis)); -queue<int>Q; theQ.push ( from); +dis[ from]=0; Avis[ from]=1; the      while(!q.empty ()) +     { -         intu=Q.front (); Q.pop (); $vis[u]=0; $          for(intI=head[u]; i!=-1; i=edge[i].next) -         { -             intv=edge[i].v; the             if(edge[i].flow>0&& dis[v]>dis[u]+edge[i].cost) -             {Wuyidis[v]=dis[u]+Edge[i].cost; thepre[v]=u; -pid[v]=i; Wu                 if(!Vis[v]) -                 { Aboutvis[v]=1; $ Q.push (v); -                 } -             } -         } A     } +     returnDis[to]; the } -  $ intmincost () the { the     intans=0, maxflow=0; the     intaug=0; the      while(1) -     { inaug=inf; the         inttmp=SPFA (); the         if(Tmp==inf) Break; About          for(intI=to; i!= from; i=Pre[i]) the         { the             if(Edge[pid[i]].flow<( ) theaug=Edge[pid[i]].flow; +         } -          for(intI=to; i!= from; i=Pre[i]) the         {BayiEdge[pid[i]].flow-=; theedge[pid[i]^1].flow + =; the         } -Maxflow + =; -Ans + = tmp*; the     } themaxflow=Maxflow; the     returnans; the } -  the intMain () the { the      while(SCANF ("%d%d", &n,&m)! =EOF)94     { thememset (head,-1,sizeof(head)); theedgenum=0; the         inta,b,c;98          from=2*n+1; Aboutto= from+1; -maxflow=0;101          for(intI=0; i<m; i++.)102         {103scanf"%d%d%d",&a,&b,&c);104Add (A,b+n,1, c); the         }106          for(intI=1; i<=n; i++.)107         {108Add fromI1,0);109Add (I+n,to,1,0); the         }111         intans=mincost (); theprintf"%d\n", Maxflow==n? Ans:-1);113     } the     return 0; the}

HDU 1853 Cyclic Tour minimum cost maximum flow

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.