poj 2351 Farm Tour (最小费用最大流)
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 17230 | Accepted: 6647 |
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
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<cstdlib>
#include<string.h>
#include<set>
#include<vector>
#include<queue>
#include<stack>
#include<map>
#include<cmath>
typedef long long ll;
typedef unsigned long long LL;
using namespace std;
const double PI=acos(-1.0);
const double eps=0.0000000001;
const int INF=0x3f3f3f3f;
const int N=+;
int head[N];
int dis[N];
int pre[N];
int vis[N];
int tot;
int m,n;
struct node{
int from,to,next,flow,cost;
}edge[N<<];
void init(){
memset(head,-,sizeof(head));
tot=;
}
void add(int u,int v,int c,int cost){
edge[tot].from=u;
edge[tot].to=v;
edge[tot].flow=c;
edge[tot].cost=cost;
edge[tot].next=head[u];
head[u]=tot++;
edge[tot].from=v;
edge[tot].to=u;
edge[tot].flow=;
edge[tot].cost=-cost;
edge[tot].next=head[v];
head[v]=tot++;
}
int spfa(int s,int t){
memset(pre,-,sizeof(pre));
memset(dis,INF,sizeof(dis));
memset(vis,,sizeof(vis));
queue<int>q;
dis[s]=;
vis[s]=;
q.push(s);
while(!q.empty()){
int x=q.front();
q.pop();
vis[x]=;
for(int i=head[x];i!=-;i=edge[i].next){
int v=edge[i].to;
if(edge[i].flow&&dis[v]>dis[x]+edge[i].cost){
dis[v]=edge[i].cost+dis[x];
pre[v]=i;
if(vis[v]==){
vis[v]=;
q.push(v);
} }
}
}
if(pre[t]==-)return ;
return ;
}
int MCMF(int s,int t){
int flow=;
int cost=;
while(spfa(s,t)){
int minn=INF;
//cout<<3<<endl;
for(int i=pre[t];i!=-;i=pre[edge[i].from]){
minn=min(minn,edge[i].flow);
}
for(int i=pre[t];i!=-;i=pre[edge[i].from]){
edge[i].flow=edge[i].flow-minn;
edge[i^].flow=edge[i^].flow+minn;
cost=edge[i].cost+cost;
}
flow=flow+minn;
if(flow==)return cost;
}
return cost;
}
int main(){
while(scanf("%d%d",&n,&m)!=EOF){
init();
add(,,,);
add(n,n+,,);
for(int i=;i<=m;i++){
int u,v,cost;
scanf("%d%d%d",&u,&v,&cost);
add(u,v,,cost);
add(v,u,,cost);
}
cout<<MCMF(,n+)<<endl;
}
}
poj 2351 Farm Tour (最小费用最大流)的更多相关文章
- poj 2135 Farm Tour 最小费用最大流建图跑最短路
题目链接 题意:无向图有N(N <= 1000)个节点,M(M <= 10000)条边:从节点1走到节点N再从N走回来,图中不能走同一条边,且图中可能出现重边,问最短距离之和为多少? 思路 ...
- POJ 2135 Farm Tour [最小费用最大流]
题意: 有n个点和m条边,让你从1出发到n再从n回到1,不要求所有点都要经过,但是每条边只能走一次.边是无向边. 问最短的行走距离多少. 一开始看这题还没搞费用流,后来搞了搞再回来看,想了想建图不是很 ...
- [poj] 1235 Farm Tour || 最小费用最大流
原题 费用流板子题. 费用流与最大流的区别就是把bfs改为spfa,dfs时把按deep搜索改成按最短路搜索即可 #include<cstdio> #include<queue> ...
- POJ2135 Farm Tour —— 最小费用最大流
题目链接:http://poj.org/problem?id=2135 Farm Tour Time Limit: 1000MS Memory Limit: 65536K Total Submis ...
- TZOJ 1513 Farm Tour(最小费用最大流)
描述 When FJ's friends visit him on the farm, he likes to show them around. His farm comprises N (1 &l ...
- Farm Tour(最小费用最大流模板)
Farm Tour Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 18150 Accepted: 7023 Descri ...
- POJ 2157 Evacuation Plan [最小费用最大流][消圈算法]
---恢复内容开始--- 题意略. 这题在poj直接求最小费用会超时,但是题意也没说要求最优解. 根据线圈定理,如果一个跑完最费用流的残余网络中存在负权环,那么顺着这个负权环跑流量为1那么会得到更小的 ...
- POJ 2195 - Going Home - [最小费用最大流][MCMF模板]
题目链接:http://poj.org/problem?id=2195 Time Limit: 1000MS Memory Limit: 65536K Description On a grid ma ...
- POJ 3680: Intervals【最小费用最大流】
题目大意:你有N个开区间,每个区间有个重量wi,你要选择一些区间,使得满足:每个点被不超过K个区间覆盖的前提下,重量最大 思路:感觉是很好想的费用流,把每个区间首尾相连,费用为该区间的重量的相反数(由 ...
随机推荐
- php中的抽象方法和抽象类
1.什么是抽象方法? 我们在类里面定义的没有方法提的方法就是抽象方法.所谓的没有方法体指的是,在声明的时候没有大括号以及其中的内容,而是直接在声明时在方法名后加上分号结束,另外在声明抽象方法时方 ...
- CentOS 7 使用 yum 安装 MariaDB 与 MariaDB 的简单配置
闲置已久的空间环境配置忘得差不多了,今天得空整理,重置了磁盘重新搭建环境,首先安装MariaDB的数据库,在这里记录下安装过程,以便以后查看. 1.安装MariaDB 安装命令 yum -y inst ...
- ubuntu下查看如何配置pycharm
ubuntu中PyCharm的安装与卸载 https://blog.csdn.net/weixin_31484477/article/details/81133590 pycharm ModuleNo ...
- GeckoWebBrowser设置cookie
var uri = new Uri("http://www.aa.com"); //often cookies are stored on domain level, so &qu ...
- SpringMVC参数绑定、Post乱码解决方法
从客户端请求key/value数据,经过参数绑定,将key/value数据绑定到controller方法的形参上. springmvc中,接收页面提交的数据是通过方法形参来接收.而不是在control ...
- 2018年为什么要学习Linux?Linux运维的前景还好吗?
Linux一直是很多人入行IT的首选,无论是从入行难度还是职业寿命来说,Linux运维都比开发有着更大的优势.为了得到高薪工作,很多人在Linux学习过程中付出了非常大的努力,最终也得到了不错的收获. ...
- 27.7 并行语言集成查询(PLinq)
static void Main() { ObsoleteMethods(Assembly.Load("mscorlib.dll")); Console.ReadKey(); } ...
- 内核调试-perf introduction
perf概念 perf_event Perf_events是目前在Linux上使用广泛的profiling/tracing工具,除了本身是内核(kernel)的组成部分以外,还提供了用户空间(user ...
- P2884 [USACO07MAR]每月的费用Monthly Expense
题目描述 Farmer John is an astounding accounting wizard and has realized he might run out of money to ru ...
- MySQL之中文乱码问题
创建 my.ini 文件,在该文件中添加以下内容,放在安装好的mysql根路径下: [client] default-character-set=utf8 [mysql] # 设置mysql客户端默认 ...