QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#166516 | #5371. Matrix | Xun_xiaoyao | 0 | 1ms | 3920kb | C++14 | 3.9kb | 2023-09-06 14:14:03 | 2023-09-06 14:14:03 |
answer
#include <bits/stdc++.h>
using namespace std;
int Qread()
{
int x=0;bool f=false;char ch=getchar();
while(ch<'0'||ch>'9') f|=(ch=='-'),ch=getchar();
while(ch>='0'&&ch<='9') x=x*10+(ch^48),ch=getchar();
return f?-x:x;
}
void Qwrite(int a)
{
if(a<10) return putchar('0'+a),void();
return Qwrite(a/10),putchar('0'+a%10),void();
}
namespace flow{
int p[120],edge_cnt,poi_cnt,S,T;
struct Edge{
int nxt,poi,val,bas;
}l[10010];
void reset(int n)
{
memset(p,0,sizeof(p));
memset(l,0,sizeof(l));
edge_cnt=1,poi_cnt=2*n+3;
S=2*n+1,T=2*n+2;
}
int add_edge(int u,int v)
{
l[++edge_cnt].nxt=p[u],l[edge_cnt].poi=v,l[edge_cnt].val=1,l[edge_cnt].bas=1,p[u]=edge_cnt;
l[++edge_cnt].nxt=p[v],l[edge_cnt].poi=u,l[edge_cnt].val=0,l[edge_cnt].bas=0,p[v]=edge_cnt;
return edge_cnt-1;
}
int dep[120],cur[120];
queue<int> Q;
void bk_fl()
{
for(int i=1;i<=edge_cnt;i++)
l[i].val=l[i].bas;
}
bool bfs()
{
memset(dep,0,poi_cnt<<2);
dep[S]=1,Q.push(S);
while(!Q.empty())
{
int rea=Q.front();Q.pop();
for(int k=cur[rea]=p[rea];k;k=l[k].nxt)
if(!dep[l[k].poi]&&l[k].val)
dep[l[k].poi]=dep[rea]+1,Q.push(l[k].poi);
}
return dep[T];
}
int dfs(int a,int flo)
{
if(a==T||flo==0) return flo;
int ret=0,rem=0;
for(int k=cur[a];k&&flo;k=l[k].nxt)
{
cur[a]=k;
if(dep[l[k].poi]!=dep[a]+1||!l[k].val) continue;
rem=dfs(l[k].poi,min(flo,l[k].val));
flo-=rem,l[k].val-=rem;
ret+=rem,l[k^1].val+=rem;
}
return ret;
}
void Dinic()
{
while(bfs()) dfs(S,20070610);
}
}
struct ANS{int num[60],a;}rem;
vector<ANS> T;
int n,tot;
long long r[60],c[60];
int a[60][60],SS[60],TT[60];
int tk[60][60];
void solve()
{
T.clear();
memset(r,0,sizeof(r));
memset(c,0,sizeof(c));
n=Qread();
flow::reset(n);
tot=n*n;
for(int i=1;i<=n;i++)
for(int j=1;j<=n;j++)
{
a[i][j]=Qread();
r[i]+=a[i][j],c[j]+=a[i][j];
}
for(int i=1;i<=n;i++)
for(int j=1;j<=n;j++)
if(a[i][j]<0)
{
printf("-1\n");
return;
}
if(r[1]!=c[1])
{
printf("-1\n");
return;
}
for(int i=2;i<=n;i++)
if(r[i]!=r[i-1]||c[i]!=c[i-1])
{
printf("-1\n");
return;
}
for(int i=1;i<=n;i++)
for(int j=1;j<=n;j++)
tk[i][j]=flow::add_edge(i,n+j);
for(int i=1;i<=n;i++)
SS[i]=flow::add_edge(flow::S,i),TT[i]=flow::add_edge(n+i,flow::T);
for(int i=1;i<=n;i++)
for(int j=1;j<=n;j++)
if(!a[i][j])
flow::l[tk[i][j]].bas=0,flow::l[tk[i][j]].val=0,tot--;
while(tot)
{
flow::Dinic();
rem.a=1e9+10;
for(int i=1;i<=n;i++)
for(int j=1;j<=n;j++)
if(flow::l[tk[i][j]].bas&&!flow::l[tk[i][j]].val)
{
rem.num[i]=j;
rem.a=min(rem.a,a[i][j]);
}
for(int i=1;i<=n;i++)
if(!(a[i][rem.num[i]]-=rem.a))
{
flow::l[tk[i][rem.num[i]]].bas=0,tot--;
flow::l[tk[i][rem.num[i]]^1].val=0;
flow::l[SS[i]].val=1,flow::l[SS[i]^1].val=0;
flow::l[TT[rem.num[i]]].val=1,flow::l[TT[rem.num[i]]^1].val=0;
}
T.push_back(rem);
}
printf("%d\n",(int)T.size());
for(ANS &g:T)
{
for(int i=1;i<=n;i++)
Qwrite(g.num[i]),putchar(' ');
Qwrite(g.a),putchar('\n');
}
}
int main()
{
int TTTTT=Qread();
while(TTTTT--) solve();
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Subtask #1:
score: 0
Wrong Answer
Test #1:
score: 0
Wrong Answer
time: 1ms
memory: 3920kb
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 2 5 3 4 8670 1 5 2 3 4 1856 5 1 2 3 4 18414 5 1 2 4 3 606 2 1 5 4 3 25895 2 5 1 4 3 9249 2 4 1 5 3 2974 3 4 1 5 2 13236 5 4 1 3 2 8426 3 4 5 1 2 20011 4 3 5 1 2 24454 4 3 5 2 1 1652 5 3 4 2 1 21428 2 5 4 3 1 2495 2 4 5 3 1 1898 -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%