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个区间覆盖的前提下,重量最大 思路:感觉是很好想的费用流,把每个区间首尾相连,费用为该区间的重量的相反数(由 ...
随机推荐
- html中设置浏览器解码方式
通过添加一行标签: <meta http-equiv="Content-Type" content="text/html; charset=utf-8"& ...
- 7、scala面向对象编程之类
1. 定义一个简单的类 2.getter与setter 3.自定义getter与setter方法 4.仅暴露field的getter方法 5.private[this]的使用 6.Java风格的ge ...
- Zabbix 监控redis
Zabbix 监控redis 1.监控脚本,github上的 [root@localhost ~]# cat /etc/zabbix/script/redis-status.sh #!/bin/bas ...
- 太坑了,mybatis注解一对多,id没了
@Select("SELECT *, id nodes FROM QUESTION_PO WHERE ID=#{id}") @Results({ @Result(property ...
- Python 之web动态服务器
webServer.py代码如下: import socket import sys from multiprocessing import Process class WSGIServer(obje ...
- 解决Mysql Workbench的Error Code: 1175错误
错误: Error Code: 1175. You are using safe update mode and you tried to update a table without a WHERE ...
- cocos creator destroy方法
node.destroy(),Node.destroyAllChildren并不会立即销毁,实际销毁操作会延迟到当前帧渲染前执行. 这段话可能不明白,但是在Node.destroyAllChildre ...
- HDU 2451 Simple Addition Expression(找规律,考验智商)
题目 最近比赛的题目好多签到题都是找规律的考验智商的题目啊,,,我怎么越来越笨了,,,, 通过列举,可以发现规律: 从左往右按位扫这个数: 当数的长度大于1时: 当首位大于3时,答案就是4*4*4*… ...
- php第十七节课
分页查询 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3 ...
- const浅析
前言 c++中使用到const的地方有很多, 而且const 本身也针对不同的类型可能有不同的含义, 比如对指针就有顶层和底层. 本节就是探讨关于C++中const的在不同的地方不同表现或含义. co ...