传送门
文章目录
- 题意:
- 思路:
题意:
思路:
读懂题就会辣,经典模型了,二分时间,让后将其转换成二分图,左边是人,右边是位置,能在规定时间到的连边,跑匹配即可。
// 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("Ofast,no-stack-protector,unroll-loops,fast-math")
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4.1,sse4.2,avx,avx2,popcnt,tune=native")
//#pragma GCC optimize(2)
#include<cstdio>
#include<iostream>
#include<string>
#include<cstring>
#include<map>
#include<cmath>
#include<cctype>
#include<vector>
#include<set>
#include<queue>
#include<algorithm>
#include<sstream>
#include<ctime>
#include<cstdlib>
#define X first
#define Y second
#define L (u<<1)
#define R (u<<1|1)
#define pb push_back
#define mk make_pair
#define Mid (tr[u].l+tr[u].r>>1)
#define Len(u) (tr[u].r-tr[u].l+1)
#define random(a,b) ((a)+rand()%((b)-(a)+1))
#define db puts("---")
using namespace std;//void rd_cre() { freopen("d://dp//data.txt","w",stdout); srand(time(NULL)); }
//void rd_ac() { freopen("d://dp//data.txt","r",stdin); freopen("d://dp//AC.txt","w",stdout); }
//void rd_wa() { freopen("d://dp//data.txt","r",stdin); freopen("d://dp//WA.txt","w",stdout); }typedef long long LL;
typedef unsigned long long ULL;
typedef pair<int,int> PII;const int N=4010,M=N*N*2,mod=1e9+7,INF=0x3f3f3f3f;
const double eps=1e-6;int n,m,t;
PII id[N];
PII p[N];
int c[1010][1010];
struct Node {int len,x,y,id;
}a[N],b[N];
vector<int>v[N];struct Maxflow
{int S,T;int e[M],ne[M],w[M],h[N],hs[N],idx;int depth[N];void init() { idx=0; memset(h,-1,sizeof(h)); }inline void add(int a,int b,int c){e[idx]=b,w[idx]=c,ne[idx]=h[a],h[a]=idx++;e[idx]=a,w[idx]=0,ne[idx]=h[b],h[b]=idx++;}bool bfs(){memset(depth,-1,sizeof (depth)); depth[S]=0;queue<int>q; q.push(S); hs[S]=h[S];while(q.size()){int t=q.front(); q.pop();for(int i=h[t];~i;i=ne[i]){int ver=e[i];if(depth[ver]==-1&&w[i]>0){depth[ver]=depth[t]+1;hs[ver]=h[ver];if(ver==T) return true;q.push(ver);}}}return false;}int dfs(int u,int flow){if(u==T) return flow;int d=flow;for(int i=hs[u];~i;i=ne[i]){int ver=e[i]; hs[u]=i;if(depth[ver]==depth[u]+1&&w[i]>0){int t=dfs(ver,min(w[i],d));if(!t) depth[ver]=-1;d-=t; w[i]-=t; w[i^1]+=t;if(!d) break;}}return flow-d;}int dinic(){int ans=0,flow;while(bfs()) while(flow=dfs(S,INF)) ans+=flow;return ans;}
}MF;bool check(int mid) {MF.idx=0; for(int i=1;i<=n*2+2;i++) MF.h[i]=-1;MF.S=n*2+2; MF.T=n*2+1;for(int i=1;i<=n;i++) MF.add(MF.S,i,1);for(int i=1;i<=t;i++) MF.add(b[i].id,MF.T,1);for(int i=1;i<=n;i++) {for(int j=1;j<=t;j++) {int dis=abs(p[i].X-b[j].x)+abs(p[i].Y-b[j].y);if(dis>mid) continue;MF.add(i,b[j].id,1);}}int ans=MF.dinic();//for(int i=0;i<=MF.T;i++) MF.h[i]=-1;if(ans==n) return true;return false;
}int main()
{
// ios::sync_with_stdio(false);
// cin.tie(0);int _; scanf("%d",&_);while(_--) {scanf("%d%d",&n,&m);for(int i=1;i<=n;i++) scanf("%d%d",&p[i].X,&p[i].Y);int tot=n; t=0;for(int i=1;i<=m;i++) {scanf("%d%d%d",&a[i].len,&a[i].x,&a[i].y);for(int j=1;j<=a[i].len;j++) b[++t]={1,a[i].x+j-1,a[i].y,++tot},id[tot].X=i,id[tot].Y=j;}int l=0,r=5000,ans=-1;while(l<=r) {int mid=(l+r)>>1;if(check(mid)) ans=mid,r=mid-1;else l=mid+1;}printf("%d\n",ans);check(ans);for(int i=0;i<MF.idx;i++) {if(MF.w[i]==0&&MF.e[i^1]>=1&&MF.e[i^1]<=n&&MF.e[i]>n&&MF.e[i]<=n*2) {int u=MF.e[i^1],v=MF.e[i];c[id[v].X][id[v].Y]=u;}}for(int i=1;i<=m;i++) {for(int j=1;j<=a[i].len;j++) printf("%d%c",c[i][j],j==a[i].len? '\n':' ');} }return 0;
}
/**/