QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#166527#5371. MatrixNATURAL60 0ms0kbC++142.7kb2023-09-06 14:19:472023-09-06 14:19:48

Judging History

你现在查看的是最新测评结果

  • [2023-09-06 14:19:48]
  • 评测
  • 测评结果:0
  • 用时:0ms
  • 内存:0kb
  • [2023-09-06 14:19:47]
  • 提交

answer

#include<bits/stdc++.h>
using namespace std;
#define inf 1e9
inline int qread()
{
	int a=0,f=1;char ch=getchar();
	while(!isdigit(ch)){if(ch=='-')f=-1;ch=getchar();}
	while(isdigit(ch)){(a*=10)+=(ch^48);ch=getchar();}
	return a*f;
}
inline void qw(int x)
{
	if(x>9)qw(x/10);
	putchar('0'+x%10);
	return ;
}
int T,n,op,A[51][51],cnt,h[51],l[51],s,t,tp;
int id,mp[2510][51],lamd[2510];
int tot,nex[6000],head[110],u[6000],to[6000],flow[6000],cur[110],gap[110],dep[110],que[110],st,ed,p,maxflow;
long long sum,S;
inline void adde(int x,int y,int val)
{
	to[++tot]=y;u[tot]=x;nex[tot]=head[x];head[x]=tot;flow[tot]=val;
	to[++tot]=x;u[tot]=y;nex[tot]=head[y];head[y]=tot;flow[tot]=0;
	return ;
}
inline void bfs()
{
	memset(dep,-1,(t+1)<<2);
	memset(gap,0,(t+1)<<2);
	que[st=ed=1]=t;++gap[dep[t]=0];
	while(st<=ed)
	{
		p=que[st++];
		for(int i=head[p];i;i=nex[i])
		{
			if(dep[to[i]]==-1)
			{
				++gap[dep[to[i]]=dep[p]+1];
				que[++ed]=to[i];
			}
		}
	}
	return ;
}
inline int dfs(int rt,int V)
{
	if(rt==t)return maxflow+=V,V;
	int an=0,cx=0;
	for(int i=cur[rt];i;i=nex[i])
	{
		cur[rt]=i;
		if(dep[to[i]]+1==dep[rt])
		{
			if(!flow[i])continue;
			cx=dfs(to[i],min(flow[i],V-an));
			if(cx)
			{
				an+=cx;
				flow[i]-=cx;
				flow[i^1]+=cx;
			}
			if(an==V)return V;
		}
	}
	--gap[dep[rt]];
	if(!gap[dep[rt]])dep[s]=t+1;
	++gap[++dep[rt]];
	return an;	
}
int main()
{
	freopen("matrix.in","r",stdin);
	freopen("matrix.out","w",stdout);
	T=qread();
	while(T--)
	{
		n=qread();sum=cnt=id=0;op=1;
		for(int i=1;i<=n;++i)for(int j=1;j<=n;++j)
		{
			A[i][j]=qread();
			if(A[i][j]<0)op=0;
		}
		for(int i=1;i<=n;++i)sum+=A[1][i];
		for(int i=2;i<=n;++i)
		{
			S=0;
			for(int j=1;j<=n;++j)S+=A[i][j];
			if(S^sum)op=0;
		}
		for(int i=1;i<=n;++i)
		{
			S=0;
			for(int j=1;j<=n;++j)S+=A[j][i];
			if(S^sum)op=0;
		}
		if(!op){puts("-1");continue;}
		for(int i=1;i<=n;++i)h[i]=++cnt;
		for(int i=1;i<=n;++i)l[i]=++cnt;
		s=++cnt;t=++cnt;
		while(1)
		{
			tot=1;maxflow=0;
			memset(head,0,(t+1)<<2);
			for(int j=1;j<=n;++j)for(int k=1;k<=n;++k)if(A[j][k])adde(h[j],l[k],1);tp=tot;
			for(int i=1;i<=n;++i)adde(s,h[i],1),adde(l[i],t,1);
			bfs();
			while(dep[s]<=t)memcpy(cur,head,(t+1)<<2),dfs(s,inf);
			if(maxflow!=n)break;++id;
			memset(mp[id],0,sizeof(mp[id]));
			maxflow=inf;p=0;
			for(int i=2;i<=tp;i+=2)
			{
				if(!flow[i])
				{
					mp[id][++p]=to[i]-n;
					maxflow=min(A[u[i]][to[i]-n],maxflow);
				}
			}
			lamd[id]=maxflow;
			for(int i=1;i<=n;++i)A[i][mp[id][i]]-=maxflow;
		}
		qw(id),puts("");
		for(int i=1;i<=id;++i)
		{
			qw(lamd[i]);putchar(' ');
			for(int j=1;j<=n;++j)qw(mp[i][j]),putchar(' ');
			puts("");
		}
	}
	return 0;	
} 

Details

Tip: Click on the bar to expand more detailed information

Subtask #1:

score: 0
Dangerous Syscalls

Test #1:

score: 0
Dangerous Syscalls

input:

10
5
41 18467 6334 26500 2995
19169 15724 11478 29358 -21392
26962 24464 5705 28145 -30939
23281 16827 9961 491 3777
-15116 -21145 20859 -30157 99896
5
4827 5436 32391 14604 1869
3902 153 292 12382 42398
17421 18716 19718 19895 -16623
5447 21726 14771 11538 5645
27530 13096 -8045 708 25838
5
41879 4...

output:


result:


Subtask #2:

score: 0
Skipped

Dependency #1:

0%

Subtask #3:

score: 0
Skipped

Dependency #1:

0%