Farm Tour POJ - 2135 (最小费用流)
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 题意:节点1把所有的节点都要跑一遍,然后回到节点1,每个边智能跑一次,问最短距离
思路:流量为2的最小费用流
#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cstring>
#include<sstream>
#include<cmath>
#include<stack>
#include<cstdlib>
#include <vector>
#include<queue>
using namespace std; #define ll long long
#define llu unsigned long long
#define INF 0x3f3f3f3f
#define PI acos(-1.0)
const int maxn = 1e4+;
const int mod = 1e9+; typedef pair<int,int>P;
struct edge{
int to,cap,cost,rev;
}; int V;
vector<edge> G[maxn];
int h[maxn];
int dist[maxn];
int prevv[maxn],preve[maxn]; void addedge(int from,int to,int cap,int cost)
{
G[from].push_back((edge){to,cap,cost,G[to].size()});
G[to].push_back((edge){from,,-cost,G[from].size() -});
} int min_cost_flow(int s,int t,int f)
{
int res =;
fill(h,h+V,);
while (f>)
{
priority_queue<P,vector<P>,greater<P> > que;
fill(dist,dist+V,INF);
dist[s] =;
que.push(P(,s));
while(!que.empty())
{
P p=que.top();
que.pop();
int v=p.second;
if(dist[v]<p.first)
continue;
for(int i=;i<G[v].size();i++) {
edge &e = G[v][i];
if(e.cap> && dist[e.to]>dist[v]+e.cost+h[v]-h[e.to])
{
dist[e.to] = dist[v] + e.cost + h[v] -h[e.to];
prevv[e.to] = v;
preve[e.to] = i;
que.push(P(dist[e.to],e.to));
}
} }
if(dist[t] == INF)
{
return -;
}
for(int v=;v<V;v++)
h[v] += dist[v];
int d=f;
for(int v=t;v!=s;v=prevv[v])
{
d=min(d,G[prevv[v]][preve[v]].cap);
}
f -= d;
res += d*h[t];
for(int v=t;v!=s;v=prevv[v])
{
edge &e = G[prevv[v]][preve[v]];
e.cap -= d;
G[v][e.rev].cap += d;
}
}
return res;
} int N,M;
int a[maxn],b[maxn],c[maxn]; void solve()
{
int s=,t=N-;
V=N;
for(int i=;i<M;i++)
{
addedge(a[i]-,b[i]-,,c[i]);
addedge(b[i]-,a[i]-,,c[i]);
}
printf("%d\n",min_cost_flow(s,t,));
} int main()
{
cin>>N>>M;
for(int i=;i<M;i++)
{
scanf("%d%d%d",&a[i],&b[i],&c[i]);
}
solve();
}
Farm Tour POJ - 2135 (最小费用流)的更多相关文章
- POJ - 2135最小费用流
题目链接:http://poj.org/problem?id=2135 今天学习最小费用流.模板手敲了一遍. 产生了一个新的问题:对于一条无向边,这样修改了正向边容量后,反向边不用管吗? 后来想了想, ...
- POJ 2135 /// 最小费用流最大流 非负花费 BellmanFord模板
题目大意: 给定一个n个点m条边的无向图 求从点1去点n再从点n回点1的不重叠(同一条边不能走两次)的最短路 挑战P239 求去和回的两条最短路很难保证不重叠 直接当做是由1去n的两条不重叠的最短路 ...
- 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 Farm Tour
Farm Tour Time Limit: 1000ms Memory Limit: 65536KB This problem will be judged on PKU. Original ID: ...
- POJ 2135 Farm Tour (最小费用最大流模板)
题目大意: 给你一个n个农场,有m条道路,起点是1号农场,终点是n号农场,现在要求从1走到n,再从n走到1,要求不走重复路径,求最短路径长度. 算法讨论: 最小费用最大流.我们可以这样建模:既然要求不 ...
- POJ Farm Tour
Farm Tour 题目: 约翰有N块地,家在1号,而N号是个仓库.农场内有M条道路(双向的),道路i连接这ai号地和bi号地,长度为ci. 约翰希望依照从家里出发,经过若干地后达到仓库.然后再返回家 ...
- poj 2351 Farm Tour (最小费用最大流)
Farm Tour Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 17230 Accepted: 6647 Descri ...
- Poj(2135),MCMF,模板
题目链接:http://poj.org/problem?id=2135 Farm Tour Time Limit: 1000MS Memory Limit: 65536K Total Submis ...
随机推荐
- Vue provide/inject 部分源码分析 实现响应式数据更新
provide/inject 数据响应式更新的坑及源码解析 下面是我自己曾经遇到 一个问题,直接以自己QA的形式来写吧 自问自答了,需要的同学也可以直接访问segmentfault地址 官网给出实例, ...
- java环境安装(win7)
首先,你应该已经安装了 java 的 JDK 了,笔者安装的是:jdk-7u13-windows-x64 接下来主要讲怎么配置 java 的环境变量,也是为了以后哪天自己忘记了做个备份 1.进入&qu ...
- IE6、7下块级元素设置display:inline-block不换行的解决办法
使用背景 在实际的工作中,我们有的时候会把块元素设置为inline-block,这样做的目的有2个,一是块元素能够排列到一行,二是块元素就形成包裹性,能够自适应content area,而不必设置宽和 ...
- 解决ie9以及以下console未定义
页面明明已经删除了所有的console,但是ie9下依旧会报错 console未定义 只能这样解决了 window.console = window.console || (function () { ...
- HTML5 参数传递
页面显示效果,如下图: 主页面代码: <!DOCTYPE html> <html> <head> <meta charset="UTF-8" ...
- mysql用户权限操作
mysql用户权限操作1.创建用户mysql -urootcreate database zabbix default charset utf8;grant all on zabbix.* to za ...
- JS案例练习 — 给div添加样式选择功能
附加效果图: CSS内容: <style> ; padding:0px} li{list-style:none} body{font:24px 'Microsoft YaHei'; col ...
- 六、C++离散傅里叶逆变换
C++离散傅里叶逆变换 一.序言: 该教程承接上文的离散傅里叶变换,用于进行离散傅里叶逆变换. 二.设计目标 对复数数组进行离散傅里叶逆变换,并生成可供使用的图像类. 三.详细步骤 输入:经傅里叶变换 ...
- c++输入
1. char c = getchar(); 输入单个字符,可输入空格.换行符. 2. cin >> s; 不读取空格或换行符. 3. getline(cin, s); 输入一行到字符串s ...
- Extjs4.1+desktop+SSH2 定义程序入口
app.js定义程序入口: MainController.js: 加载控制器: 外部组件引用入口loader.js 时间组件 静态变量组件: 引入comm.js index.jsp 验证打印 comm ...