[cogs396] [网络流24题#4] 魔术球 [网络流,最大流,最小路径覆盖]
2024-08-22 23:19:42
本题枚举每多一个球需要多少个柱子,可以边加边边计算,每次只需要判断$i-Dinic()$即可;特别注意边界。
#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <ctime>
#include <queue> using namespace std; template<const int _n>
struct Edge
{
struct Edge_base { int to,w,next; }e[_n];
int cnt,p[_n];
Edge() { clear(); }
void clear() { cnt=,memset(p,,sizeof(p)); }
int start(const int x) { return p[x]; }
Edge_base& operator[](const int x) { return e[x]; }
void insert(const int x,const int y,const int z)
{ e[++cnt].to=y; e[cnt].next=p[x]; e[cnt].w=z; p[x]=cnt; return ; }
}; int n,level[],cur[],SSS,TTT,Ans;
Edge<> e; bool Bfs(const int S)
{
int i,t;
queue<int> Q;
memset(level,,sizeof(level));
level[S]=;
Q.push(S);
while(!Q.empty())
{
t=Q.front(),Q.pop();
for(i=e.start(t);i;i=e[i].next)
{
if(!level[e[i].to] && e[i].w)
{
level[e[i].to]=level[t]+;
Q.push(e[i].to);
}
}
}
return level[TTT];
} int Dfs(const int S,const int bk)
{
if(S==TTT)return bk;
int rest=bk;
for(int &i=cur[S];i;i=e[i].next)
{
if(level[e[i].to]==level[S]+ && e[i].w)
{
int flow=Dfs(e[i].to,min(rest,e[i].w));
e[i].w-=flow;
e[i^].w+=flow;
if((rest-=flow)<=)break;
}
}
if(rest==bk)level[S]=;
return bk-rest;
} int Dinic()
{
while(Bfs(SSS))
{
memcpy(cur,e.p,sizeof(cur));
Ans+=Dfs(SSS,0x3f3f3f3f);
}
return Ans;
} int Build()
{
SSS=,TTT=SSS+;
int j;
e.insert(SSS,,);
e.insert(,SSS,);
e.insert(,TTT,);
e.insert(TTT,,);
for(j=;j-Dinic()<=n;)
{
j++;
e.insert(SSS,j,),e.insert(j,SSS,);
e.insert(j+,TTT,),e.insert(TTT,j+,);
for(int i=;i<j;++i)
{
int t=i+j;
for(int k=;k*k<=t;++k)
if(t==k*k)
{
e.insert(i,j+,);
e.insert(j+,i,);
break;
}
}
}
return j;
} int main()
{
freopen("balla.in","r",stdin);
freopen("balla.out","w",stdout);
int mid;
scanf("%d",&n); mid=Build(); printf("%d\n",mid-); return ;
}
最新文章
- [CentOs7]搭建ftp服务器(3)——上传,下载,删除,重命名,新建文件夹
- Python时区转换
- Perl重命名当前目录下的文件
- 黑盒测试用例设计方法&;理论结合实际 ->; 因果图法
- Altium Designer完美双屏显示方法演示
- 【腾讯Bugly干货分享】RecyclerView 必知必会
- cas改造随笔
- 不错的轮播插件flexslider
- js+dom开发第十六天
- 小菜学习Lucene.Net(更新3.0.3版本使用)
- Android Weekly Notes Issue #250
- ServiceFabric极简文档-1.0 Service Fabric 自定义集群部署
- Swift基础之音乐播放随机变换着色板
- python爬虫数据解析之BeautifulSoup
- JS 超类和子类
- Openvswitch手册(4): Mirror
- [转]Hive开发经验问答式总结
- ajax代码整理
- # 2017-2018-1 20155224 加分项-实现mypwd
- pta 习题集 5-15 数组循环左移