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 ...
随机推荐
- Linux安装、卸载软件
在linux环境中,尤其是cenos中安装过一些软件,一般是二进制安装与源码安装,现小结一下linux中的安装与卸载. 一.通常Linux应用软件的安装包有三种: 1) tar包,如software- ...
- maven总结2
依赖 maven版本:apache-maven-3.1.1 IDE: springsource 默认支持maven集成 若使用的是eclipse,则需要先安装m2ec ...
- 创建Java Web监听器
之前从Java web一路学习过来,一直没有学习过Servlet容器类的一些高级用法,因为学完简单的JSP以及Servlet编写之后就开始了Spring的学习 对web应用的一些常用变量进行 appl ...
- 实现Maya FEM节点
准备实现FEM节点. 发现一种让自定义的Locator以及它的变换节点自动命名的方法.代码如下: void FEMSimulationNode::postConstructor() { MFnDepe ...
- CSS padding margin border属性
主要定义四个区域:内容(content).内边距(padding).边框(border)和外边距(margin) margin:层的边框以外留的空白 background-color:背景颜色 bac ...
- 认识实验室信息管理系统(LIMS)
在当今互联网如日中天的大环境下,各种伴随着互联网的产物如p2p,o2o在如火如荼的进行着,吸引了大量的开发人员都涌向了这个行业,所有的技术似乎都在围绕着互联网发展,传统行业软件开发的人气和关注度就相形 ...
- C语言 百炼成钢9
//题目25:求1+2!+3!+...+20!的和 #define _CRT_SECURE_NO_WARNINGS #include<stdio.h> #include<stdlib ...
- box-css3
父容器样式必须有定义:"{ display: -webkit-box }" 现象:水平时只能在一行布局,子容器在垂直方向上会填充父容器. 技巧:可以做水平居中和垂直居中.也可以实现 ...
- 【原创】Junit4详解一:Junit总体介绍
Junit是一个可编写重复测试的简单框架,是基于Xunit架构的单元测试框架的实例.Junit4最大的改进是大量使用注解(元数据),很多实际执行过程都在Junit的后台做完了,而且写test case ...
- MANIFEST.INF!JAR规范中
MANIFEST.INF!JAR规范中 META-INF 目录中内容心得.顺带整理了网上资料,提供地址 标签: jarjava产品sunantapache 2012-03-31 17:09 2768人 ...