QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#166521 | #5371. Matrix | NATURAL6 | 0 | 1ms | 3612kb | C++14 | 2.6kb | 2023-09-06 14:16:23 | 2023-09-06 14:16:24 |
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()
{
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)
{
for(int j=1;j<=n;++j)qw(mp[i][j]),putchar(' ');
qw(lamd[i]),puts("");
}
}
return 0;
}
詳細信息
Subtask #1:
score: 0
Wrong Answer
Test #1:
score: 0
Wrong Answer
time: 1ms
memory: 3612kb
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:
-1 -1 17 1 2 3 4 5 8106 1 2 3 5 4 23247 1 3 2 5 4 10526 5 3 2 1 4 10350 2 3 1 5 4 5684 5 2 1 3 4 2380 5 2 1 4 3 6290 2 1 5 4 3 29460 5 4 1 2 3 2974 5 4 1 3 2 16557 4 1 5 3 2 15455 3 4 5 1 2 27014 4 3 5 1 2 7101 4 3 5 2 1 3550 2 5 4 3 1 7367 3 5 4 2 1 6233 5 3 4 2 1 10323 -1 -1 -1 -1 -1 -1 -1
result:
wrong answer Integer 8106 violates the range [1, 5] (test case 3)
Subtask #2:
score: 0
Skipped
Dependency #1:
0%
Subtask #3:
score: 0
Skipped
Dependency #1:
0%