题目大意:

给你一个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 (最小费用最大流模板)的更多相关文章

  1. poj 2135 Farm Tour 最小费用最大流建图跑最短路

    题目链接 题意:无向图有N(N <= 1000)个节点,M(M <= 10000)条边:从节点1走到节点N再从N走回来,图中不能走同一条边,且图中可能出现重边,问最短距离之和为多少? 思路 ...

  2. POJ 2135 Farm Tour [最小费用最大流]

    题意: 有n个点和m条边,让你从1出发到n再从n回到1,不要求所有点都要经过,但是每条边只能走一次.边是无向边. 问最短的行走距离多少. 一开始看这题还没搞费用流,后来搞了搞再回来看,想了想建图不是很 ...

  3. poj 2351 Farm Tour (最小费用最大流)

    Farm Tour Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 17230   Accepted: 6647 Descri ...

  4. Farm Tour(最小费用最大流模板)

    Farm Tour Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 18150   Accepted: 7023 Descri ...

  5. [poj] 1235 Farm Tour || 最小费用最大流

    原题 费用流板子题. 费用流与最大流的区别就是把bfs改为spfa,dfs时把按deep搜索改成按最短路搜索即可 #include<cstdio> #include<queue> ...

  6. POJ2135 Farm Tour —— 最小费用最大流

    题目链接:http://poj.org/problem?id=2135 Farm Tour Time Limit: 1000MS   Memory Limit: 65536K Total Submis ...

  7. 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 ...

  8. POJ 2135 Farm Tour (费用流)

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

  9. poj 2135 Farm Tour 最小费最大流

    inf开太小错了好久--下次还是要用0x7fffffff #include<stdio.h> #include<string.h> #include<vector> ...

  10. POJ 2135 Farm Tour (网络流,最小费用最大流)

    POJ 2135 Farm Tour (网络流,最小费用最大流) Description When FJ's friends visit him on the farm, he likes to sh ...

随机推荐

  1. Asp.net mvc4 + HighCharts + 柱状图

    前端代码: @{ Layout = null;} <!DOCTYPE html> <html><head> <meta name="viewport ...

  2. php中的时区设置

    date_default_timezone_set('PRC'); 设置为中国时区

  3. 用Python制作markdown编辑器

    还记得在上篇提到的rest-framework,文档中提到了markdown也是可选应用. 那么这篇我们就来尝试使用markdown来制作一个在线的可以预览的editor. 安装 Python Mar ...

  4. IOS 开发之文件管理

    一.iOS中的沙盒机制 iOS应用程序只能对自己创建的文件系统读取文件,这个独立.封闭.安全的空间,叫做沙盒.它一般存放着程序包文件(可执行文件).图片.音频.视频.plist文件.sqlite数据库 ...

  5. 手机QQ v4.2 有感

    因为经常宅宿舍,很少出门,所以无论微信还是手机QQ都很少使用. 刚好最近见别人发来的表情我的2012自改版QQ总是无法解析,只能显示[吼叫].[啦啦]之类的字符,于是更新了v4.2的手机QQ把玩一番, ...

  6. 使用StreamReader与StreamWriter进行文本文件读写

    namespace filetest { class FileUtil { public static void WriteFile(string file) { using (FileStream ...

  7. scaletype

    http://www.myexception.cn/image/726203.html 图片说明Andorid中ImageView的不同属性ScaleType的区别 ImageView是Android ...

  8. Java数据结构: java.util.BitSet源码学习

    接着上一篇Blog:一道面试题与Java位操作 和 BitSet 库的使用,分析下Java源码中BitSet类的源码. 位图(Bitmap),即位(Bit)的集合,是一种常用的数据结构,可用于记录大量 ...

  9. Duff策略

    Tom Duff首先在C语言中提出了展开循环的构想,所以这种模式被称之为Duff策略.Duff策略背后的思想是每一次循环完成标准循环的1-8次.首先通过数组值得总数除以8来取定循环次数.Duff发现对 ...

  10. 《Java程序员面试笔试宝典》之Java变量命名有哪些规则

    在Java语言中,变量名.函数名.数组名统称为标识符,Java语言规定标识符只能由字母(a~z,A~Z).数字(0~9).下划线(_)和$组成,并且标识符的第一个字符必须是字母.下划线或$.此外,标识 ...