1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103
| #include<cstdio> #include<cstring> #include<algorithm> #define fo(i,a,b) for(int i=a;i<=b;i++) using namespace std;
typedef long long LL;
const int maxn=205, maxp=20405, maxe=80205;
int n,mp[maxn][maxn],dg[maxn],sum;
int ReadDigit() { char ch=getchar(); while (ch<'0' || ch>'9') ch=getchar(); return ch-'0'; }
int tot,go[2*maxe],val[2*maxe],Cos[2*maxe],nxt[2*maxe],f1[maxp]; void ins(int x,int y,int z,int c) { go[++tot]=y; val[tot]=z; Cos[tot]=c; nxt[tot]=f1[x]; f1[x]=tot; }
LL ans; int d[10*maxp],dis[maxp],fro[maxp][2]; bool bz[maxp]; void McMf() { while (1) { memset(dis,127,sizeof(dis)), dis[0]=0; bz[ d[1]=0 ]=1; for(int i=1, j=1; i<=j; i++) { for(int p=f1[d[i]]; p; p=nxt[p]) if (val[p] && dis[d[i]]+Cos[p]<dis[go[p]]) { dis[go[p]]=dis[d[i]]+Cos[p]; fro[go[p]][0]=d[i], fro[go[p]][1]=p; if (!bz[go[p]]) { bz[ d[++j]=go[p] ]=1; if (dis[d[i+1]]>dis[d[j]]) swap(d[i+1],d[j]); } } bz[d[i]]=0; } if (dis[sum]==2139062143) break; for(int i=sum; i; i=fro[i][0]) { int p=fro[i][1]; val[p]--, val[(p&1) ?p+1 :p-1 ]++; ans+=Cos[p]; } } }
int T; int main() { scanf("%d",&T); while (T--) { tot=0; memset(f1,0,sizeof(f1)); scanf("%d",&n); memset(dg,0,sizeof(dg)); fo(i,1,n) fo(j,1,n) { mp[i][j]=ReadDigit(); if (mp[i][j]==1) dg[i]++; } fo(i,1,n) { ins(0,i,n+5,0), ins(i,0,0,0); fo(j,dg[i]+1,n-1) ins(i,n+i,1,j*(j-1)/2-(j-1)*(j-2)/2), ins(n+i,i,0,0); } sum=2*n; fo(i,1,n-1) fo(j,i+1,n) if (mp[i][j]==2) { sum++; ins(n+i,sum,1,0), ins(sum,n+i,0,0); ins(n+j,sum,1,0), ins(sum,n+j,0,0); } fo(i,2*n+1,sum) ins(i,sum+1,1,0), ins(sum+1,i,0,0); sum++; ans=0; fo(i,1,n) ans+=dg[i]*(dg[i]-1)/2; McMf(); ans=(LL)n*(n-1)*(n-2)*(n-3)-ans*(n-3)*8; printf("%I64d\n",ans); } }
|