题目大意:

给你一个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. hdu 5586 sum

    Problem Description There is a number sequence A1,A2....An,you can select a interval [l,r] or not,al ...

  2. js判断上传文件的类型和大小

    //检测文件大小和类型 function fileChange(target){ //检测上传文件的类型 if(!(/(?:jpg|gif|png|jpeg)$/i.test(target.value ...

  3. oracle 数据库用户登录相关

    oracle 数据库的安装 : 一: 安装的时候可以设定解锁的用户  一般默认是解锁soctt用户和hr用户  : oracle的超级用户是sysdba这个用户在安装的时候也可以设置密码,一 般自己使 ...

  4. 论如何进CSDN博客排名前500

    http://www.jtahstu.com/blog/post-71.html 目前该方法并不适用于博客园,显然写博客园的程序员智商要高些.

  5. [LA] 2031 Dance Dance Revolution

    Dance Dance Revolution Time limit: 3.000 seconds Mr. White, a fat man, now is crazy about a game nam ...

  6. 【ROC曲线】关于ROC曲线、PR曲线对于不平衡样本的不敏感性分析说引发的思考

    ROC曲线 在网上有很多地方都有说ROC曲线对于正负样本比例不敏感,即正负样本比例的变化不会改变ROC曲线.但是对于PR曲线就不一样了.PR曲线会随着正负样本比例的变化而变化.但是没有一个有十分具体和 ...

  7. iPhone 和Android应用,特殊的链接:打电话,短信,email;

    http://ice-k.iteye.com/blog/1426526 下面的这篇文章主要是说,网页中的链接如何写,可以激活电话的功能. 例如,页面中展示的是一个电话号码,当用户在手机浏览器里面点击这 ...

  8. LeetCode_Spiral Matrix

    Given a matrix of m x n elements (m rows, n columns), return all elements of the matrix in spiral or ...

  9. 粗窥STARTUP.A51和INIT.A51

    也许大家曾经注意过使用Keil C51来编译链接生成目标代码之后,在我们的主程序之前有些代码不是我们写的,它们从哪里来的? Keil C51的\C51\LIB目录下有STARTUP.A51和INIT. ...

  10. 福建省队集训被虐记——DAY2

    唉--第二天依然被虐--但是比第一天好一点--我必须负责任的指出:志灿大神出的题比柯黑的不知道靠谱到哪里去了--柯黑出的简直不可做 但是被虐的命运是无法改变的--求各位神犇别D我 黄巨大真是强啊,不愧 ...