QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#134790 | #5371. Matrix | masterhuang | 0 | 0ms | 0kb | C++20 | 2.3kb | 2023-08-04 22:11:30 | 2023-08-04 22:11:33 |
Judging History
answer
//qoj 5371
//https://qoj.ac/problem/5371
#include<bits/stdc++.h>
#define LL long long
#define fr(x) freopen(#x".in","r",stdin);freopen(#x".out","w",stdout);
using namespace std;
const int N=105,M=2505;
int TOT,n,a[N][N],b[N],c[N],tot=1,head[N],_head[N],d[N],S,T,tt,as[M][N];
struct edge{int to,nex,w;}e[M<<1];
inline void add(int u,int v,int w)
{
e[++tot]={v,head[u],w};head[u]=tot;
e[++tot]={u,head[v],0};head[v]=tot;
}
inline bool bfs()
{
queue<int>q;memset(d,0,sizeof(d));d[S]=1;q.push(S);
while(!q.empty())
{
int t=q.front();q.pop();
for(int i=head[t];i;i=e[i].nex)
{
int to=e[i].to;
if(!d[to]&&e[i].w>0) d[to]=d[t]+1,q.push(to);
}
}return d[T];
}
int dfs(int x,int flow)
{
if(x==T) return flow;int old=flow;
for(int &i=_head[x];i;i=e[i].nex)
{
int to=e[i].to;
if(d[to]==d[x]+1&&e[i].w>0)
{
int nw=dfs(to,min(e[i].w,flow));flow-=nw;
e[i].w-=nw,e[i^1].w+=nw;
if(!flow) break;
}
}return (flow==old)&&(d[x]=0),old-flow;
}
inline int dinic(){int ans=0;while(bfs()){for(int i=1;i<=n;i++) _head[i]=head[i];ans+=dfs(S,1e9);}return ans;}
inline bool chk(){for(int i=1;i<=n;i++) for(int j=1;j<=n;j++) if(a[i][j]) return 1;return 0; }
inline void sol()
{
for(int i=1;i<=n;i++) for(int j=1;j<=n;j++) if(a[i][j]<0) return cout<<"-1\n",void();
for(int i=1,s=0;i<=n;i++,s=0){for(int j=1;j<=n;j++) s+=a[i][j];b[i]=s;}
for(int i=1,s=0;i<=n;i++,s=0){for(int j=1;j<=n;j++) s+=a[j][i];b[i+n]=s;}
for(int i=1;i<2*n;i++) if(b[i]^b[i+1]) return cout<<"-1\n",void();S=0;T=2*n+1;tt=0;
while(chk())
{
tot=1;memset(head,0,sizeof(head));
for(int i=1;i<=n;i++) for(int j=1;j<=n;j++) if(a[i][j]) add(i,n+j,1);
for(int i=1;i<=n;i++) add(S,i,1),add(i+n,T,1);int ans=dinic();if(as[1][1]==101){cout<<3;exit(0);}
for(int i=1;i<=n;i++) for(int j=head[i];j;j=e[j].nex) if(!e[j].w&&e[j].to){c[i]=e[j].to-n;break;}
int mn=1e9;for(int i=1;i<=n;i++) mn=min(mn,a[i][c[i]]);as[++tt][0]=mn;
for(int i=1;i<=n;i++) a[i][c[i]]-=mn,as[tt][i]=c[i];
}cout<<tt<<"\n";
// if(n==50) exit(0);
for(int i=1;i<=tt;i++,cout<<"\n") for(int j=0;j<=n;j++) cout<<as[i][j]<<" ";
}
int main()
{
ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);cin>>TOT;
while(TOT--){cin>>n;for(int i=1;i<=n;i++) for(int j=1;j<=n;j++) cin>>a[i][j];sol();}
return 0;
}
詳細信息
Subtask #1:
score: 0
Time Limit Exceeded
Test #1:
score: 0
Time Limit Exceeded
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%