【网络流24题】魔术球问题
2017-01-02 20:41
357 查看
(网络流24题大多需要spj,所以需要一个有spj的oj,本系列代码均在www.oj.swust.edu.cn测试通过)
这道题真是一道好题!
我们可以这样想,每加入一个数就枚举这个数可以和哪几个数相互组合,这样的话我们就可以把问题转化为一个最小路径覆盖问题,算出最少需要几根柱子才能放满这几个数,显然答案具有单调性,但是这题比较特殊,二分会增大时间复杂度,相反,如果每次我们将答案增加一,在原先的残量网络中继续增广的话会省很多时间,当需要的柱子数超过n时跳出,答案就为n-1。
(由于不明确到底有多少点,所以最好将一个点拆分成 <<1和<<1+1)
这道题真是一道好题!
我们可以这样想,每加入一个数就枚举这个数可以和哪几个数相互组合,这样的话我们就可以把问题转化为一个最小路径覆盖问题,算出最少需要几根柱子才能放满这几个数,显然答案具有单调性,但是这题比较特殊,二分会增大时间复杂度,相反,如果每次我们将答案增加一,在原先的残量网络中继续增广的话会省很多时间,当需要的柱子数超过n时跳出,答案就为n-1。
(由于不明确到底有多少点,所以最好将一个点拆分成 <<1和<<1+1)
#include<cstdio> #include<cstdlib> #include<ctime> #include<cmath> #include<cstring> #include<string> #include<iostream> #include<iomanip> #include<algorithm> using namespace std; #define INF 100000000 struct bian { int l,r,f,lei; }a[1000000]; int fir[1000000]; int nex[1000000]; int s=0,t=10000; int d[10001]; bool bfs() { static int dui[1000000]; memset(d,-1,sizeof(d)); int top=1,my_final=2; dui[1]=s; d[s]=1; while(top<my_final) { int u=dui[top++]; for(int o=fir[u];o;o=nex[o]) { if(a[o].f && d[a[o].r]==-1) { d[a[o].r]=d[u]+1; dui[my_final++]=a[o].r; if(a[o].r==t) return true; } } } return false; } int dinic(int u,int flow) { if(u==t) return flow; int left=flow; for(int o=fir[u];o && left;o=nex[o]) { if(a[o].f && d[a[o].r]==d[u]+1) { int temp=dinic(a[o].r,min(a[o].f,left)); if(!temp) d[a[o].r]=-1; left-=temp; a[o].f-=temp; a[o^1].f+=temp; } } return flow-left; } int tot=1; inline void add_edge(int l,int r,int f,int lei=0) { a[++tot].l=l; a[tot].r=r; a[tot].f=f; a[tot].lei=lei; nex[tot]=fir[l]; fir[l]=tot; } inline bool check(int x,int y) { double sum=x+y; int gen=sqrt(sum); if(gen*gen==sum) return true; return false; } int to[1000000]; bool pd[1000000]; int main() { int n; scanf("%d",&n); int ans=1; int flow=0; while(1) { add_edge(s,(ans<<1),1); add_edge((ans<<1),s,0); add_edge((ans<<1)+1,t,1); add_edge(t,(ans<<1)+1,0); for(int i=1;i<=sqrt(2*ans)+1 && i<ans;i++) { int x=i*i-ans; if(x>0 && x<ans) { add_edge((ans<<1),(x<<1)+1,1,1); add_edge((x<<1)+1,(ans<<1),0); } } while(bfs()) flow+=dinic(s,INF); if(ans-flow>n) break; ans++; } printf("%d\n",ans-1); for(int i=2;i<=tot;i++) if(a[i].lei==1 && !a[i].f) to[(a[i].l>>1)]=(a[i].r>>1),pd[(a[i].r>>1)]=true; for(int i=1;i<ans;i++) { if(pd[i]) continue; int t=i; while(t) { printf("%d ",t); t=to[t]; } printf("\n"); } return 0; }
相关文章推荐
- [网络流24题]魔术球问题 贪心||枚举答案+最小路径覆盖
- 【网络流24题】No.4 魔术球问题 (二分+最小路径覆盖)
- 【网络流24题】魔术球问题 二分答案+最小路径覆盖
- [网络流24题] 魔术球问题
- [网络流24题]魔术球问题
- cogs 396. [网络流24题]魔术球问题(简化版
- 【网络流24题----04】魔术球问题
- 【网络流24题】魔术球问题
- [网络流24题]魔术球问题(简化版) 最小路径覆盖+二分答案 + 很快的最大流
- 洛谷2765:[网络流24题]魔术球问题——题解
- 396. [网络流24题]魔术球问题(简化版
- COGS396. [网络流24题]魔术球问题(简化版
- 网络流24题 魔术球问题(弱化?)(二分图解)
- [网络流24题-2]cogs396魔术球问题
- 网络流24题4. 魔术球问题
- 魔术球问题[网络流24题之4]
- cogs396 [网络流24题]魔术球问题简化版
- [网络流24题]魔术球问题(简化版
- 【网络流24题】魔术球问题(最大流)
- [网络流24题]魔术球问题