UVA 1658 Admiral "minimum cost maximum Flow"

Source: Internet
Author: User

Split, each point split into I,i '.

Between I and I ' a cost of 0, the capacity of 1 side, you can guarantee that each point only once

Special points, between 1 and N,, between N and 2*n a cost of 0, 2 of the edge, you can find two paths

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 =2*n+1; theAdd_edge (1,1+n,2,0); theAdd_edge (N,2*n,2,0);98          for(inti =2; i < n;i++) Add_edge (I,i+n,1,0); About          -          for(inti =0; I < m;i++){101             intu,v,w;102scanf" %d%d%d",&u,&v,&W);103Add_edge (U+n,v,1, W);104         } theprintf"%lld\n", MCMF (1,2*n));106     }107     return 0;108}
View Code

UVA 1658 Admiral "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.