从1走到N然后从N走回来的最短路程是多少? 转换为费用流来建模.

   1:  /**
   2:  因为e ==0 所以 pe[v]   pe[v]^1 是两条相对应的边
   3:  E[pe[v]].c -= aug;            E[pe[v]^1].c += aug;
   4:  
   5:  */
   6:  #include <queue>
   7:  #include <iostream>
   8:  #include <string.h>
   9:  #include <stdio.h>
  10:  #include <map>
  11:  using namespace std;
  12:  #define V 30010      // vertex
  13:  #define E 150010      // edge
  14:  #define INF 0x3F3F3F3F
  15:  struct MinCostMaxFlow
  16:  {
  17:      struct Edge
  18:      {
  19:          int v, next, cap, cost;  // cap 为容量, cost为单位流量费用
  20:      } edge[E];
  21:   
  22:      int head[V], pe[V], pv[V];            // 每个节点的第一条edge[idx]的编号.
  23:      int  dis[V];            // the shortest path to the src
  24:      bool vis[V];            // visted
  25:      // pe[v]  存放在增广路上到达v的边(u,v) 在edge[]的位置
  26:      // pv[u]  存放在增广路上从u出发的边(u,v) 在edge[]中的位置
  27:      int e, src, sink;            // the index of the edge
  28:      void addedge(int  u, int v, int cap, int cost)
  29:      {
  30:          edge[e].v = v, edge[e].cap = cap;
  31:          edge[e].cost = cost,edge[e].next = head[u], head[u] = e++;
  32:          edge[e].v = u, edge[e].cap = 0;
  33:          edge[e].cost = -1*cost, edge[e].next = head[v], head[v] = e++;
  34:      }
  35:      // 求最短路,不存在负环的时候,比 DFS快
  36:      int SPFABFS()
  37:      {
  38:          memset(vis, 0 ,sizeof(vis));
  39:          memset(pv, -1, sizeof(pv));
  40:          for(int i=0; i<V; i++) dis[i] = INF;
  41:          queue<int> Q;
  42:          Q.push(src), vis[src] = 1, dis[src] = 0;
  43:          while(!Q.empty())
  44:          {
  45:              int u = Q.front();
  46:              Q.pop(), vis[u] = 0;
  47:              for(int i=head[u]; i!=-1; i=edge[i].next)
  48:              {
  49:                  int v = edge[i].v;
  50:                  if(edge[i].cap > 0 &&  dis[v] > dis[u] + edge[i].cost  )
  51:                  {
  52:                      dis[v] = dis[u] + edge[i].cost;
  53:                      if(!vis[v]) Q.push(v),vis[v] = 1;
  54:                      pv[v] = u, pe[v] = i;
  55:                  }
  56:              }
  57:          }
  58:          if(dis[sink] == INF) return -2;          // can't from src to sink.
  59:          return dis[sink];
  60:      }
  61:   
  62:      pair<int,int> MCMF()
  63:      {
  64:          int maxflow = 0, mincost = 0;
  65:          while(SPFABFS())
  66:          {
  67:              if(pv[sink] == -1) break;
  68:              int aug = INF;
  69:              for(int i= sink; i!= src; i = pv[i])
  70:                  aug =min(aug, edge[pe[i]].cap);
  71:              maxflow += aug;
  72:              mincost += aug * dis[sink];
  73:              for(int i = sink; i!= src; i = pv[i])
  74:              {
  75:                  edge[pe[i]].cap -= aug;
  76:                  edge[pe[i]^1].cap += aug;
  77:              }
  78:          }
  79:          return make_pair(maxflow, mincost);
  80:      }
  81:   
  82:      /// 一定需要初始化的是 src, sink
  83:      void solve()
  84:      {
  85:          int N,M;
  86:          while(scanf("%d%d", &N , &M)!= EOF)
  87:          {
  88:              e=0;
  89:              memset(head, -1,sizeof(head));
  90:              for(int i=0; i<M; i++)
  91:              {
  92:                  int a, b, c;
  93:                  scanf("%d%d%d", &a, &b, &c);
  94:                  addedge(a, b,1,c);
  95:                  addedge(b, a,1, c);
  96:              }
  97:              src = 0;
  98:              sink = N+1;
  99:              addedge(src,1, 2, 0);
 100:              addedge(N, sink, 2, 0);
 101:              pair<int,int> ret = MCMF();
 102:              cout<<ret.second<<endl;
 103:          }
 104:      }
 105:  } mcmf;
 106:   
 107:  int main()
 108:  {
 109:      mcmf.solve();
 110:      return 0;
 111:  }

