[poj] 1235 Farm Tour || 最小费用最大流
原题
费用流板子题。
费用流与最大流的区别就是把bfs改为spfa,dfs时把按deep搜索改成按最短路搜索即可
#include<cstdio>
#include<queue>
#include<cstring>
#define N 20020
using namespace std;
int n,m,src, des, head[N],dis[N],cur[N],ans,cnt=2,s,t, ANS;
queue <int> q;
bool vis[N];
struct hhh
{
int to,next,w,cost;
}edge[500005];
int read()
{
int ans=0,fu=1;
char j=getchar();
for (;(j<'0' || j>'9') && j!='-';j=getchar()) ;
if (j=='-') fu=-1,j=getchar();
for (;j>='0' && j<='9';j=getchar()) ans*=10,ans+=j-'0';
return ans*fu;
}
void add(int u,int v,int w,int c)
{
edge[cnt].to=v;
edge[cnt].w=w;
edge[cnt].next=head[u];
edge[cnt].cost=c;
head[u]=cnt++;
}
void addEdge(int u,int v,int w,int c)
{
add(u,v,w,c);
add(v,u,0,-c);
}
bool bfs()
{
for (int i=s;i<=t;i++)
vis[i]=0,cur[i]=head[i],dis[i]=0x3f3f3f3f;
q.push(s);
dis[s]=0;
vis[s]=1;
while(!q.empty())
{
int r=q.front();
q.pop();
vis[r]=0;
for (int i=head[r],v;i;i=edge[i].next)
{
v=edge[i].to;
if (edge[i].w>0 && dis[r]+edge[i].cost<dis[v])
{
dis[v]=dis[r]+edge[i].cost;
if (!vis[v])
{
vis[v]=1;
q.push(v);
}
}
}
}
return dis[t]!=0x3f3f3f3f;
}
int dfs(int x,int f)
{
if (x==t) return ANS+=f*dis[t],f;
int ha=0,now;
vis[x]=1;
for (int &i=cur[x],v;i;i=edge[i].next)
{
v=edge[i].to;
if (vis[v]) continue;
if (edge[i].w>0 && dis[v]==dis[x]+edge[i].cost)
{
now=dfs(v,min(f-ha,edge[i].w));
if (now)
{
ha+=now;
edge[i].w-=now;
edge[i^1].w+=now;
}
}
if (ha==f) return ha;
}
return ha;
}
int main()
{
scanf("%d%d",&n,&m);
s=0;
t=n+1;
for (int i=1,a,b,d;i<=m;i++)
{
a=read();
b=read();
d=read();
addEdge(a,b,1,d);
addEdge(b,a,1,d);
}
addEdge(s,1,2,0);
addEdge(n,t,2,0);
while (bfs()) ans+=dfs(s,0x3f3f3f3f);
printf("%d\n",ANS);
return 0;
}
[poj] 1235 Farm Tour || 最小费用最大流的更多相关文章
- poj 2351 Farm Tour (最小费用最大流)
Farm Tour Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 17230 Accepted: 6647 Descri ...
- poj 2135 Farm Tour 最小费用最大流建图跑最短路
题目链接 题意:无向图有N(N <= 1000)个节点,M(M <= 10000)条边:从节点1走到节点N再从N走回来,图中不能走同一条边,且图中可能出现重边,问最短距离之和为多少? 思路 ...
- POJ 2135 Farm Tour [最小费用最大流]
题意: 有n个点和m条边,让你从1出发到n再从n回到1,不要求所有点都要经过,但是每条边只能走一次.边是无向边. 问最短的行走距离多少. 一开始看这题还没搞费用流,后来搞了搞再回来看,想了想建图不是很 ...
- 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个区间覆盖的前提下,重量最大 思路:感觉是很好想的费用流,把每个区间首尾相连,费用为该区间的重量的相反数(由 ...
随机推荐
- 关于var和ES6中的let,const的理解
var的作用就不多说了,下面说说var的缺点: 1.var可以重复声明 var a = 1; var a = 5; console.log(a); //5 不会报错 在像这些这些严谨的语言来说,一般是 ...
- 时间戳与QDateTime相互转换
最近项目中需要将日期时间输出到Excel中,程序使用Qt开发,使用第三方库QtXlsx进行Excel读写操作.Excel中第一列为时间,时间间隔为1小时,如图所示. 赋值起始时间stDTime,则后续 ...
- MySQL时间戳、时间
MySQL中: now():获取当前时间:例:SELECT now(); // 结果:2018-07-03 13:40:14 unix_timestamp():将时间转换为时间戳:例: SELECT ...
- Linux apt & yum 及 常用命令
yum yum 语法 yum [options] [command] [package ...] options:可选,选项包括-h(帮助),-y(当安装过程提示选择全部为"yes" ...
- 初试PHP连接sql server
最开始想使用 pdo_sqlsrv 拓展,但是一直没成功,本文采用的是 pdo_dblib + freetds. 环境:CentOS 6.8.PHP 5.6.20 freetds wget ftp:/ ...
- php扩展开发-常量
//常量在内核中的结构 typedef struct _zend_constant { zval value; int flags; char *name; uint name_len; int mo ...
- MyFirstDay_答案_1.**猫(自己整理)
1>***猫: python基础类: 字符串反转的常用处理方式: # 方法一:使用字符串切片 s = "hello python" result = s[::-1] prin ...
- python3 练习题100例 (二十)
#!/usr/bin/env python3# -*- coding: utf-8 -*-"""练习二十:判断一个年份是否是闰年公历闰年计算方法:1.普通年能被4整除且不 ...
- Pythond函数的参数使用操作注意事项
定义函数的时候,我们把参数的名字和位置确定下来,函数的接口定义就完成了.对于函数的调用者来说,只需要知道如何传递正确的参数,以及函数将返回什么样的值就够了,函数内部的复杂逻辑被封装起来,调用者无需了解 ...
- Leetcode 653. 两数之和 IV - 输入 BST
题目链接 https://leetcode.com/problems/two-sum-iv-input-is-a-bst/description/ 题目描述 给定一个二叉搜索树和一个目标结果,如果 B ...