【洛谷1345】 [USACO5.4]奶牛的电信(最小割)
2024-09-05 08:18:59
传送门
Solution
emmm,直接对于每一个点拆点就好了。
然后边连Inf,点连1,跑最小割就是答案。
代码实现
#include<bits/stdc++.h>
using namespace std;
const int N=100010,Inf=1e9+10;
int front[N],cnt,s,t,n;
struct node
{
int to,nxt,w;
}e[500010];
queue<int>Q;
int dep[N];
void Add(int u,int v,int w)
{
e[cnt]=(node){v,front[u],w};front[u]=cnt++;
e[cnt]=(node){u,front[v],0};front[v]=cnt++;
}
bool bfs()
{
memset(dep,0,sizeof(dep));
Q.push(s);dep[s]=1;
while(!Q.empty())
{
int u=Q.front();Q.pop();
for(int i=front[u];i!=-1;i=e[i].nxt)
{
int v=e[i].to;
if(!dep[v] && e[i].w)
{
dep[v]=dep[u]+1;Q.push(v);
}
}
}
return dep[t];
}
int dfs(int u,int flow)
{
if(u==t || !flow)return flow;
for(int i=front[u];i!=-1;i=e[i].nxt)
{
int v=e[i].to;
if(dep[v]==dep[u]+1 && e[i].w)
{
int di=dfs(v,min(flow,e[i].w));
if(di)
{
e[i].w-=di;e[i^1].w+=di;
return di;
}
else dep[v]=0;
}
}
return 0;
}
int dinic()
{
int flow=0;
while(bfs())
{
while(int d=dfs(s,Inf))flow+=d;
}
return flow;
}
int m;
int main()
{
memset(front,-1,sizeof(front));
scanf("%d%d%d%d", &n,&m,&s,&t);
t+=n;
for(int i=1;i<=n;i++)
if(i!=s && (i!=t-n))Add(i,i+n,1);
else Add(i,i+n,Inf);
while(m--)
{
int u,v;scanf("%d%d",&u,&v);
Add(u+n,v,Inf);
Add(v+n,u,Inf);
}
printf("%d\n",dinic());
return 0;
}
最新文章
- 諾基亞定制的Android系統名為 Z Launcher
- C++ 非阻塞套接字的使用 (3)
- JSP的原理
- Codeforces 86D Powerful array(莫队算法)
- Date Picker Calendar For Oracle Forms 6i
- js net 除法取整
- python 自动化之路 day 04.1 python内置函数
- OA请假流程 -- 编码
- radio 和checkbox与文字对齐问题
- 奇妙的go语言(聊天室的开发)
- attribute和property兼容性分析
- ABP官方文档翻译 6.2.1 ASP.NET Core集成
- 文件系统扫描工具-fsck
- ping百度域名时的收获
- go 方法
- 开发中经常遇到SVN清理失败的问题:
- CentOS7 下curl使用
- Vue2.5开发去哪儿网App 第五章笔记 上
- golang init函数
- Spring的配置文件ApplicationContext.xml配置头文件解析