hdu 6214 : Smallest Minimum Cut 【网络流】
ISAP写法
#include <bits/stdc++.h>
using namespace std;
typedef long long LL; namespace FastIO
{
const static int MX=1e6;
bool IOerror=;
char nc()
{
static char buf[MX],*p1=buf+MX,*pend=buf+MX;
if(p1==pend)
{
p1=buf;
pend=buf+fread(buf,,MX,stdin);
if(pend==p1)
{
IOerror=;
return -;
}
}
return *p1++;
}
inline bool blank(char ch)
{
return ch==' '||ch=='\n'||ch=='\r'||ch=='\t';
}
inline int read(int& x)
{
char ch;
while(blank(ch=nc()));
if(IOerror) return ;
for(x=ch-''; (ch=nc())>=''&&ch<=''; x=x*+ch-''); //printf("%d===\n",x);
return ;
}
inline int read(LL& x)
{
char ch;
while(blank(ch=nc()));
if(IOerror) return ;
for(x=ch-''; (ch=nc())>=''&&ch<=''; x=x*+ch-''); //printf("%d===\n",x);
return ;
}
}
using namespace FastIO; const int MAXN=,MAXM=,MO=;
const int INF = 0x3f3f3f3f;
struct Edge
{
int to,next,cap,flow;
Edge() {}
Edge(int _to,int _next,int _cap,int _flow)
{
to=_to,next=_next,cap=_cap,flow=_flow;
}
} edge[MAXM]; //注意是MAXM
int tol;
int head[MAXN];
int gap[MAXN],d[MAXN],pre[MAXN],cur[MAXN];
void init()
{
tol = ;
memset(head,-,sizeof(head));
}
void addedge(int u,int v,int w)
{
edge[tol]=Edge(v,head[u],w,);
head[u]=tol++;
edge[tol]=Edge(u,head[v],,);
head[v]=tol++;
}
int isap(int start,int end,int N)
{
memset(gap,,sizeof(gap));
memset(d,,sizeof(d));
memcpy(cur,head,sizeof(head));
int u = start;
pre[u] = -;
gap[] = N;
int ans = ;
while(d[start] < N)
{
if(u == end)
{
int Min = INF;
for(int i = pre[u]; i != -; i = pre[edge[i^].to])
if(Min > edge[i].cap - edge[i].flow)
Min = edge[i].cap - edge[i].flow;
for(int i = pre[u]; i != -; i = pre[edge[i^].to])
{
edge[i].flow += Min;
edge[i^].flow -= Min;
}
u = start;
ans += Min;
continue;
}
bool flag = false;
int v;
for(int i = cur[u]; i != -; i = edge[i].next)
{
v = edge[i].to;
if(edge[i].cap - edge[i].flow && d[v]+ == d[u])
{
flag = true;
cur[u] = pre[v] = i;
break;
}
}
if(flag)
{
u = v;
continue;
}
int Min = N;
for(int i = head[u]; i != -; i = edge[i].next)
if(edge[i].cap - edge[i].flow && d[edge[i].to] < Min)
{
Min = d[edge[i].to];
cur[u] = i;
}
gap[d[u]]--;
if(!gap[d[u]])return ans;
d[u] = Min+;
gap[d[u]]++;
if(u != start) u = edge[pre[u]^].to;
}
return ans;
} int main()
{
int T;
read(T);
while (T--)
{
int n,m,s,t;
read(n),read(m);
read(s),read(t);
init();
for(int i=; i<=m; i++)
{
int x,y,z;
read(x),read(y),read(z);
addedge(x,y,z*MO+);
}
int ans=isap(s,t,n);
printf("%d\n",ans%MO);
}
}
Dinic写法
#include <bits/stdc++.h>
using namespace std;
#define INF 0x3f3f3f3f
#define LC(x) (x<<1)
#define RC(x) ((x<<1)+1)
#define MID(x,y) ((x+y)>>1)
#define CLR(arr,val) memset(arr,val,sizeof(arr))
#define FAST_IO ios::sync_with_stdio(false);cin.tie(0);
typedef pair<int,int> pii;
//typedef long long LL; namespace FastIO
{
const static int MX=1e6;
bool IOerror=;
char nc()
{
static char buf[MX],*p1=buf+MX,*pend=buf+MX;
if(p1==pend)
{
p1=buf;
pend=buf+fread(buf,,MX,stdin);
if(pend==p1)
{
IOerror=;
return -;
}
}
return *p1++;
}
inline bool blank(char ch)
{
return ch==' '||ch=='\n'||ch=='\r'||ch=='\t';
}
inline int read(int& x)
{
char ch;
while(blank(ch=nc()));
if(IOerror) return ;
for(x=ch-''; (ch=nc())>=''&&ch<=''; x=x*+ch-''); //printf("%d===\n",x);
return ;
}
}
using namespace FastIO; const int N=;
const int M=;
struct edge
{
int to,nxt;
int cap;
edge() {}
edge(int _to,int _nxt,int _cap): to(_to),nxt(_nxt),cap(_cap) {}
};
edge E[M << ];
int head[N],tot,d[N]; void init()
{
CLR(head,-);
tot=;
}
inline void add(int s,int t,int cap)
{
E[tot]=edge(t,head[s],cap);
head[s]=tot++;
E[tot]=edge(s,head[t],);
head[t]=tot++;
}
int bfs(int s,int t)
{
CLR(d,-);
d[s]=;
queue<int>Q;
Q.push(s);
while(!Q.empty())
{
int u=Q.front();
Q.pop();
for(int i=head[u]; ~i; i=E[i].nxt)
{
int v=E[i].to;
if(d[v]==- && E[i].cap > )
{
d[v]=d[u] + ;
if(v==t) return ;
Q.push(v);
}
}
}
return ~d[t];
}
int dfs(int s,int t,int f)
{
if(s==t || !f)
return f;
int ret=;
for(int i=head[s]; ~i; i=E[i].nxt)
{
int v=E[i].to;
if(d[v]==d[s] + && E[i].cap > )
{
int df=dfs(v,t,min(f,E[i].cap));
if(df>)
{
E[i].cap -=df;
E[i ^ ].cap +=df;
ret +=df;
f -=df;
if(!f) break;
}
}
}
if(!ret) d[s]=-;
return ret;
}
int dinic(int s,int t)
{
int ret=;
while (bfs(s,t))
ret+=dfs(s,t,0x3f3f3f3f);
return ret;
}
int main()
{
int T;read(T);
while(T--)
{
init();
int n,m,s,t;
read(n),read(m);
read(s),read(t);
while(m--)
{
int u,v,c;
read(u),read(v),read(c);
add(u,v,c*+);
}
printf("%d\n",dinic(s,t)%);
}
}
=========================================================================================
9.22更新
根据刘汝佳的板子,自己改编一下后如下(希望以后可以一直用
#include <bits/stdc++.h>
using namespace std;
typedef long long LL; namespace FastIO
{
const static int MX=1e6;
bool IOerror=;
char nc()
{
static char buf[MX],*p1=buf+MX,*pend=buf+MX;
if(p1==pend)
{
p1=buf;
pend=buf+fread(buf,,MX,stdin);
if(pend==p1)
{
IOerror=;
return -;
}
}
return *p1++;
}
inline bool blank(char ch)
{
return ch==' '||ch=='\n'||ch=='\r'||ch=='\t';
}
inline int read(int& x)
{
char ch;
while(blank(ch=nc()));
if(IOerror) return ;
for(x=ch-''; (ch=nc())>=''&&ch<=''; x=x*+ch-''); //printf("%d===\n",x);
return ;
}
}
using namespace FastIO; const int N=;
const int M=;
const int INF=0x3f3f3f3f; struct Edge
{
int to,next;
int flow,cap; //根据情况设定变量类型
Edge(){}
Edge(int _to,int _next,int _cap,int _flow)
{
to=_to,next=_next,cap=_cap,flow=_flow;
}
};
Edge edge[M<<];
int head[N],tot;
int cur[N];
int d[N]; void init()
{
memset(head,-,sizeof(head));
tot=;
}
inline void addedge(int u,int v,int cap)
{
edge[tot]=Edge(v,head[u],cap,);
head[u]=tot++;
edge[tot]=Edge(u,head[v],,);
head[v]=tot++;
}
int bfs(int s,int t)
{
memset(d,-,sizeof(d));
queue<int> Q;
Q.push(s),d[s]=;
while(!Q.empty())
{
int u=Q.front();Q.pop();
for(int i=head[u];~i;i=edge[i].next)
{
int v=edge[i].to;
if(d[v]==-&&edge[i].cap>edge[i].flow)
{
d[v]=d[u]+;
if(v==t) return ;
Q.push(v);
}
}
}
return ~d[t];
}
int dfs(int s,int t,int a)
{
if(s==t||a==) return a;
int flow=,df;
// for(int& i=cur[s];~i;i=edge[i].next)
for(int i=head[s];~i;i=edge[i].next)
{
int v=edge[i].to;
// if(d[v]==d[s]+1 &&
// (df=dfs(v,t,min(a,edge[i].cap-edge[i].flow))>0)) //这种写法 hdu6214 TLE
if(d[v]==d[s]+&&edge[i].cap>edge[i].flow
&&(df=dfs(v,t,min(a,edge[i].cap-edge[i].flow)))>)
{
edge[i].flow+=df;
edge[i^].flow-=df;
flow+=df;
a-=df;
if(a==) break;
}
}
if(flow==) d[s]=-;
return flow;
}
int dinic(int s,int t)
{
int ret=;
while(bfs(s,t))
{
// memcpy(cur,head,sizeof(cur));
ret+=dfs(s,t,INF);
}
return ret;
} int main()
{
int T;read(T);
while(T--)
{
init();
int n,m,s,t;
read(n),read(m);
read(s),read(t);
while(m--)
{
int u,v,c;
read(u),read(v),read(c);
addedge(u,v,c*+);
}
printf("%d\n",dinic(s,t)%);
}
}
hdu 6214 : Smallest Minimum Cut 【网络流】的更多相关文章
- hdu 6214 Smallest Minimum Cut[最大流]
hdu 6214 Smallest Minimum Cut[最大流] 题意:求最小割中最少的边数. 题解:对边权乘个比边大点的数比如300,再加1 ,最后,最大流对300取余就是边数啦.. #incl ...
- HDU 6214.Smallest Minimum Cut 最少边数最小割
Smallest Minimum Cut Time Limit: 2000/2000 MS (Java/Others) Memory Limit: 65535/32768 K (Java/Oth ...
- HDU 6214 Smallest Minimum Cut 【网络流最小割+ 二种方法只能一种有效+hdu 3987原题】
Problem Description Consider a network G=(V,E) with source s and sink t . An s-t cut is a partition ...
- HDU 6214 Smallest Minimum Cut(最少边最小割)
Problem Description Consider a network G=(V,E) with source s and sink t. An s-t cut is a partition o ...
- HDU 6214 Smallest Minimum Cut 最小割,权值编码
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6214 题意:求边数最小的割. 解法: 建边的时候每条边权 w = w * (E + 1) + 1; 这 ...
- HDU 6214 Smallest Minimum Cut (最小割且边数最少)
题意:给定上一个有向图,求 s - t 的最小割且边数最少. 析:设边的容量是w,边数为m,只要把每边打容量变成 w * (m+1) + 1,然后跑一个最大流,最大流%(m+1),就是答案. 代码如下 ...
- hdu 6214 Smallest Minimum Cut(最小割的最少边数)
题目大意是给一张网络,网络可能存在不同边集的最小割,求出拥有最少边集的最小割,最少的边是多少条? 思路:题目很好理解,就是找一个边集最少的最小割,一个方法是在建图的时候把边的容量处理成C *(E+1 ...
- Smallest Minimum Cut HDU - 6214(最小割集)
Smallest Minimum Cut Time Limit: 2000/2000 MS (Java/Others) Memory Limit: 65535/32768 K (Java/Oth ...
- HDU - 6214:Smallest Minimum Cut(最小割边最小割)
Consider a network G=(V,E) G=(V,E) with source s s and sink t t . An s-t cut is a partition of nodes ...
随机推荐
- Delphi 快速检测是否联网 判断网线是否拔开。 但是不能判断是否能上网
https://blog.csdn.net/chelen_jak/article/details/50204145 Delphi 快速检测是否联网 2015年12月07日 12:01:26 chele ...
- delphi assigned函数的用法
if not Assigned(Modeless) then Assigned()什么意思! assigned 是用来判断某一指针(pointer)或过程引用是否为nil(空),如果为空则返回假(fa ...
- 【ABAP系列】SAP 如何用ABAP实现自动发送外部邮件
公众号:SAP Technical 本文作者:matinal 原文出处:http://www.cnblogs.com/SAPmatinal/ 原文链接:[ABAP系列]SAP 如何用ABAP实现自动发 ...
- if you wanna the rainbow, you have to deal with the rain.
bulk. n. 大量 reluctant. adj. 不情愿的 terrorist. n. 恐怖分子 recognition. n. 认出 tout.v. 兜售 conceal.v. 隐藏 dras ...
- 【Linux 环境搭建】Ubuntu下安装tftp
1.安装软件 sudo apt-get install tftp-hpa tftpd-hap xinetd 2.修改配置文件 sudo vim /etc/default/tftpd-hpa ...
- idea 代码部分格式化
效果: 处理Idea使用ctrl+alt+L进行代码格式化时部分代码可以被忽略,不执行格式化功能(webstorm,phpstorm同理) 原因: 有时希望自己写的一些代码不被格式化,或者发现格式化后 ...
- c语言中字符串跨行书写的问题
字符串常量定义时的换行问题 如果我们在一行代码的行尾放置一个反斜杠,c语言编译器会忽略行尾的换行符,而把下一行的内容也算作是本行的内容.这里反斜杠起到了续行的作用. 如果我们不使 ...
- js中箭头函数 及 针对箭头函数this指向问题引出的单体模式
ES6允许使用“箭头”(=>)定义函数 var f = a = > a //等同于 var f = function(a){ return a; } 如果箭头函数不需要参数或需要多个参数, ...
- 自动清理ES索引脚本
#/bin/bash #指定日期(3个月前) DATA=`date -d "3 month ago" +%Y.%m.%d` #当前日期 time=`date` #删除3个月前的日志 ...
- JS调用PageMethods
http://www.cnblogs.com/Ren_Lei/archive/2010/07/14/1777413.html JS调用PageMethods 操作步骤: 1.新建一个WebApplic ...