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 ...
随机推荐
- ant 自动构建血泪史
1. android.bat update project -p . -t xxx 其中: xxx 为 targetid 特别注意的是: targetid 不等于 API Level.... 2. ...
- Android service ( 二) 远程服务
通常每个应用程序都在它自己的进程内运行,但有时需要在进程间传递对象,你可以通过应用程序UI的方式写个运行在一个不同的进程中的service.在android平台中,一个进程通常不能访问其他进程中的内存 ...
- 原生js颗粒页换图效果
<!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF-8&quo ...
- Spring中使用Quartz
package com.ncs.hj; import org.quartz.JobExecutionContext; import org.quartz.JobExecutionException; ...
- 微软职位内部推荐-Software Development Engineering II
微软近期Open的职位: Job Title: Software Development Engineering II Work Location: Suzhou, China Enterprise ...
- [tomcat7源码学习]初始化之catalina.home和catalina.base(转)
我们在代码中为了获取某个配置文件路径下的文件经常会这么写 String tomcatPath = System.getProperty("catalina.home") + &qu ...
- An Introduction to Interactive Programming in Python (Part 1) -- Week 2_2 练习
#Practice Exercises for Logic and Conditionals # Solve each of the practice exercises below. # 1.Wri ...
- 社交网站好友储存设计和实现(PHP+MySQL)
最近手头的一个网站新增社交功能,用户可以互加好友. 通常,对好友列表设计是新增一个好友,就往好友列表新增一行,当要查询一个用户好友 SELECT * FROM WHERE userid="1 ...
- PRML读书会第一章 Introduction(机器学习基本概念、学习理论、模型选择、维灾等)
主讲人 常象宇 大家好,我是likrain,本来我和网神说的是我可以作为机动,大家不想讲哪里我可以试试,结果大家不想讲第一章.估计都是大神觉得第一章比较简单,所以就由我来吧.我的背景是统计与数学,稍懂 ...
- 大数据:从开源告诉你身边的IT故事
最近我们Team利用Dream分布式计算平台,做了这样一件事情,将Github的大量数据通过爬虫抓取下来,通过分析后,我们抽取最近一年中部分的开发者和项目信息,得到了如下有趣的信息,故分享之,数据原汁 ...