A simple Gaussian elimination problem.
64-bit integer IO format: %I64d Java class name: Main
However Dragon's mom came back and found what he had done. She would give dragon a feast if Dragon could reconstruct the table, otherwise keep Dragon hungry. Dragon is so young and so simple so that the original numbers in the table are one-digit number (e.g. 0-9).
Could you help Dragon to do that?
Input
There are three lines for each block. The first line contains two integers N(<=500) and M(<=500), showing the number of rows and columns.
The second line contains N integer show the sum of each row.
The third line contains M integer show the sum of each column.
Output
Sample Input
3 1 1 5 5 2 2 0 10 0 10 2 2 2 2 2 2
Sample Output
Case #1: So simple! Case #2: So naive! Case #3: So young!
Source
1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <queue> 5 using namespace std; 6 const int maxn = 1010; 7 const int INF = 0x3f3f3f3f; 8 struct arc{ 9 int to,flow,next; 10 arc(int x = 0,int y = 0,int z = -1){ 11 to = x; 12 flow = y; 13 next = z; 14 } 15 }e[1000000]; 16 int head[maxn],d[maxn],cur[maxn],tot,S,T; 17 bool vis[maxn],hv[maxn]; 18 void add(int u,int v,int flow){ 19 e[tot] = arc(v,flow,head[u]); 20 head[u] = tot++; 21 e[tot] = arc(u,0,head[v]); 22 head[v] = tot++; 23 } 24 bool bfs(){ 25 queue<int>q; 26 memset(d,-1,sizeof d); 27 d[S] = 1; 28 q.push(S); 29 while(!q.empty()){ 30 int u = q.front(); 31 q.pop(); 32 for(int i = head[u]; ~i; i = e[i].next){ 33 if(e[i].flow && d[e[i].to] == -1){ 34 d[e[i].to] = d[u] + 1; 35 q.push(e[i].to); 36 } 37 } 38 } 39 return d[T] > -1; 40 } 41 int dfs(int u,int low){ 42 if(u == T) return low; 43 int tmp = 0,a; 44 for(int &i = cur[u]; ~i; i = e[i].next){ 45 if(e[i].flow && d[e[i].to] == d[u]+1&&(a=dfs(e[i].to,min(low,e[i].flow)))){ 46 e[i].flow -= a; 47 e[i^1].flow += a; 48 low -= a; 49 tmp += a; 50 if(!low) break; 51 } 52 } 53 if(!tmp) d[u] = -1; 54 return tmp; 55 } 56 int dinic(){ 57 int ret = 0; 58 while(bfs()){ 59 memcpy(cur,head,sizeof head); 60 ret += dfs(S,INF); 61 } 62 return ret; 63 } 64 bool dfs2(int u,int fa){ 65 if(vis[u]) return true; 66 vis[u] = true; 67 for(int i = head[u]; ~i; i = e[i].next) 68 if(!hv[e[i].to] && e[i].flow && e[i].to != fa && dfs2(e[i].to,u)) return true; 69 hv[u] = true; 70 return vis[u] = false; 71 } 72 int main(){ 73 int Ts,n,m,tmp,sum,sum2,cs = 1; 74 scanf("%d",&Ts); 75 while(Ts--){ 76 scanf("%d %d",&n,&m); 77 memset(head,-1,sizeof head); 78 memset(hv,false,sizeof hv); 79 sum2 = sum = S = tot = 0; 80 T = n + m + 1; 81 for(int i = 1; i <= n; ++i){ 82 scanf("%d",&tmp); 83 add(S,i,tmp); 84 sum += tmp; 85 for(int j = 1; j <= m; ++j) 86 add(i,j+n,9); 87 } 88 for(int i = 1; i <= m; ++i){ 89 scanf("%d",&tmp); 90 add(i+n,T,tmp); 91 sum2 += tmp; 92 } 93 if(sum == sum2){ 94 if(sum == dinic()){ 95 bool flag = false; 96 memset(vis,false,sizeof vis); 97 for(int i = 1; i <= n; ++i) 98 if(flag = dfs2(i,-1)) break; 99 if(flag) printf("Case #%d: So young!\n",cs++); 100 else printf("Case #%d: So simple!\n",cs++); 101 }else printf("Case #%d: So naive!\n",cs++); 102 }else printf("Case #%d: So naive!\n",cs++); 103 } 104 return 0; 105 }