题目链接:http://poj.org/problem?id=2135

  很容易看出来时最小费用流,但这里要注意是无向边,所以要建立两条边。为了满足退流时,花费还是最小,反向边的花费要为相反数。

 1 //STATUS:G++_AC_32MS_980KB
 2 #include<stdio.h>
 3 #include<stdlib.h>
 4 #include<string.h>
 5 #include<math.h>
 6 #include<iostream>
 7 #include<string>
 8 #include<algorithm>
 9 #include<vector>
10 #include<queue>
11 #include<stack>
12 #include<map>
13 using namespace std;
14 #define LL __int64
15 #define pii pair<int,int>
16 #define Max(a,b) ((a)>(b)?(a):(b))
17 #define Min(a,b) ((a)<(b)?(a):(b))
18 #define mem(a,b) memset(a,b,sizeof(a))
19 #define lson l,mid,rt<<1
20 #define rson mid+1,r,rt<<1|1
21 const int MAX=1010,INF=0x3f3f3f3f;
22 const LL LLNF=0x3f3f3f3f3f3f3f3fLL;
23
24 struct Edge{
25     int u,v,cap,w;
26 }e[MAX*40];
27
28 int d[MAX],first[MAX],next[MAX*40],inq[MAX],p[MAX];
29 int n,m,s,t,mt;
30
31 void adde(int a,int b,int c,int val){
32     e[mt].u=a,e[mt].v=b,e[mt].cap=c,e[mt].w=val;
33     next[mt]=first[a],first[a]=mt++;
34     e[mt].u=b,e[mt].v=a,e[mt].cap=0,e[mt].w=-val;
35     next[mt]=first[b],first[b]=mt++;
36 }
37
38 int Mincost()
39 {
40     int i,j,x,a,cost=0;
41     queue<int> q;
42     p[s]=-1;
43     while(1){
44         a=INF;
45         mem(d,0x3f);
46         mem(inq,0);
47         d[s]=0;
48         q.push(s);
49         while(!q.empty()){
50             x=q.front();q.pop();
51             inq[x]=0;
52             for(i=first[x];i!=-1;i=next[i]){
53                 if(e[i].cap && d[e[i].u]+e[i].w<d[e[i].v]){
54                     d[e[i].v]=d[e[i].u]+e[i].w;
55                     p[e[i].v]=i;
56                     if(!inq[e[i].v]){
57                         q.push(e[i].v);
58                         inq[e[i].v]=1;
59                     }
60                 }
61             }
62         }
63         if(d[t]==INF)break;
64         for(i=p[t];i!=-1;i=p[e[i].u])
65             if(e[i].cap<a)a=e[i].cap;
66         for(i=p[t];i!=-1;i=p[e[i].u]){
67             e[i].cap-=a;
68             e[i^1].cap+=a;
69         }
70         cost+=d[t]*a;
71     }
72     return cost;
73 }
74
75 int main()
76 {
77  //   freopen("in.txt","r",stdin);
78     int i,j,a,b,c;
79     while(~scanf("%d%d",&n,&m))
80     {
81         t=n;
82         s=mt=0;
83         mem(first,-1);
84         for(i=0;i<m;i++){
85             scanf("%d%d%d",&a,&b,&c);
86             adde(a,b,1,c);
87             adde(b,a,1,c);
88         }
89         adde(s,1,2,0);
90
91         printf("%d\n",Mincost());
92     }
93     return 0;
94 }

转载于:https://www.cnblogs.com/zhsl/archive/2012/12/15/2818875.html

