兰州大学第一届『飞马杯』程序设计竞赛 - ★★体育课排队★★(二分+最大流)
作者:互联网
题目链接:点击查看
题目大意:给出 n n n 个人,再给出 n n n 个指定位置,每个人每秒钟可以向上下左右四个方向移动一个单位,问最少需要多长时间,才能使所有人都到达指定位置
题目分析:数据比较小,可以直接二分时间然后跑最大流 c h e c k check check,输出路径也是中规中矩的,注意行末空格就好啦
代码:
// Problem: ★★体育课排队★★
// Contest: NowCoder
// URL: https://ac.nowcoder.com/acm/contest/16520/B
// Memory Limit: 524288 MB
// Time Limit: 8000 ms
//
// Powered by CP Editor (https://cpeditor.org)
// #pragma GCC optimize(2)
// #pragma GCC optimize("Ofast","inline","-ffast-math")
// #pragma GCC target("avx,sse2,sse3,sse4,mmx")
#include<iostream>
#include<cstdio>
#include<string>
#include<ctime>
#include<cmath>
#include<cstring>
#include<algorithm>
#include<stack>
#include<climits>
#include<queue>
#include<map>
#include<set>
#include<sstream>
#include<cassert>
#include<bitset>
#include<list>
#include<unordered_map>
#define lowbit(x) x&-x
using namespace std;
typedef long long LL;
typedef unsigned long long ull;
template<typename T>
inline void read(T &x)
{
T f=1;x=0;
char ch=getchar();
while(0==isdigit(ch)){if(ch=='-')f=-1;ch=getchar();}
while(0!=isdigit(ch)) x=(x<<1)+(x<<3)+ch-'0',ch=getchar();
x*=f;
}
template<typename T>
inline void write(T x)
{
if(x<0){x=~(x-1);putchar('-');}
if(x>9)write(x/10);
putchar(x%10+'0');
}
const int inf=0x3f3f3f3f;
const int N=2e3+100;
struct Edge
{
int to,w,next;
}edge[N*N*2];//边数
int head[N],cnt;
void addedge(int u,int v,int w)
{
edge[cnt].to=v;
edge[cnt].w=w;
edge[cnt].next=head[u];
head[u]=cnt++;
edge[cnt].to=u;
edge[cnt].w=0;//反向边边权设置为0
edge[cnt].next=head[v];
head[v]=cnt++;
}
int d[N],now[N];//深度 当前弧优化
bool bfs(int s,int t)//寻找增广路
{
memset(d,0,sizeof(d));
queue<int>q;
q.push(s);
now[s]=head[s];
d[s]=1;
while(!q.empty())
{
int u=q.front();
q.pop();
for(int i=head[u];i!=-1;i=edge[i].next)
{
int v=edge[i].to;
int w=edge[i].w;
if(d[v])
continue;
if(!w)
continue;
d[v]=d[u]+1;
now[v]=head[v];
q.push(v);
if(v==t)
return true;
}
}
return false;
}
int dinic(int x,int t,int flow)//更新答案
{
if(x==t)
return flow;
int rest=flow,i;
for(i=now[x];i!=-1&&rest;i=edge[i].next)
{
int v=edge[i].to;
int w=edge[i].w;
if(w&&d[v]==d[x]+1)
{
int k=dinic(v,t,min(rest,w));
if(!k)
d[v]=0;
edge[i].w-=k;
edge[i^1].w+=k;
rest-=k;
}
}
now[x]=i;
return flow-rest;
}
void init()
{
memset(now,0,sizeof(now));
memset(head,-1,sizeof(head));
cnt=0;
}
int solve(int st,int ed)
{
int ans=0,flow;
while(bfs(st,ed))
while(flow=dinic(st,ed,inf))
ans+=flow;
return ans;
}
int n,m;
int x[N],y[N],s[N],xx[N],yy[N];
bool check(int mid) {
init();
int st=N-1,ed=st-1;
for(int i=1;i<=n;i++) {
addedge(st,i,1);
addedge(i+n,ed,1);
}
int tot;
for(int i=1;i<=n;i++) {
tot=0;
for(int j=1;j<=m;j++) {
for(int k=0;k<s[j];k++) {
tot++;
if(abs(x[i]-(xx[j]+k))+abs(y[i]-yy[j])<=mid) {
addedge(i,tot+n,1);
}
}
}
}
return solve(st,ed)==n;
}
int main()
{
#ifndef ONLINE_JUDGE
// freopen("data.in.txt","r",stdin);
// freopen("data.out.txt","w",stdout);
#endif
// ios::sync_with_stdio(false);
int w;
cin>>w;
while(w--) {
read(n),read(m);
for(int i=1;i<=n;i++) {
read(x[i]),read(y[i]);
}
for(int i=1;i<=m;i++) {
read(s[i]),read(xx[i]),read(yy[i]);
}
int l=0,r=5000,ans=0;
while(l<=r) {
int mid=(l+r)>>1;
if(check(mid)) {
ans=mid;
r=mid-1;
} else {
l=mid+1;
}
}
check(ans);
cout<<ans<<endl;
int tot=0;
for(int j=1;j<=m;j++) {
for(int k=0;k<s[j];k++) {
tot++;
for(int i=head[n+tot];i!=-1;i=edge[i].next) {
int to=edge[i].to;
if(to>=1&&to<=n&&edge[i].w==1) {
printf("%d%c",to,k==s[j]-1?'\n':' ');
break;
}
}
}
}
}
return 0;
}
标签:二分,head,兰州大学,int,cnt,read,edge,include,体育课 来源: https://blog.csdn.net/qq_45458915/article/details/117390337