题目大意:

给你一个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 mvc 删除栏目、栏目下又有子栏目的处理方式

  2. WordPress插件制作教程(四): 将数据保存到数据库

    上一篇讲解了添加菜单的方法,这一篇为大家讲解如何将数据保存到数据库中,并且显示在页面上,不会因提交表单时刷新页面输入框中内容消失.要实现这一功能我们需要借助WordPress函数来实现,下面就来讲解具 ...

  3. python爬虫下载youtube单个视频

    __author__ = 'Sentinel'import requestsimport reimport jsonimport sysimport shutilimport urlparse &qu ...

  4. py2exe生成exe后,运行exe时提示No module named * 的解决办法

    一个pymssql 的程序在解释器上运行正常,但是用py2exe打包后,提示 ImportError: No module named _mssql 百度了半天无果,然后bing,结果bing还是比百 ...

  5. python中的StringIO模块

    python中的StringIO模块 标签:python StringIO 此模块主要用于在内存缓冲区中读写数据.模块是用类编写的,只有一个StringIO类,所以它的可用方法都在类中.此类中的大部分 ...

  6. Hdu1093

    #include <stdio.h> int main() { int T,n; ; while(scanf("%d",&T)!=EOF){ while(sca ...

  7. 如何在异步请求时设置RequestHeader

    一.为何要用到setRequestHeader 通常在HTTP协议里,客户端像服务器取得某个网页的时候,必须发送一个HTTP协议的头文件,告诉服务器客户端要下载什么信息以及相关的参数.而 XMLHTT ...

  8. qt之窗口换肤(一个qss的坑:当类属性发现变化时需要重置qss,使用rcc资源文件)

    1.相关文章 Qt 资源系统qt的moc,uic,rcc命令的使用 2.概要    毕业两年了,一直使用的是qt界面库来开发程序,使用过vs08.10.13等开发工具,并安装了qt的插件,最近在做客户 ...

  9. WINAPI大全~

    http://www.alsprogrammingresource.com/win32_functions_g.html

  10. 15款免费WiFi入侵破解安全测试工具

    以下是的15款免费(接近免费)的WiFi网络入侵测试工具.这些工具将帮你发现流氓AP,弱Wi-Fi密码等安全隐患,在黑客光临之前把漏洞补上. 一.Vistumbler扫描器 Kismet是一个开源的W ...