算法复习——费用流模板(poj2135)
题目:
| Time Limit: 1000MS | Memory Limit: 65536K | |
| Total Submissions: 16898 | Accepted: 6543 |
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
方法:
题目相当于是从 1 到 N 找两条不相交的路径并使得总长度最小。由于每条边不能重复经过,所以我们将每条边容量视为 1,费用为边的长度,新建源点 s 向 1 连一条容量 2 费用0 的边,新建汇点 t,N 向 t 连一条容量为 2 费用为 0 的边,则题目就转化为求从 s 到 t 的最小费用最大流问题。
代码:
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<string>
#include<cmath>
#include<ctime>
#include<cctype>
#include<algorithm>
#include<queue>
using namespace std;
const int N=;
const int M=;
const int inf=0x3f3f3f3f;
queue<int> que;
int n,m,ans=;
int first[],next[],go[],rest[],cost[],dis[],tot=;
bool visit[],work[];
int src,des;
void combin(int u,int v,int r,int w)
{
next[++tot]=first[u],first[u]=tot,go[tot]=v,rest[tot]=r,cost[tot]=w;
next[++tot]=first[v],first[v]=tot,go[tot]=u,rest[tot]=,cost[tot]=-w;
}
void init(int n,int m)
{
src=,des=n+;
for(int i=;i<=m;i++)
{
int u,v,w;
scanf("%d%d%d",&u,&v,&w);
combin(u,v,,w);
combin(v,u,,w);
}
combin(src,,,);
combin(n,des,,);
}
bool spfa()
{
memset(dis,inf,sizeof(dis));
memset(work,false,sizeof(work));
int u;
que.push(src),dis[src]=;
while(!que.empty())
{
u=que.front(),que.pop();
visit[u]=false;
for(int e=first[u];e;e=next[e])
{
int v=go[e];
if(rest[e]&&dis[u]+cost[e]<dis[v])
{
dis[v]=dis[u]+cost[e];
if(!visit[v])
{
que.push(v);
visit[v]=true;
}
}
}
}
return dis[des]<inf;
}
int dinic(int u,int flow)
{
if(u==des)
{
ans+=flow*dis[des];
return flow;
}
work[u]=true;
int res=,temp,v,e;
for(e=first[u];e;e=next[e])
{
if(!work[v=go[e]]&&rest[e]&&dis[v]==dis[u]+cost[e])
{
temp=dinic(v,min(rest[e],flow-res));
if(temp)
{
rest[e]-=temp,rest[e^]+=temp;
res+=temp;
if(res==flow) break;
}
}
}
return res;
}
int maxflow()
{
while(spfa()) dinic(src,inf);
return ans;
}
int main()
{
//freopen("a.in","r",stdin);
scanf("%d%d",&n,&m);
init(n,m);
cout<<maxflow()<<endl;
return ;
}
算法复习——费用流模板(poj2135)的更多相关文章
- 二分图带权匹配 KM算法与费用流模型建立
[二分图带权匹配与最佳匹配] 什么是二分图的带权匹配?二分图的带权匹配就是求出一个匹配集合,使得集合中边的权值之和最大或最小.而二分图的最佳匹配则一定为完备匹配,在此基础上,才要求匹配的边权值之和最大 ...
- HDU2686 费用流 模板
Matrix Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Subm ...
- HDU 6611 K Subsequence(Dijkstra优化费用流 模板)题解
题意: 有\(n\)个数\(a_1\cdots a_n\),现要你给出\(k\)个不相交的非降子序列,使得和最大. 思路: 费用流建图,每个点拆点,费用为\(-a[i]\),然后和源点连边,和后面非降 ...
- 初识费用流 模板(spfa+slf优化) 餐巾计划问题
今天学习了最小费用最大流,是网络流算法之一.可以对于一个每条边有一个容量和一个费用(即每单位流的消耗)的图指定一个源点和汇点,求在从源点到汇点的流量最大的前提下的最小费用. 这里讲一种最基础也是最好掌 ...
- hdu1533 费用流模板
Going Home Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total ...
- BZOJ3291Alice与能源计划——匈牙利算法+模拟费用流
题目描述 在梦境中,Alice来到了火星.不知为何,转眼间Alice被任命为火星能源部长,并立刻面临着一个严峻的考验.为 了方便,我们可以将火星抽象成平面,并建立平面直角坐标系.火星上一共有N个居民点 ...
- 费用流模板(带权二分图匹配)——hdu1533
/* 带权二分图匹配 用费用流求,增加源点s 和 汇点t */ #include<bits/stdc++.h> using namespace std; #define maxn 1000 ...
- [BZOJ1937][SHOI2004]Mst最小生成树(KM算法,最大费用流)
1937: [Shoi2004]Mst 最小生成树 Time Limit: 3 Sec Memory Limit: 64 MBSubmit: 802 Solved: 344[Submit][Sta ...
- BZOJ2557[Poi2011]Programming Contest——匈牙利算法+模拟费用流
题目描述 Bartie and his friends compete in the Team Programming Contest. There are n contestants on each ...
随机推荐
- jmeter中通过jdbc方式连接mysql数据库的配置参考
jmeter中通过jdbc方式连接mysql数据库的配置参考: Database URL=jdbc:mysql://ip:port/dbname?useUnicode=true&allowMu ...
- 洛谷 P1276 校门外的树(增强版)
题目描述 校门外马路上本来从编号0到L,每一编号的位置都有1棵树.有砍树者每次从编号A到B处连续砍掉每1棵树,就连树苗也不放过(记 0 A B ,含A和B):幸运的是还有植树者每次从编号C到D 中凡是 ...
- (1)Ngixn 编译安装 (版本:1.12.1)
1.创建用户和群组 groupadd nginx 创建一个用户,不允许登陆和不创主目录 useradd -s /sbin/nologin -g nginx -M ngi ...
- FTP的环境搭建和防火墙设置
步骤: 1.右键点击无线网--->打开网络和共享中心--->控制面板--->程序--->启用或关闭Wondows功能
- js设置元素float的问题
用js设置一个元素的float样式 div.style.float = 'left'; 这句话在谷歌浏览器或许没问题,但是在IE,火狐下会无效 正确写法是 div.style.styleFloat = ...
- Asp.Net Core 入门(五)—— 布局视图_Layout.cshtml
布局视图和我们在Asp.Net MVC一样,布局视图_Layout.cshtml使得所有视图保持一致的外观变得更加容易,因为我们只有一个要修改的布局视图文件,更改后将立即反映在整个应用程序的所有视图中 ...
- Bootstrap历练实例:带有下拉菜单的标签和胶囊导航
<!DOCTYPE html><html><head><meta http-equiv="Content-Type" content=&q ...
- bootstrap 按钮组的嵌套
您可以在一个按钮组内嵌套另一个按钮组,即,在一个 .btn-group 内嵌套另一个 .btn-group .当您想让下拉菜单与一系列按钮组合使用时,就会用到这个. 实例: <!DOCTYPE ...
- Bootstrap历练实例:表单控件大小
表单控件大小 您可以分别使用 class .input-lg 和 .col-lg-* 来设置表单的高度和宽度. 实例: <!DOCTYPE html><html><hea ...
- javase(6)_异常
一.异常的概念 1.java异常是Java提供的用于处理程序中错误的一种机制. 2.所谓错误是程序在运行过程中发生的一些异常事件(如:除0,数组下标越界,文件不存在等). 3.Java程序的执行过程中 ...