HDU 4975 A simple Gaussian elimination problem.

A simple Gaussian elimination problem.

Time Limit: 1000ms
Memory Limit: 65536KB
This problem will be judged on  HDU. Original ID: 4975
64-bit integer IO format: %I64d      Java class name: Main
Dragon is studying math. One day, he drew a table with several rows and columns, randomly wrote numbers on each elements of the table. Then he counted the sum of each row and column. Since he thought the map will be useless after he got the sums, he destroyed the table after that.

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

The first line of input contains only one integer, T(<=30), the number of test cases. Following T blocks, each block describes one test case.

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

Each output should occupy one line. Each line should start with "Case #i: ", with i implying the case number. For each case, if we cannot get the original table, just output: "So naive!", else if we can reconstruct the table by more than one ways, you should output one line contains only: "So young!", otherwise (only one way to reconstruct the table) you should output: "So simple!".
 

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

 
解题:跟hdu 4888一样啊。。。关键是是优化判环的过程。。标记已经访问过的点,如果这些点上次访问的时候没有成环,这次再去访问还是不会成环
 
HDU 4975 A simple Gaussian elimination problem.
  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 }
View Code

 

你可能感兴趣的:(simple)