poj 3204(最小割)
题目链接:http://poj.org/problem?id=3204
思路:显然只有增大那最小割边集上的边才能增加最大流,因此,我们可以先跑一遍最大流,然后对于那些满足条件的边u->v,当且仅当从源点开始沿着正向边能遍历到u,从汇点开始沿着正向边能遍历到v.
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<queue>
using namespace std;
#define MAXN 555
#define MAXM 55555
#define inf 1<<30 struct Edge{
int v,cap,next;
}edge[MAXM]; int n,m,NE,NV,vs,vt;
int head[MAXN]; void Insert(int u,int v,int cap)
{
edge[NE].v=v;
edge[NE].cap=cap;
edge[NE].next=head[u];
head[u]=NE++; edge[NE].v=u;
edge[NE].cap=;
edge[NE].next=head[v];
head[v]=NE++;
} int level[MAXN],gap[MAXN];
void bfs(int vt)
{
memset(level,-,sizeof(level));
memset(gap,,sizeof(gap));
level[vt]=;
gap[level[vt]]++;
queue<int>que;
que.push(vt);
while(!que.empty()){
int u=que.front();
que.pop();
for(int i=head[u];i!=-;i=edge[i].next){
int v=edge[i].v;
if(level[v]<){
level[v]=level[u]+;
gap[level[v]]++;
que.push(v);
}
}
}
} int pre[MAXN],cur[MAXN];
int SAP(int vs,int vt)
{
bfs(vt);
memset(pre,-,sizeof(pre));
memcpy(cur,head,sizeof(head));
int maxflow=;
int u=pre[vs]=vs,aug=inf;
gap[]=NV;
while(level[vs]<NV){
bool flag=false;
for(int &i=cur[u];i!=-;i=edge[i].next){
int v=edge[i].v;
if(edge[i].cap>&&level[u]==level[v]+){
flag=true;
pre[v]=u;
u=v;
aug=min(aug,edge[i].cap);
if(v==vt){
maxflow+=aug;
for(u=pre[v];v!=vs;v=u,u=pre[u]){
edge[cur[u]].cap-=aug;
edge[cur[u]^].cap+=aug;
}
aug=inf;
}
break;
}
}
if(flag)continue;
int minlevel=NV;
for(int i=head[u];i!=-;i=edge[i].next){
int v=edge[i].v;
if(edge[i].cap>&&level[v]<minlevel){
minlevel=level[v];
cur[u]=i;
}
}
if(--gap[level[u]]==)break;
level[u]=minlevel+;
gap[level[u]]++;
u=pre[u];
}
return maxflow;
} int vis[MAXN];
void dfs1(int u)
{
vis[u]=;
for(int i=head[u];i!=-;i=edge[i].next){
int v=edge[i].v,cap=edge[i].cap;
if(cap>&&!vis[v])dfs1(v);
}
} void dfs2(int u)
{
vis[u]=;
for(int i=head[u];i!=-;i=edge[i].next){
int v=edge[i].v,cap=edge[i^].cap;//注意是正向边容量
if(cap>&&!vis[v])dfs2(v);
}
} int main()
{
int u,v,w,ans;
while(~scanf("%d%d",&n,&m)){
NE=;
memset(head,-,sizeof(head));
vs=,vt=n-,NV=n;
for(int i=;i<=m;i++){
scanf("%d%d%d",&u,&v,&w);
Insert(u,v,w);
}
SAP(vs,vt);
ans=;
memset(vis,,sizeof(vis));
dfs1();
dfs2(n-);
for(int i=;i<NE;i+=){
if(vis[edge[i].v]==&&vis[edge[i^].v]==)ans++;
}
printf("%d\n",ans);
}
return ;
}
poj 3204(最小割)的更多相关文章
- poj 3204(最小割--关键割边)
Ikki's Story I - Road Reconstruction Time Limit: 2000MS Memory Limit: 131072K Total Submissions: 7 ...
- poj 2125(最小割)
题目链接:http://poj.org/problem?id=2125 思路:将最小点权覆盖转化为最小割模型,于是拆点建图,将点i拆成i,i+n,其中vs与i相连,边容量为w[i]-,i+n与vt相连 ...
- POJ 3469 最小割 Dual Core CPU
题意: 一个双核CPU上运行N个模块,每个模块在两个核上运行的费用分别为Ai和Bi. 同时,有M对模块需要进行数据交换,如果这两个模块不在同一个核上运行需要额外花费. 求运行N个模块的最小费用. 分析 ...
- 网络流 poj 3308 最小割
t个样例 n*m的矩阵 L个伞兵 给出每行每列装激光的花费 伞兵的位置 要求杀死所有伞兵 总费用为这些费用的乘积 求花费最小 建图 源点 -> 行 -> 列 -> 汇点 ...
- poj 3084 最小割
题目链接:http://poj.org/problem?id=3084 本题主要在构图上,我采用的是把要保护的房间与源点相连,有intruder的与汇点相连,相对麻烦. #include <cs ...
- poj 3469 最小割模板sap+gap+弧优化
/*以核心1为源点,以核心2为汇点建图,跑一遍最大流*/ #include<stdio.h> #include<string.h> #include<queue> ...
- POJ 3308 Paratroopers(最小割EK(邻接表&矩阵))
Description It is year 2500 A.D. and there is a terrible war between the forces of the Earth and the ...
- poj 1815 Friendship 字典序最小+最小割
题目链接:http://poj.org/problem?id=1815 In modern society, each person has his own friends. Since all th ...
- 【网络流#8】POJ 3469 Dual Core CPU 最小割【ISAP模板】 - 《挑战程序设计竞赛》例题
[题意]有n个程序,分别在两个内核中运行,程序i在内核A上运行代价为ai,在内核B上运行的代价为bi,现在有程序间数据交换,如果两个程序在同一核上运行,则不产生额外代价,在不同核上运行则产生Cij的额 ...
- POJ 1815 Friendship (Dinic 最小割)
Friendship Time Limit: 2000MS Memory Limit: 20000K Total Submissions: 8025 Accepted: 2224 Descri ...
随机推荐
- 算法笔记_080:蓝桥杯练习 队列操作(Java)
目录 1 问题描述 2 解决方案 1 问题描述 问题描述 队列操作题.根据输入的操作命令,操作队列(1)入队.(2)出队并输出.(3)计算队中元素个数并输出. 输入格式 第一行一个数字N. 下面N行, ...
- ionic 签名、打包
ionic cordova platform add androidionic cordova build android [debug版本,无需签名] ionic cordova build and ...
- vue 不能检测数组长度 值变化原因解析
1.vue不能检测数组长度或者值的变化 (1)数组长度变化 未检测到 <!DOCTYPE html> <html lang="en"> <head&g ...
- [Exception Android 20] - Error:Execution failed for task ':app:processDebugResources'
Error:Execution failed for task ':app:processDebugResources'. > com.android.ide.common.process.Pr ...
- 南阳OJ 背包问题
/*背包问题 时间限制:3000 ms | 内存限制:65535 KB 难度:3 描写叙述 如今有非常多物品(它们是能够切割的),我们知道它们每一个物品的单位重量的价值v和 重量w(1< ...
- 为 sublime text3 添加 github 上的插件
1.CMD+SHIFT+P ---> ADD REPOSITORY 输入 github 上的地址 https://github.com/akira-cn/sublime-gbk 2.CMD+SH ...
- MySQL数据库的查询缓冲机制
MySQL数据库的查询缓冲机制 2011-08-10 11:07 佚名 火魔网 字号:T | T 使用查询缓冲机制,可以极大地提高MySQL数据库查询的效率,节省查询所用的时间.那么查询缓冲机制是怎样 ...
- C++14系列(1):Linux下C++14开发环境配置
g++安装 參考地址: http://sysads.co.uk/2014/07/install-gcc-gnu-4-9-1-on-ubuntu-14-04/ 当前Ubuntu的LTS版本号为14.04 ...
- oracle 事务多表查询以及额外的用处
/* 以下代码是对emp表进行显示宽度设置*/ col empno for 9999;col ename for a10;col job for a10;col mgr for 9999;col hi ...
- EMQ配置“ClientId 认证”
EMQ配置“ClientId 认证” 基于 MQTT 客户端 ID 认证 clientId配置文件地址 /etc/emqttd/plugins/emq_auth_clientid.conf 内容如下: ...