POJ 2135 Farm Tour(最小费用最大流)
Description
To show off his farm in the best way, he walks a tour that starts at his house, potentially travels through some fields, and ends at the barn. Later, he returns (potentially through some fields) back to his house again.
He wants his tour to be as short as possible, however he doesn't want to walk on any given path more than once. Calculate the shortest tour possible. FJ is sure that some tour exists for any given farm.
Input
* Lines 2..M+1: Three space-separated integers that define a path: The starting field, the end field, and the path's length.
Output
题目大意:给一个n个点m条边的无向图,求从1走到n再从n走到1的最短路径,要求边不重复。
思路:建立一个附加源点S,连一条边到点1,容量为2,费用为0;建立一个附加汇点T,从n连一条边到T,容量为2,费用为0;对于每条边u、v,连一条边u到v,容量为1,费用为距离,再连一条边v到u,容量为1,费用为距离。最小费用最大流为答案。
建图正确性略微说明:可以考虑两次从1走到n,边不重复,那么建图流两个流量从源点到汇点,最小的费用就保证了距离最短。由于边长度为非负数,那么最小花费一定不会是一条路径从u到v,另一条路径从v到u,如果这样走了两次,还不如两条路径都不经过u-v,都走另外一条路的后继路径(不要跟我说长度是0怎么破)
PS:忘了改数组大小又TLE了一次T_T,怎么老是忘
#include <cstdio>
#include <cstring>
#include <queue>
#include <iostream>
#include <algorithm>
using namespace std; const int MAXV = ;
const int MAXE = ;
const int INF = 0x7f7f7f7f; struct SPFA_COST_FLOW {
bool vis[MAXV];
int head[MAXV], dis[MAXV], pre[MAXV];
int to[MAXE], next[MAXE], cost[MAXE], flow[MAXE];
int n, st, ed, ecnt; void init() {
memset(head, , sizeof(head));
ecnt = ;
} void add_edge(int u, int v, int c, int w) {
to[ecnt] = v; flow[ecnt] = c; cost[ecnt] = w; next[ecnt] = head[u]; head[u] = ecnt++;
to[ecnt] = u; flow[ecnt] = ; cost[ecnt] = -w; next[ecnt] = head[v]; head[v] = ecnt++;
} bool spfa() {
memset(vis, , sizeof(vis));
memset(dis, 0x7f, sizeof(dis));
queue<int> que; que.push(st);
vis[st] = true; dis[st] = ;
while(!que.empty()) {
int u = que.front(); que.pop();
vis[u] = false;
for(int p = head[u]; p; p = next[p]) {
int &v = to[p];
if(flow[p] && dis[v] > dis[u] + cost[p]) {
dis[v] = dis[u] + cost[p];
pre[v] = p;
if(!vis[v]) {
vis[v] = true;
que.push(v);
}
}
}
}
return dis[ed] < INF;
} int maxFlow, minCost; int min_cost_flow(int ss, int tt, int nn) {
st = ss, ed = tt, n = nn;
maxFlow = minCost = ;
while(spfa()) {
minCost += dis[ed];
int u = ed, tmp = INF;
while(u != st) {
tmp = min(tmp, flow[pre[u]]);
u = to[pre[u] ^ ];
}
u = ed;
while(u != st) {
flow[pre[u]] -= tmp;
flow[pre[u] ^ ] += tmp;
u = to[pre[u] ^ ];
}
maxFlow += tmp;
}
return minCost;
}
} G; int n, m; int main() {
scanf("%d%d", &n, &m);
G.init();
int ss = n + , tt = n + ;
while(m--) {
int u, v, c;
scanf("%d%d%d", &u, &v, &c);
G.add_edge(u, v, , c);
G.add_edge(v, u, , c);
}
G.add_edge(ss, , , );
G.add_edge(n, tt, , );
printf("%d\n", G.min_cost_flow(ss, tt, tt));
}
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 ...
- [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 ...
- Farm Tour(最小费用最大流模板)
Farm Tour Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 18150 Accepted: 7023 Descri ...
- 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 ...
随机推荐
- CSS选择器种类及使用方法
css选择器 有通配符选择器书写格式:*+{声名块} 并集选择器/组合选择器 书写格式;元素或类或id+""+元素或类或id+","+元素或类或id{声明块} ...
- element 表单的input循环生成,并可单个input失去焦点单个验证并保存; (多个表单实例)
<div class="box_item"> <el-form ref="aList" :model="form" :ru ...
- zepto 基础知识(5)
81.width width() 类型:number width(value) 类型:self width(function(index,oldWidth){....}) 类型:self 获取对象集合 ...
- python查找目录及子目录下特定文件
写这篇博客的缘由: 面试归来翻脉脉发现一个陌生的朋友提出一个面试题,设计实现遍历目录及子目录,抓取.pyc文件. 并贴出两种实现方法: 个人感觉,这两种方法中规中矩,不像是python的风格.pyth ...
- HTTP基本内容
*********************HTTP基本交互*************************** HTTP请求格式:HTTP 请求由三部分组成:请求行.请求头和请求正文请求行: 请求方 ...
- mysql-新增表前判断同名表是否存在
新增多个表时,如果有同名表会报错,导致其中一个表不能正确创建,此时可以用以下语句进行判断: DROP TABLE IF EXISTS USER; --判断表是否存在,如果存在就删除! CREATE T ...
- sqlite内存数据库和文件数据库的同步[转]
由于sqlite对多进程操作支持效果不太理想,在项目中,为了避免频繁读写 文件数据库带来的性能损耗,我们可以采用操作sqlite内存数据库,并将内存数据库定时同步到文件数据库中的方法. 实现思路如下: ...
- linux后台程序开发常用工具
linux开发工具: 1.编辑工具:1)sourceInsight2)Notepad++3)UltraEdit4)Altova XMLSpy 2.linux服务器访问工具:1)FileZilla2)X ...
- 通过SVI实现VLAN间通信
两个不同网段的计算机与三层交换机直连,通过SVI实现VLAN间通信vlan 1 //几个不同网段就创建几个VLANvlan 2 int f0/1 //划分VLANswitchport mode acc ...
- 人人都会设计模式:观察者模式--Observer
https://segmentfault.com/a/1190000012295887 观察者模式是抽像通知者和观察者,达到具体通知者跟具体观察者没有偶合.能达到不管是切换通知者,或者是切换观察者,都 ...