# HDU4975 A simple Gaussian elimination problem.

17 篇文章 0 订阅
9 篇文章 0 订阅

## A simple Gaussian elimination problem.

Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 474 Accepted Submission(s): 156

Problem Description
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
2014 Multi-University Training Contest 10

#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int MAXN=1010;
const int MAXE=600010;
const int INF=1<<30;
struct EDGE
{
int v,next;
int cap;
bool flag;
}edge[MAXE];
void init()
{
size=0;
}
{
edge[size].v=v;
edge[size].cap=cap;
edge[size].flag=0;
edge[size].v=u;
edge[size].cap=0;
edge[size].flag=0;
}
int pe[MAXN],pre[MAXN],gap[MAXN],dist[MAXN];
int ISAP(int s,int t)
{
int i,neck,temp,cur_flow,u;
int max_flow;
memset(pre,-1,sizeof(pre));
memset(gap,0,sizeof(gap));
memset(dist,0,sizeof(dist));
for(i=0;i<t+1;i++)
gap[t+1]=t+1;
u=s;
max_flow=0;
while(dist[s]<t+1)
{
if(u==t)
{
cur_flow=INF;
for(i=s;i!=t;i=edge[pe[i]].v)
{
if(cur_flow>edge[pe[i]].cap)
{
cur_flow=edge[pe[i]].cap;
neck=i;
}
}
for(i=s;i!=t;i=edge[pe[i]].v)
{
temp=pe[i];
edge[temp].cap-=cur_flow;
edge[temp^1].cap+=cur_flow;
}
u=neck;
max_flow+=cur_flow;
}
for(i=pe[u];i!=-1;i=edge[i].next)
if(edge[i].cap&&dist[u]==dist[edge[i].v]+1)
break;
if(i!=-1)
{
pe[u]=i;
pre[edge[i].v]=u;
u=edge[i].v;
}
else
{
if(0==--gap[dist[u]])
break;
temp=t+1;
if(edge[i].cap)
temp=min(temp,dist[edge[i].v]);
dist[u]=temp+1;
++gap[dist[u]];
if(u!=s)
u=pre[u];
}
}
return max_flow;
}
int vis[MAXN],flag;
void dfs(int u,int cnt)
{
if(flag)
return;
if(vis[u]==1)
{
flag=1;
return;
}
if(vis[u]==2)
return;
vis[u]=1;
{
if(edge[i].cap>0&&edge[i].flag==0&&edge[i^1].flag==0)
{
edge[i].flag=1;
edge[i^1].flag=1;
dfs(edge[i].v,cnt+1);
edge[i].flag=0;
edge[i^1].flag=0;
}
}
vis[u]=2;
}
int main()
{
int T,n,m,i,j,s,t,x,c=1;
scanf("%d",&T);
while(T--)
{
scanf("%d%d",&n,&m);
s=0,t=n+m+1;
init();
int sum1=0,sum2=0;
for(i=1;i<=n;i++)
{
scanf("%d",&x);
sum1+=x;
for(j=1;j<=m;j++)
{
}
}
for(i=1;i<=m;i++)
{
scanf("%d",&x);
sum2+=x;
}
int ans=ISAP(s,t);
printf("Case #%d: ",c++);
if(ans!=sum1||ans!=sum2)
{
printf("So naive!\n");
continue;
}
flag=0;
memset(vis,0,sizeof(vis));
for(i=1;i<=n+m;i++)
if(!flag&&!vis[i])
dfs(i,0);
if(flag)
printf("So young!\n");
else
printf("So simple!\n");
}
return 0;
}


• 0
点赞
• 0
收藏
觉得还不错? 一键收藏
• 打赏
• 0
评论
09-23
08-13 821
07-25 537
08-07 534
07-29 478
07-23 474
08-21 452
08-06 438
08-12 393
08-22 381
07-29 363
08-12 359
07-31 358
07-30 358

### “相关推荐”对你有帮助么？

• 非常没帮助
• 没帮助
• 一般
• 有帮助
• 非常有帮助

eeeaaaaa

¥1 ¥2 ¥4 ¥6 ¥10 ¥20

1.余额是钱包充值的虚拟货币，按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载，可以购买VIP、付费专栏及课程。