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 ...
随机推荐
- Docker | 第六章:构建私有仓库
前言 上一章节,讲解了利用Dockerfile和commit进行自定义镜像的构建.大部分时候,公司运维或者实施部门在构建了符合公司业务的镜像环境后,一般上不会上传到公共资源库的.这就需要自己搭建一个私 ...
- Docker:安装部署RabbitMQ
前言 今天原本想讲解SpringBoot集成RabbitMQ的,临近开始写时才发现家里的电脑根本没有安装RabbitMQ呀.这下只好利用已有的阿里云服务器,直接Docker安装一下了,顺道记录下,算是 ...
- 服务器部署nginx报错 nginx: [warn] conflicting server name "localhost" on xxx.xxx.xxx.xxx:80, ignored
问题 修改nginx配置参数后,使用nginx -t检查配置. 提示successfull后就可以使用 nginx -s reload来重新加载配置 我配置的过程中遇到这样的问题,就是绑定了主机名后, ...
- P1681 最大正方形 Iand II
题目描述 在一个n*m的只包含0和1的矩阵里找出一个不包含0的最大正方形,输出边长. 输入输出格式 输入格式: 输入文件第一行为两个整数n,m(1<=n,m<=100),接下来n行,每行m ...
- 极飞P20农业无人机多机协同作业飞行
来自为知笔记(Wiz)
- table-列组
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- echarts折柱混合(图表数据与x轴对应显示)
一天24个小时,每个小时不一定都有对应的数据,所以后台给出的数据,只有每个时间点对应的数据,比如4点,给的是112,5点的242,其他时间没有,则只显示4点,5点时候的数据,那么现在对应的时间点就是后 ...
- 将vim配置成一个轻量的IDE开发工具
1.插件管理工具 2.安装插件 3.配置.vimrc 1.插件管理工具 vim的插件有很多,为了后面方便添加新的插件,我们需要一个插件管理工具来帮我们管理安装的插件,这里使用的是vim-pathoge ...
- Java开发工具IntelliJ IDEA创建Andriod项目示例说明
IntelliJ IDEA社区版作为一个轻量级的Java开发IDE,是一个开箱即用的Android开发工具. 注意:在本次的教程中我们将以Android平台2.2为例进行IntelliJ IDEA的使 ...
- C#值类型、引用类型的区别
在<C#类型简述>http://blog.csdn.net/letnet1981/article/details/48223831,中提到了值类型和引用类型,这里我们就来了解一下它们的区别 ...