POJ-2135 Farm Tour 最小费用流相关推荐

  1. POJ 2135 Farm Tour 最小费用流

    两条路不能有重边,既每条边的容量是1.求流量为2的最小费用即可. //#pragma comment(linker, "/STACK:1024000000,1024000000") ...

  2. POJ 2135 Farm Tour (费用流)

    [题目链接] http://poj.org/problem?id=2135 [题目大意] 有一张无向图,求从1到n然后又回来的最短路 同一条路只能走一次 [题解] 题目等价于求从1到n的两条路,使得两 ...

  3. POJ 2135 Farm Tour (最小费用最大流)

    Description 给出一张\(N\)个点\(M\)条边的带权无向图,结点编号从\(1\)到\(N\),求从\(1\)到\(N\)再到\(1\)的最短路,每条边最多走一次. Input 第一行给出 ...

  4. POJ 2135 Farm Tour amp;amp; HDU 2686 Matrix amp;amp; HDU 3376 Matrix Again 费用流求来回最短路...

    累了就要写题解,近期总是被虐到没脾气. 来回最短路问题貌似也能够用DP来搞.只是拿费用流还是非常方便的. 能够转化成求满流为2 的最小花费.一般做法为拆点,对于 i 拆为2*i 和 2*i+1.然后连 ...

  5. POJ - 2135 Farm Tour(最小费用最大流)

    题目链接:点击查看 题目大意:给出一个由n个点和m条边组成的无向图,每条边都有权值,求点1->点n->点1的最短路,且要求每条路至多经过一次,并使得途径的权值和最小 题目分析:虽然题目要求 ...

  6. POJ 1637 Sightseeing tour(最大流)

    POJ 1637 Sightseeing tour 题目链接 题意:给一些有向边一些无向边,问能否把无向边定向之后确定一个欧拉回路 思路:这题的模型很的巧妙,转一个http://blog.csdn.n ...

  7. poj 1637 Sightseeing tour 混合欧拉图判定

    POJ - 1637点我点我:-) Sightseeing tour Time Limit: 1000MS   Memory Limit: 10000KB   64bit IO Format: %ll ...

  8. poj 1637 Sightseeing tour

    http://poj.org/problem?id=1637 题意: 给出一张混合图,判断是否存在欧拉回路 原理: 1.存在欧拉回路的充要条件:所有点入度=出度 2.给无向边随便定向不会影响点的|出度 ...

  9. POJ 2135 最小费用最大流

    思路: 源->1连费用0 流量2 其它的边 费用w 流量1 n->汇 费用0 流量2 最小费用流 搞定~ //By SiriusRen #include <queue> #in ...

最新文章

  1. Maven实战:pom.xml与settings.xml
  2. Jenkins 程序目录
  3. stm32 PWM互补输出
  4. 微信昵称上标电话号码,实用的新玩法
  5. Windows事件ID详细
  6. 解读 | 关于阿里巴巴架构大调整,有 7 个重点值得特别关注
  7. 如何使用PDF转换器将PDF转换成图片
  8. 发那科syst178_发那科系统报警大全
  9. 文氏桥振荡电路多类分析 LM386 DZ006套件
  10. 解决org.apache.hadoop.io.nativeio.NativeIO$Windows.createDirectoryWithMode0(Ljava/lang/String;I)
  11. 浅尝webSocket
  12. MySQL时间戳与时间格式的转换
  13. css实现圆形进度条
  14. python怎么求商_如何用python求差商?
  15. 使用pyqt和pyautogui来实现自动输出英文文本
  16. 微软学术搜索的新功能设想:用户账户系统——史经浩
  17. 标题 穿越雷区 java_标题:穿越雷区
  18. Vue.js中Twitter第三方登录api实现[亲测可用]
  19. 如何在线快速把json数据转excel表格
  20. 《高等数学》学习笔记二:导数与微分(持续更新)

热门文章

  1. jQuery实现影院选座订座效果
  2. 前端—每天5道面试题(7)
  3. HTML+CSS制作Windows启动加载动画
  4. LeetCode58. 最后一个单词的长度
  5. 感觉养老金越涨差距越大,有人提议高于5000的不再上涨,合理吗?
  6. 大家有没有发现,女生带上口罩后,感觉颜值升高了,尤其眼睛好看?
  7. 大家为什么去国企后都不想跳槽了?
  8. 为什么你的店铺不赚钱?
  9. 穷的时候要记住这5点,才能有机会翻身
  10. 3类兼职渠道,赚钱的方法来了