POJ 2135
| Time Limit: 1000MS | Memory Limit: 65536K | |
| Total Submissions: 10840 | Accepted: 4011 |
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
Sample Input
4 5
1 2 1
2 3 1
3 4 1
1 3 2
2 4 2
Sample Output
6
Source
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
#include <queue> using namespace std; const int MAX_N = ;
const int INF = 1e9;
struct Edge {int from, to, cap, flow, cost; };
int N,M;
vector<Edge> edges;
vector<int> G[MAX_N];
int dist[MAX_N],prevv[MAX_N],preve[MAX_N];
bool inq[MAX_N]; void add_edge(int from, int to, int cap, int cost) {
edges.push_back(Edge {from, to, cap, , cost});
edges.push_back(Edge {to, from, , , -cost});
int m = edges.size();
G[from].push_back(m - );
G[to].push_back(m - );
} int min_cost_flow(int s, int t, int f) {
int res = ;
while(f > ) {
fill(dist, dist + N, INF);
dist[s] = ;
queue<int> q;
q.push(s); while(!q.empty()) {
int u = q.front(); q.pop();
inq[u] = ;
for(int i = ; i < G[u].size(); ++i) {
Edge& e = edges[ G[u][i] ];
if(e.cap > e.flow && dist[e.to] > dist[u] + e.cost) {
dist[e.to] = dist[u] + e.cost;
prevv[e.to] = u;
preve[e.to] = i;
if(!inq[e.to]) {
inq[e.to] = ;
q.push(e.to);
}
}
} }
if(dist[t] == INF) {
return -;
} int d = f;
for (int v = t; v != s; v = prevv[v]) {
Edge& e = edges[ G[ prevv[v] ][preve[v]] ];
d = min(d,e.cap - e.flow);
} //printf("d = %d dis = %d\n",d,dist[t]);
f -= d;
res += d * dist[t]; for (int v = t; v != s; v = prevv[v]) {
Edge& e = edges[ G[prevv[v]][ preve[v] ] ];
e.flow += d;
edges[ G[prevv[v]][ preve[v] ] ^ ].flow -= d;
} } return res;
} int main()
{
//freopen("sw.in","r",stdin); scanf("%d%d",&N,&M);
for(int i = ; i <= M; ++i) {
int a,b,cost;
scanf("%d%d%d",&a,&b,&cost);
add_edge(a - ,b - ,,cost);
add_edge(b - ,a - ,,cost);
} printf("%d\n",min_cost_flow(,N - ,));
//cout << "Hello world!" << endl;
return ;
}
POJ 2135的更多相关文章
- POJ 2135 Farm Tour (最小费用最大流模板)
题目大意: 给你一个n个农场,有m条道路,起点是1号农场,终点是n号农场,现在要求从1走到n,再从n走到1,要求不走重复路径,求最短路径长度. 算法讨论: 最小费用最大流.我们可以这样建模:既然要求不 ...
- POJ 2135 Farm Tour (网络流,最小费用最大流)
POJ 2135 Farm Tour (网络流,最小费用最大流) Description When FJ's friends visit him on the farm, he likes to sh ...
- poj 2135 Farm Tour 【无向图最小费用最大流】
题目:id=2135" target="_blank">poj 2135 Farm Tour 题意:给出一个无向图,问从 1 点到 n 点然后又回到一点总共的最短路 ...
- Poj(2135),MCMF,模板
题目链接:http://poj.org/problem?id=2135 Farm Tour Time Limit: 1000MS Memory Limit: 65536K Total Submis ...
- POJ 2135 Farm Tour (费用流)
[题目链接] http://poj.org/problem?id=2135 [题目大意] 有一张无向图,求从1到n然后又回来的最短路 同一条路只能走一次 [题解] 题目等价于求从1到n的两条路,使得两 ...
- POJ - 2135最小费用流
题目链接:http://poj.org/problem?id=2135 今天学习最小费用流.模板手敲了一遍. 产生了一个新的问题:对于一条无向边,这样修改了正向边容量后,反向边不用管吗? 后来想了想, ...
- 网络流(最小费用最大流):POJ 2135 Farm Tour
Farm Tour Time Limit: 1000ms Memory Limit: 65536KB This problem will be judged on PKU. Original ID: ...
- POJ 2135.Farm Tour 消负圈法最小费用最大流
Evacuation Plan Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 4914 Accepted: 1284 ...
- POJ 2135 Farm Tour 最小费用流
两条路不能有重边,既每条边的容量是1.求流量为2的最小费用即可. //#pragma comment(linker, "/STACK:1024000000,1024000000") ...
- 网络流 poj 2135
n个点 m条边 给m条边 求1->n n->1 最小花费,每条边最多走一次 两个最短路显然不行 会影响另外一条 #include<stdio.h> #include<al ...
随机推荐
- DrawTool多重笔之前奏 => 通过InkAnalyzer实现图形识别
这里要介绍的是通过InkAnalyzer来实现简单图形的识别,例如圆,椭圆,正方形,三角形等,当然你也可以通过扩展来实现自定义图形的识别,在使用InkAnalyzer前,你需要引用IAWinFX.dl ...
- node.js 使用 UglifyJS2 高效率压缩 javascript 文件
UglifyJS2 这个工具使用很长时间了,但之前都是在 gulp 自动构建 时用到了 UglifyJS 算法进行压缩. 最近玩了一下 UglifyJS2 ,做了一个 在线压缩javascript工具 ...
- android开发系列之多线程
今天在这篇博客里面,我只想谈谈自己对程序开发里面避无可避的一个问题-多线程的一些看法与思考. 其实说到多线程这个名称相信只要接触过软件这个行业的人都已经耳熟能详了,但是如果被问到到底什么才是多线程呢? ...
- 记录:asp.net mvc 中 使用 jquery 实现html5 实现placeholder 密码框 提示兼容password IE6
@{ViewBag.Title = "完美结合";} <script>var G_start_time = new Date;</script> <! ...
- Go defer延迟执行
defer用于延迟执行,可以类比于java或c++中的析构函数. 查看一段示例代码: func Contents(filename string) (string, error) { //打开文件 f ...
- NSAttributedString之设置字间距与行间距
// 调整行间距 NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithSt ...
- KafkaOffsetMonitor使用方法
(1)下载jar包 去网上搜索KafkaOffsetMonitor即可. 我这里共享了我的百度云连接:http://yun.baidu.com/s/1nvGjbDn 如果某一天我这个取消共享了,大家去 ...
- MongoDB学习笔记-游标
理解MongoDB的游标有两种维度:客户端和服务器端.下面将从这两方面来说明. 客户端 find方法返回值是一个游标.可以通过游标来对最终结果进行控制.比如限制结果数量,略过某一部分,根据任意键按任意 ...
- html 元素 变小手
要设置鼠标指针放在一个元素边界范围内时所用的光标形状,需要对元素的css属性cursor进行设置.cursor属性可能的值1.default 默认光标(通常是一个箭头)2.auto 默认.浏览器设置的 ...
- linux设置环境变量的方法
0.查看环境变量 export 1.直接执行命令,不过只有此次会话有效 export PATH=$PATH:/dir/I/want 2.修改profile文件 在里面加入: export PATH=& ...