CodeChef ROBOTDAG Robots in a DAG
来源:互联网 发布:知党史,学党史,跟党走 编辑:程序博客网 时间:2024/05/17 19:21
CodeChef ROBOTDAG Robots in a DAG
SAP板子
#include<cstdio>#include<cstring>#include<queue>#include<iostream>#include<algorithm>using namespace std;const int MAXN=200007;const int oo=0x3f3f3f3f;const int MAXM=2000007;//边数的最大值typedef long long LL;const LL loo=4223372036854775807ll;typedef long double LB;const LL mod=1e9+7;struct Edge{ int from, to; Edge(){} Edge(int _a, int _b) { from=_a, to=_b; }};vector<Edge> G;//sapstruct Node{ int from, to, next; int cap;}edge[MAXM];int tol;int head[MAXN];int dep[MAXN];int gap[MAXN];//gap[x]=y :说明残留网络中dep[i]==x的个数为ystruct SAP{ int n;//n是总的点的个数,包括源点和汇点 void init(int _n) { n=_n; tol=0; memset(head, -1, sizeof(head)); } void addedge(int u, int v, int w) { edge[tol].from=u; edge[tol].to=v; edge[tol].cap=w; edge[tol].next=head[u]; head[u]=tol++; edge[tol].from=v; edge[tol].to=u; edge[tol].cap=0; edge[tol].next=head[v]; head[v]=tol++; } void BFS(int start, int end) { memset(dep, -1, sizeof(dep)); memset(gap, 0, sizeof(gap)); gap[0]=1; int que[MAXN]; int front, rear; front=rear=0; dep[end]=0; que[rear++]=end; while(front!=rear) { int u=que[front++]; if(front==MAXN)front=0; for(int i=head[u];i!=-1;i=edge[i].next) { int v=edge[i].to; if(dep[v]!=-1)continue; que[rear++]=v; if(rear==MAXN)rear=0; dep[v]=dep[u]+1; ++gap[dep[v]]; } } } int sap(int start, int end, int k)//这里加了限制,流超过k就直接返回,为了加快速度 { int res=0; BFS(start, end); int cur[MAXN]; int S[MAXN]; int top=0; memcpy(cur, head, sizeof(head)); int u=start; int i; while(dep[start]<n) { if(u==end) { int temp=oo; int inser; for(i=0;i<top;i++) if(temp>edge[S[i]].cap) { temp=edge[S[i]].cap; inser=i; } for(i=0;i<top;i++) { edge[S[i]].cap-=temp; edge[S[i]^1].cap+=temp; } res+=temp; top=inser; u=edge[S[top]].from; } if(u!=end&&gap[dep[u]-1]==0)//出现断层,无增广路 break; if(res>=k) break;//流大于k直接返回 for(i=cur[u];i!=-1;i=edge[i].next) if(edge[i].cap!=0&&dep[u]==dep[edge[i].to]+1) break; if(i!=-1) { cur[u]=i; S[top++]=i; u=edge[i].to; } else { int min=n; for(i=head[u];i!=-1;i=edge[i].next) { if(edge[i].cap==0)continue; if(min>dep[edge[i].to]) { min=dep[edge[i].to]; cur[u]=i; } } --gap[dep[u]]; dep[u]=min+1; ++gap[dep[u]]; if(u!=start)u=edge[S[--top]].from; } } return res; }}isap;int main(){ int T;scanf("%d", &T); while(T--) { int n, m, k; scanf("%d%d%d", &n, &m, &k); G.clear(); for(int i=0;i<m;i++) { int a, b;scanf("%d%d", &a, &b); G.push_back(Edge(a, b)); } int l=2, r=m+1; int ans=-1; while(l<=r) { int mid=(l+r)>>1; const int super_t=(n*mid+1); isap.init(n*mid+1); for(Edge e : G) { int u=e.from, v=e.to; for(int j=1;j<mid;j++) isap.addedge(u+(j-1)*n, v+j*n, 1); } for(int j=1;j<=mid;j++) { isap.addedge(j*n, super_t, oo); } int flow=isap.sap(1, super_t, k); if(flow>=k) { r=mid-1, ans=mid-1; } else l=mid+1; } printf("%d\n", ans); } return 0;}
阅读全文
0 0
- CodeChef ROBOTDAG Robots in a DAG
- Topological Sorting in a DAG
- codechef Sums in a Triangle题解
- CodeChef A
- POJ 1548 Robots(DAG最小路径覆盖)
- POJ 1548 Robots(DAG最小路径覆盖)
- POJ 1548 Robots(DAG最小路径覆盖)
- codechef May challenge A
- A Summary of 'Emotion in Reinforcement Learning Agents and Robots: A Survey'
- Robots on a grid
- Robots on a grid
- Robots on a grid
- UVa 1025 A Spy in the Metro dp : DAG、最短路
- uva 1025 A Spy in the Metro DAG上的动态规划(固定终点)
- 【CodeChef】Holes in the text
- Codechef Not a Triangle题解
- Codechef A Simple Equation 题解
- CodeChef
- GamePlay
- 数据结构 二叉树的先序 中序 后序遍历(linux 下c语言实现)
- C语言中typedef和define一些理解
- vue的修饰符
- MySQL创建视图的语法格式
- CodeChef ROBOTDAG Robots in a DAG
- lua-2 逻辑控制和函数
- 自定义linux命令
- C语言如何模拟C++实现继承和多态
- B1006
- 《应用拆分与平台搭建最佳实践》- 拆分中的工作内容
- Json Web Token
- 证明接口中量和方法的默认访问修饰符及其信息
- leetcode485