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 ...
随机推荐
- IE兼容性标签和条件注释
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- C++中的函数指针用法
代码: #include <iostream> #include <cstdio> typedef void (*FUN)(); using namespace std; vo ...
- C++的学习记录 - 0
最近玩Arduino发现,在编写函数库的时候要用到C++.正好手头有一本教材,于是时隔2年,开始重学. 又看到重载.构造.拷贝这些词竟然还有些小兴奋. 开个系列日志记录一下学习过程中的问题和体会. 看 ...
- 模拟Sping MVC
在Spring MVC中,将一个普通的java类标注上Controller注解之后,再将类中的方法使用RequestMapping注解标注,那么这个普通的java类就够处理Web请求,示例代码如下: ...
- C#编程实现朴素贝叶斯算法下的情感分析
C#编程实现 这篇文章做了什么 朴素贝叶斯算法是机器学习中非常重要的分类算法,用途十分广泛,如垃圾邮件处理等.而情感分析(Sentiment Analysis)是自然语言处理(Natural Lang ...
- jQuery验证表单格式
工作之余整理一些工作中编写的代码,记录自己工作中的技术要点,便于自己记忆已经整合.以下是关于此jQuery验证的一些标记以及使用方法: 整个js代码都放入jquery_validate_1.1.0.j ...
- 用jquery可以用使用serialize()序列化表单值,那有没有什么方法可以将值填充到表单中呢? (一段不错的代码)
serialize()的作用,是生成一个类似这种格式的字符串用于ajax提交 a=&b=&c=.你想将值填写到表单,首先要有值,然后就是表单控件的id或者能唯一定位控件的属性.然后就$ ...
- Oracle 安装时候的网络相关内核参数
http://www.cnblogs.com/gaojian/archive/2012/10/12/2721284.html http://blog.chinaunix.net/uid-2442641 ...
- 安装ArchLinux的参考分区方案
其实就是从Archwiki上搬运过来的 = =. 分区方案 虽然有一些关于分区方案的通用建议,但没有严格的准则.有许多影响分区方案的因素,例如对灵活性的期望,访问速度,安全性以及可用磁盘空间的硬性限制 ...
- 返回本机的外网ip地址
; ); string ip = tempip.Replace("]", "").Replace(" ...