POJ 2135 Farm Tour (最小费用最大流模板)
题目大意:
给你一个n个农场,有m条道路,起点是1号农场,终点是n号农场,现在要求从1走到n,再从n走到1,要求不走重复路径,求最短路径长度。
算法讨论:
最小费用最大流。我们可以这样建模:既然要求不能走重复路,就相当于每条边的容量是1,我们只可以单向流过容量为1的流量。但是要注意,对于每一条边来说,
它可能是去路的边,也可能是回路的边,所以这个图是个无向图。在加边的时候,两个方向的边都要加。所以要加两组的边,流量为1像正常一样加边就可以了。
然后我们考虑,求这个“环”就是相当于求从1..N的两条不相交路径,所以我们建立一个源点连向1,建立一个汇点连向N,然后在这个源点和汇点之间跑MinCostMaxFlow。
这里可能会有一个问题就是,如果这样流的话,流出多条路径怎么办?答案是:凉拌。我们把加入的这两条边的流量设置为2就可以了,这样就保证了从0..N+1只能流两条路径了。
然后就是随意做了。
代码:
#include <iostream>
#include <queue>
#include <cstring>
#include <cstdlib>
#include <algorithm>
#include <cstdio>
using namespace std; struct MCMF{
static const int maxn = + ;
static const int maxe = + ;
static const int oo = 0x3f3f3f3f; int n, m, s, t, tot;
int first[maxn], next[maxe];
int u[maxe], v[maxe], cap[maxe], flow[maxe], cost[maxe];
int dis[maxn], inque[maxn], a[maxn], pre[maxn]; MCMF(){memset(first, -, sizeof first); tot=;}
void Clear(){memset(first, -, sizeof first); tot=;}
void Add(int from, int to, int cp, int flw, int ct){
u[tot] = from; v[tot] = to; cap[tot] = cp; flow[tot] = ; cost[tot] = ct;
next[tot] = first[u[tot]];
first[u[tot]] = tot; ++ tot;
u[tot] = to; v[tot] = from; cap[tot] = ; flow[tot] = ; cost[tot] = -ct;
next[tot] = first[u[tot]];
first[u[tot]] = tot; ++ tot;
}
bool bfs(int &flw, int& ct){
for(int i = ; i <= n+; ++ i) dis[i] = oo;
memset(inque, , sizeof inque);
a[s] = oo; dis[s] = ; inque[s] = ; pre[s] = ; queue <int> q;
q.push(s);
while(!q.empty()){
int now = q.front(); q.pop();
inque[now] = ;
for(int i = first[now]; i != -; i = next[i]){
if(cap[i] > flow[i] && dis[v[i]] > dis[now] + cost[i]){
dis[v[i]] = dis[now] + cost[i];
pre[v[i]] = i;
a[v[i]] = min(a[now], cap[i] - flow[i]);
if(!inque[v[i]]){
q.push(v[i]); inque[v[i]] = ;
}
}
}
}
if(dis[t] == oo) return false;
flw += a[t];
ct += dis[t] * a[t];
int now = t;
while(now != s){
flow[pre[now]] += a[t];
flow[pre[now]^] -= a[t];
now = u[pre[now]];
}
return true;
}
int MinCostMaxFlow(int s, int t){
this->s = s; this->t = t;
int flw = , ct = ;
while(bfs(flw, ct));
return ct;
}
}Net; #define ONLINE_JUDGE
int main(){
#ifndef ONLINE_JUDGE
freopen("Poj2135.in", "r", stdin);
freopen("Poj2135.out", "w", stdout);
#endif int x, y, z;
scanf("%d%d", &Net.n, &Net.m);
Net.Clear();
Net.Add(, , , , );
Net.Add(Net.n, Net.n+, , , );
for(int i = ; i <= Net.m; ++ i){
scanf("%d%d%d", &x, &y, &z);
Net.Add(x, y, , , z);
Net.Add(y, x, , , z);
}
printf("%d\n", Net.MinCostMaxFlow(, Net.n+)); #ifndef ONLINE_JUDGE
fclose(stdin); fclose(stdout);
#endif
return ;
}
/*
Poj2135.in
10 15
7 1 13784
6 1 31692
4 9 16318
5 10 521
10 3 16420
5 2 11817
6 4 29070
8 5 13614
2 9 17168
8 1 19260
1 2 6076
2 3 1038
3 6 12917
2 6 17815
10 4 26493 Poj2135.out
56929
*/
POJ 2135
POJ 2135 Farm Tour (最小费用最大流模板)的更多相关文章
- poj 2135 Farm Tour 最小费用最大流建图跑最短路
题目链接 题意:无向图有N(N <= 1000)个节点,M(M <= 10000)条边:从节点1走到节点N再从N走回来,图中不能走同一条边,且图中可能出现重边,问最短距离之和为多少? 思路 ...
- POJ 2135 Farm Tour [最小费用最大流]
题意: 有n个点和m条边,让你从1出发到n再从n回到1,不要求所有点都要经过,但是每条边只能走一次.边是无向边. 问最短的行走距离多少. 一开始看这题还没搞费用流,后来搞了搞再回来看,想了想建图不是很 ...
- poj 2351 Farm Tour (最小费用最大流)
Farm Tour Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 17230 Accepted: 6647 Descri ...
- Farm Tour(最小费用最大流模板)
Farm Tour Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 18150 Accepted: 7023 Descri ...
- [poj] 1235 Farm Tour || 最小费用最大流
原题 费用流板子题. 费用流与最大流的区别就是把bfs改为spfa,dfs时把按deep搜索改成按最短路搜索即可 #include<cstdio> #include<queue> ...
- POJ2135 Farm Tour —— 最小费用最大流
题目链接:http://poj.org/problem?id=2135 Farm Tour Time Limit: 1000MS Memory Limit: 65536K Total Submis ...
- TZOJ 1513 Farm Tour(最小费用最大流)
描述 When FJ's friends visit him on the farm, he likes to show them around. His farm comprises N (1 &l ...
- POJ 2135 Farm Tour (费用流)
[题目链接] http://poj.org/problem?id=2135 [题目大意] 有一张无向图,求从1到n然后又回来的最短路 同一条路只能走一次 [题解] 题目等价于求从1到n的两条路,使得两 ...
- poj 2135 Farm Tour 最小费最大流
inf开太小错了好久--下次还是要用0x7fffffff #include<stdio.h> #include<string.h> #include<vector> ...
- POJ 2135 Farm Tour (网络流,最小费用最大流)
POJ 2135 Farm Tour (网络流,最小费用最大流) Description When FJ's friends visit him on the farm, he likes to sh ...
随机推荐
- BZOJ4195 NOI2015 程序自动分析
4195: [Noi2015]程序自动分析 Time Limit: 10 Sec Memory Limit: 512 MB Description 在实现程序自动分析的过程中,常常需要判定一些约束条件 ...
- 《转载》深入理解 cocos2d-x 坐标系
原文地址:http://www.cnblogs.com/lyout/p/3292702.html. 首先我们添加两个测试精灵(宽:27,高:40)到场景里面: CCSprite *sprite1 = ...
- Qt窗口句柄
关键字: 透明效果,异形,子窗口,控件,浮窗,同级句柄
- 数据链路层-点对点协议PPP
在通信质量较差的年代,在数据链路层使用可靠传输协议曾是一个好的办法.因此,能实现可靠传输的高级数据链路控制HDLC(High-Level Data Link Control)就称为当时比较流行的数据链 ...
- 【面试】Spring问答Top 25
本文由 ImportNew - 一直在路上 翻译自 howtodoinjava.欢迎加入翻译小组.转载请见文末要求. 本人收集了一些在大家在面试时被经常问及的关于Spring的主要问题,这些问题有可能 ...
- [转载]linux下mysql 自动备份
ySQL :: Linux 下自动备份数据库的 shell 脚本Linux 服务器上的程序每天都在更新 MySQL 数据库,于是就想起写一个 shell 脚本,结合 crontab,定时备份数据库.其 ...
- 1169 二叉树遍历(XCOJ DFS)
给出一棵二叉树的中序与后序排列.求出它的先序排列.(约定树结点用不同的大写字母表示,长度≤8). 样例输入 BADC BDCA 样例输出 ABCD #include <iostream> ...
- MHA环境搭建【3】node相关依赖的解决
mha的node软件包依赖于perl-DBD-Mysql 这个包,我之前有遇到过用yum安装perl-DBD-MySQL,安装完成后不能正常使用的情况,所以这里选择源码编译安装: perl5.10.1 ...
- BZOJ 1001 狼抓兔子
链接:http://www.lydsy.com/JudgeOnline/problem.php?id=1001 现在小朋友们最喜欢的"喜羊羊与灰太狼",话说灰太狼抓羊不到,但抓兔子 ...
- SendMessage参数
http://download.csdn.net/download/wshjldaxiong/4830242