POJ 2135 Farm Tour "minimum cost maximum Flow"

Source: Internet
Author: User

The topic of the first fee flow---

In fact---still do not understand, only know along the shortest path to find the augmented road

Building Map

Source point to 1 with a capacity of 2 (because to go back and forth), the cost is 0 of the edge

N to a meeting point with a capacity of 2 and a cost of 0 sides

The other one is the one that was typed in the title.

In addition, the problem is the non-direction side-----

MAXN to 1000, re----

Save a template First------------------------------------------

1#include <cstdio>2#include <cstring>3#include <cmath>4#include <iostream>5#include <algorithm>6#include <Set>7#include <map>8#include <stack>9#include <vector>Ten#include <queue> One#include <string> A using namespacestd; -  -typedefLong LongLL; the Const intMAXN = the; - Const intINF = (1<< -) -1; -   - intfirst[maxn],vis[maxn],dis[maxn],pos[maxn],ecnt,size; +  - structedge{ +     intV,next,cap,cost; A} e[Ten*MAXN]; at  - voidinit () { -ECNT =0; -memset (first,-1,sizeof(first)); - } -    in voidAdd_edge (intUintVintCapintCost ) { -E[ECNT].V =v; toE[ecnt].cap =cap; +E[ecnt].cost =Cost ; -E[ecnt].next =First[u]; theFirst[u] = ecnt++; *      $E[ECNT].V =u;Panax NotoginsengE[ecnt].cap =0; -E[ecnt].cost =-Cost ; theE[ecnt].next =First[v]; +FIRST[V] = ecnt++; A } the    + BOOLSPFA (intSintt) - { $     intu,v,i; $Queue <int>Q; -memset (Vis,0,sizeof(Vis)); -      for(i=0; I <= size;i++) dis[i]=INF; the      -dis[s]=0;Wuyivis[s]=1; the Q.push (s); -      Wu      while(!Q.empty ()) { -U=q.front (); Q.pop (); vis[u]=0; About          for(i = first[u]; ~i;i =E[i].next) { $v=e[i].v; -                if(E[i].cap >0&& Dis[u]+e[i].cost <Dis[v]) { -dis[v]=dis[u]+E[i].cost; -pos[v]=i; A                   if(!Vis[v]) { +vis[v]=1; the Q.push (v); -                   } $                } the           } the     } the     returnDIS[T]! =INF; the } -  inLL MCMF (intSintt) the { the     inti; AboutLL cost=0, flow=0; the      while(SPFA (s,t)) { the         intD=INF; the          for(i = t;i! = S;i = e[pos[i]^1].v) { +D =min (d,e[pos[i]].cap); -         } the          for(i = t;i! = S;i = e[pos[i]^1].v) {BayiE[pos[i]].cap-=D; thee[pos[i]^1].cap + =D; the         } -Flow + =D; -Cost + = dis[t]*D; the     } the     returnCost ; the } the  -  the intMain () { the     intn,m; the      while(SCANF ("%d%d", &n,&m)! =EOF) {94 init (); theSize = n+1; theAdd_edge (0,1,2,0); Add_edge (1,0,2,0); theAdd_edge (n+1N2,0); Add_edge (n,n+1,2,0);98          About          for(inti =0; I < m;i++){ -             intu,v,w;101scanf" %d%d%d",&u,&v,&W);102Add_edge (U,v,1, W);103Add_edge (V,u,1, W);104         } theprintf"%i64d\n", MCMF (0, n+1));106     }107     return 0;108}
View Code

POJ 2135 Farm 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.