POJ2135 Farm Tour
Time Limit: 2MS | Memory Limit: 65536KB | 64bit IO Format: %I64d & %I64u |
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
/*by SilverN*/
#include<iostream>
#include<algorithm>
#include<cstring>
#include<cstdio>
#include<cmath>
#include<queue>
using namespace std;
const int INF=;
const int mxn=;
int head[mxn],dis[mxn],pr[mxn];
bool inqu[mxn];
int n,m;
int s,t;
int ans;
int cnt=;
struct edge{
int from,to,next,v,c;
}e[mxn*];
void add_edge(int f,int t,int v,int c){
e[++cnt]=(edge){f,t,head[f],v,c};head[f]=cnt;
e[++cnt]=(edge){t,f,head[t],,-c};head[t]=cnt;
}
bool SPFA(){
queue<int>q;
memset(inqu,false,sizeof(inqu));
for(int i=;i<=t;i++)dis[i]=INF;
dis[s]=;
inqu[s]=;
q.push(s);
int i,j;
while(!q.empty()){
int u=q.front();q.pop();
inqu[u]=false;
for(i=head[u];i;i=e[i].next){
int v=e[i].to;
if(e[i].v && dis[u]+e[i].c<dis[v]){
dis[v]=dis[u]+e[i].c;
pr[v]=i;
if(!inqu[v]){
q.push(v);
inqu[v]=true;
}
}
}
}
return dis[t]!=INF;
}
void mcf(){
int i;
while(SPFA()){
int temp=INF;
for(i=pr[t];i;i=e[pr[i]].from)temp=min(temp,e[i].v);
ans+=dis[t]*temp;
for(i=pr[t];i;i=e[pr[i]].from){
e[i].v-=temp;
e[i^].v+=temp;
}
}
}
int main(){
scanf("%d%d",&n,&m);
s=;t=n+;
int x,y,c;
for(int i=;i<=m;i++){
scanf("%d%d%d",&x,&y,&c);
add_edge(x,y,,c);
add_edge(y,x,,c);
}
add_edge(s,,,);
add_edge(n,t,,);
mcf();
printf("%d\n",ans);
}
POJ2135 Farm Tour的更多相关文章
- POJ2135 Farm Tour —— 最小费用最大流
题目链接:http://poj.org/problem?id=2135 Farm Tour Time Limit: 1000MS Memory Limit: 65536K Total Submis ...
- poj2135 Farm Tour(费用流)
Description When FJ's friends visit him on the farm, he likes to show them around. His farm comprise ...
- POJ2135 Farm Tour(最小费用最大流)
题目问的是从1到n再回到1边不重复走的最短路,本质是找1到n的两条路径不重复的尽量短的路. #include<cstdio> #include<cstring> #includ ...
- [poj2135]Farm Tour(最小费用流)
解题关键:最小费用流 代码一:bellma-ford $O(FVE)$ bellman-ford求最短路,并在最短路上增广,速度较慢 #include<cstdio> #include& ...
- 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
Farm Tour Time Limit: 1000ms Memory Limit: 65536KB This problem will be judged on PKU. Original ID: ...
- POJ Farm Tour
Farm Tour 题目: 约翰有N块地,家在1号,而N号是个仓库.农场内有M条道路(双向的),道路i连接这ai号地和bi号地,长度为ci. 约翰希望依照从家里出发,经过若干地后达到仓库.然后再返回家 ...
- [网络流]Farm Tour(费用流
Farm Tour 题目描述 When FJ's friends visit him on the farm, he likes to show them around. His farm compr ...
- Farm Tour(最小费用最大流模板)
Farm Tour Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 18150 Accepted: 7023 Descri ...
随机推荐
- java 20 - 9 带有缓冲区的字节输出流和字节输入流
由之前字节输入的两个方式,我们可以发现,通过定义数组读取数组的方式比一个个字节读取的方式快得多. 所以,java就专门提供了带有缓冲区的字节类: 缓冲区类(高效类) 写数据:BufferedOutpu ...
- [WPF]资源字典——程序集之间的资源共享 简单换皮肤
直接上代码,已便已后自己查况阅,新手也可以看! 1.新建一个资料类和一个WPF工程 2.APP.XAML应该资源字典,注意应Source格式,前面一定要有“/” <ResourceDiction ...
- variable 'xxx' unsafe in 'case'的处理
问题描述: case get(?Player_LoopTaskInfo) of {TargetCnt, TaskStar, TaskExp} -> ok; _ -> throw(" ...
- [转]Ubuntu 用vsftpd 配置FTP服务器
FROM : http://www.cnblogs.com/CSGrandeur/p/3754126.html 网上的文章好难懂啊..只想要简单粗暴,弄好能用就行啊,复杂的以后研究不行吗...折腾好久 ...
- 小程序基础09:视图层之WXML
1.WXML WXML是框架设计的一套标签语言,结合基础组件,事件系统,可以构建出页面的结构. 用以下的例子来看看WXML有什么能力: 1.1数据绑定 <view>{{age}}</ ...
- webapi同一个Controller多个函数
现在用webapi发现一个问题,比如用get方法调用的的时候函数名好像没用,不管你写什么名称,它只认调用相同参数的一个函数,而且以get开头的函数虽然名称不同但是只要参数相同就会报错,无法调用怎么回事 ...
- 第十章实践——系统级I/O代码运行
第十章实践——系统级I/O代码运行 实验代码清单如下: 1. cp1——复制一个文件到另一个文件中(两个已经存在的文件) 复制前: 执行后结果 2. setecho.echostate——改变.显示输 ...
- 信息安全系统设计基础实验一 20135211&20135216
北京电子科技学院(BESTI) 实 验 报 告 封面 课程:信息安全系统设计基础 班级:1352 姓名:(按贡献大小排名)李行之 刘蔚然 ...
- Python面试题 —— 获取列表中位数
中位数是一个可将数值集合划分为相等的上下两部分的一个数值.如果列表数据的个数是奇数,则列表中间那个数据就是列表数据的中位数:如果列表数据的个数是偶数,则列表中间那2个数据的算术平均值就是列表数据的中位 ...
- 【android】实现一个自己的标题栏
完整项目下载 背景:项目中使用标题栏,只是简单的include一个标题栏的视图,赋值.控制元素显示.点击事件都要自己搞,不优雅! 要求: 1:对现有代码入侵最小 2:使用足够简单 OK,围绕着这个需求 ...