.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, "Courier New", courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }

POJ 2315 最小费用最大流的更多相关文章

  1. poj 3422(最小费用最大流)

    题目链接:http://poj.org/problem?id=3422 思路:求从起点到终点走k次获得的最大值,最小费用最大流的应用:将点权转化为边权,需要拆点,边容量为1,费用为该点的点权,表示该点 ...

  2. POJ 2516 最小费用最大流

    每一种货物都是独立的,分成k次最小费用最大流即可! 1: /** 2: 因为e ==0 所以 pe[v] pe[v]^1 是两条相对应的边 3: E[pe[v]].c -= aug; E[pe[v]^ ...

  3. POJ - 2195 最小费用最大流

    题意:每个人到每个房子一一对应,费用为曼哈顿距离,求最小的费用 题解:单源点汇点最小费用最大流,每个人和房子对于建边 #include<map> #include<set> # ...

  4. poj 2195 最小费用最大流模板

    /*Source Code Problem: 2195 User: HEU_daoguang Memory: 1172K Time: 94MS Language: G++ Result: Accept ...

  5. POJ 2135 最小费用最大流 入门题

    Farm Tour Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 19207   Accepted: 7441 Descri ...

  6. poj 2135最小费用最大流

    最小费用最大流问题是经济学和管理学中的一类典型问题.在一个网络中每段路径都有"容量"和"费用"两个限制的条件下,此类问题的研究试图寻找出:流量从A到B,如何选择 ...

  7. poj 3680(最小费用最大流)

    题目链接:http://poj.org/problem?id=3680 思路:因为N<=200,而区间范围为[1,100000],因此需要离散化,去重,然后就是建图了相连两点连边,容量为k,费用 ...

  8. POJ 2135 最小费用最大流

    题目链接 Farm Tour Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 18961   Accepted: 7326 D ...

  9. POJ 2195:Going Home(最小费用最大流)

    http://poj.org/problem?id=2195 题意:有一个地图里面有N个人和N个家,每走一格的花费是1,问让这N个人分别到这N个家的最小花费是多少. 思路:通过这个题目学了最小费用最大 ...

随机推荐

  1. 剑指Offer16 判断子树

    /************************************************************************* > File Name: 17_Mirror ...

  2. 写jQuery插件

    如今做web开发,jquery 几乎是必不可少的,就连vs神器在2010版本开始将Jquery 及ui 内置web项目里了.至于使用jquery好处这里就不再赘述了,用过的都知道.今天我们来讨论下jq ...

  3. Part 8 Coalesce function in sql server

  4. 写shell,运行出错:syntax error near unexpected token `$’do\r”

    cygwin下面写shell,运行出错:syntax error near unexpected token `$’do\r” 写shell,运行出错:syntax error near unexpe ...

  5. Cocos2d-JS中的文本菜单

    文本菜单是菜单项只能显示文本,文本菜单类包括了cc.MenuItemLabel.cc.MenuItemFont和cc.MenuItemAtlasFont.cc.MenuItemLabel是个抽象类,具 ...

  6. 读书笔记-UIView与控件

    1.UIView 在Objective-C中,NSObject是所有类的“根”类.同样,在UIKit框架中,也存在一个如此神奇的类UIView.从继承关系上看,UIView是所有视图的根. 1.1.U ...

  7. hdu 1716(dfs)

    题目链接 : http://acm.hdu.edu.cn/showproblem.php?pid=1716     排列2   Problem Description Ray又对数字的列产生了兴趣:现 ...

  8. Facebook抛弃了HTML5,微信却捧火了它

    苹果普及了HTML5技术,Facebook押注HTML5上,却受到不小的打击,导致在后来一段时间里,唱衰HTML5的言论成为媒体的一种幸灾乐祸的态度,人人避而不谈.微信通过公众号的形式,以游戏.营销重 ...

  9. JS函数式编程【译】2.1 函数式编程语言

  10. n盏灯亮灭问题

    前几天看了华为的一个上机操作题,讲得是n盏灯亮灭问题,本质上还是数学问题,感觉很有趣,和大家分享一下,问题描述如下: 有n盏灯排成一排,依次标号1,2,…,n,每盏灯都有一根拉线开关,最初电灯都是关着 ...