POJ-2135 Farm Tour 最小费用流
题目链接: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 最小费用流相关推荐
- POJ 2135 Farm Tour 最小费用流
两条路不能有重边,既每条边的容量是1.求流量为2的最小费用即可. //#pragma comment(linker, "/STACK:1024000000,1024000000") ...
- POJ 2135 Farm Tour (费用流)
[题目链接] http://poj.org/problem?id=2135 [题目大意] 有一张无向图,求从1到n然后又回来的最短路 同一条路只能走一次 [题解] 题目等价于求从1到n的两条路,使得两 ...
- POJ 2135 Farm Tour (最小费用最大流)
Description 给出一张\(N\)个点\(M\)条边的带权无向图,结点编号从\(1\)到\(N\),求从\(1\)到\(N\)再到\(1\)的最短路,每条边最多走一次. Input 第一行给出 ...
- POJ 2135 Farm Tour amp;amp; HDU 2686 Matrix amp;amp; HDU 3376 Matrix Again 费用流求来回最短路...
累了就要写题解,近期总是被虐到没脾气. 来回最短路问题貌似也能够用DP来搞.只是拿费用流还是非常方便的. 能够转化成求满流为2 的最小花费.一般做法为拆点,对于 i 拆为2*i 和 2*i+1.然后连 ...
- POJ - 2135 Farm Tour(最小费用最大流)
题目链接:点击查看 题目大意:给出一个由n个点和m条边组成的无向图,每条边都有权值,求点1->点n->点1的最短路,且要求每条路至多经过一次,并使得途径的权值和最小 题目分析:虽然题目要求 ...
- POJ 1637 Sightseeing tour(最大流)
POJ 1637 Sightseeing tour 题目链接 题意:给一些有向边一些无向边,问能否把无向边定向之后确定一个欧拉回路 思路:这题的模型很的巧妙,转一个http://blog.csdn.n ...
- poj 1637 Sightseeing tour 混合欧拉图判定
POJ - 1637点我点我:-) Sightseeing tour Time Limit: 1000MS Memory Limit: 10000KB 64bit IO Format: %ll ...
- poj 1637 Sightseeing tour
http://poj.org/problem?id=1637 题意: 给出一张混合图,判断是否存在欧拉回路 原理: 1.存在欧拉回路的充要条件:所有点入度=出度 2.给无向边随便定向不会影响点的|出度 ...
- POJ 2135 最小费用最大流
思路: 源->1连费用0 流量2 其它的边 费用w 流量1 n->汇 费用0 流量2 最小费用流 搞定~ //By SiriusRen #include <queue> #in ...
最新文章
- Maven实战:pom.xml与settings.xml
- Jenkins 程序目录
- stm32 PWM互补输出
- 微信昵称上标电话号码,实用的新玩法
- Windows事件ID详细
- 解读 | 关于阿里巴巴架构大调整,有 7 个重点值得特别关注
- 如何使用PDF转换器将PDF转换成图片
- 发那科syst178_发那科系统报警大全
- 文氏桥振荡电路多类分析 LM386 DZ006套件
- 解决org.apache.hadoop.io.nativeio.NativeIO$Windows.createDirectoryWithMode0(Ljava/lang/String;I)
- 浅尝webSocket
- MySQL时间戳与时间格式的转换
- css实现圆形进度条
- python怎么求商_如何用python求差商?
- 使用pyqt和pyautogui来实现自动输出英文文本
- 微软学术搜索的新功能设想:用户账户系统——史经浩
- 标题 穿越雷区 java_标题:穿越雷区
- Vue.js中Twitter第三方登录api实现[亲测可用]
- 如何在线快速把json数据转excel表格
- 《高等数学》学习笔记二:导数与微分(持续更新)