HDU4975 A simple Gaussian elimination problem.

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

题意:和hdu4888差不多,把那题的k改成9就是。。。

这题多校第三场出现过(就是hdu4888)。。。不过那题时限不卡.....

还是一样的对残余网络判环。。。自己写的dfs太弱了。。比赛没过。。。然后用4888的标程。。也过不了。。后来看了下这题的标程。。就是优化了一下判环。。。这里的判环如果在这个点判过了(有环或无环),那么下次又访问到这个节点是可以直接返回了。。。弱渣还是太弱。。。。

#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int MAXN=1010;
const int MAXE=600010;
const int INF=1<<30;
int head[MAXN],size;
struct EDGE
{
    int v,next;
    int cap;
    bool flag;
}edge[MAXE];
void init()
{
    memset(head,-1,sizeof(head));
    size=0;
}
void add_edge(int u,int v,int cap)
{
    edge[size].v=v;
    edge[size].cap=cap;
    edge[size].flag=0;
    edge[size].next=head[u];
    head[u]=size++;
    edge[size].v=u;
    edge[size].cap=0;
    edge[size].flag=0;
    edge[size].next=head[v];
    head[v]=size++;
}
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++)
        pe[i]=head[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;
            pe[u]=head[u];
            for(i=head[u];i!=-1;i=edge[i].next)
                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;
    for(int i=head[u];i!=-1;i=edge[i].next)
    {
        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;
            add_edge(s,i,x);
            for(j=1;j<=m;j++)
            {
                add_edge(i,j+n,9);
            }
        }
        for(i=1;i<=m;i++)
        {
            scanf("%d",&x);
            sum2+=x;
            add_edge(i+n,t,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
    评论

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

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值