hdu4975 -网络流

A - A simple Gaussian elimination problem.
Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u

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!

题意:t组数据,然后一个n*m的矩阵,每个单元有一个0-9的整数,但是现在只告诉你:第一行n个数,告诉你每行的和,第二行m个数高数你每列的和;问这样的矩阵是否存在,如果存在是否唯一;

建立起点与汇点,起点与行和相连,列和与终点相连,行与列之间建边权值为9,判断是否满流,不满流则无法得到这样的矩阵,如果满流,利用残余网络判断是否有环,如果有,则有多种方法,否则只有一种;

#include <iostream>
#include <stdio.h>
#include <string.h>

using namespace std;
const int MAXN=1100,maxn=1110;
const int MAXM=503*503*3;
const int INF=0x3f3f3f3f;
struct Node
{
    int to,next,cap;
} edge[MAXM];
int tol;
int head[MAXN];
int gap[MAXN],dis[MAXN],pre[MAXN],cur[MAXN];
void init()
{
    tol=0;
    memset(head,-1,sizeof(head));
}
void addedge(int u,int v,int w,int rw=0)
{
    edge[tol].to=v;
    edge[tol].cap=w;
    edge[tol].next=head[u];
    head[u]=tol++;
    edge[tol].to=u;
    edge[tol].cap=rw;
    edge[tol].next=head[v];
    head[v]=tol++;
}
int sap(int start,int end,int nodenum)
{
    memset(dis,0,sizeof(dis));
    memset(gap,0,sizeof(gap));
    memcpy(cur,head,sizeof(head));
    int u=pre[start]=start,maxflow=0,aug=-1;
    gap[0]=nodenum;
    while(dis[start]<nodenum)
    {
loop:
        for(int  &i=cur[u]; i!=-1; i=edge[i].next)
        {
            int v=edge[i].to;
            if(edge[i].cap&&dis[u]==dis[v]+1)
            {
                if(aug==-1||aug>edge[i].cap)
                    aug=edge[i].cap;
                pre[v]=u;
                u=v;
                if(v==end)
                {
                    maxflow+=aug;
                    for(u=pre[u]; v!=start; v=u,u=pre[u])
                    {
                        edge[cur[u]].cap-=aug;
                        edge[cur[u]^1].cap+=aug;
                    }
                    aug=-1;
                }
                goto loop;
            }
        }

        int mindis=nodenum;
        for(int i=head[u]; i!=-1; i=edge[i].next)
        {
            int v=edge[i].to;
            if(edge[i].cap&&mindis>dis[v])
            {
                cur[u]=i;
                mindis=dis[v];
            }
        }
        if((--gap[dis[u]])==0)break;
        gap[dis[u]=mindis+1]++;
        u=pre[u];
    }
    return maxflow;
}
bool vis[maxn];
bool dfs(int u,int pre)
{
    vis[u]=true;
    int biu=-1;
    for(int i=head[u]; i!=-1; i=edge[i].next)
    {
        int v=edge[i].to;
         if(v==pre)continue;
        if(edge[i].cap>0)
        {
           if(vis[v])
            return true;
           if(dfs(v,u))
            return true;
        }
        if(biu==-1)head[u]=edge[i].next;
        else edge[biu].next=edge[i].next;
        biu=i;

    }
    vis[u]=false;
    return false;
}
bool check(int NodeNum)
{
      memset(vis,false,sizeof(vis));
    for(int i=0;i<=NodeNum;i++)
    {
         if(dfs(i,-1))
            return true;
    }
    return false;
}

int Scan()
{
    int res = 0, ch, flag = 0;

    if((ch = getchar()) == '-')             //判断正负输入外挂。。。
        flag = 1;

    else if(ch >= '0' && ch <= '9')           //得到完整的数
        res = ch - '0';
    while((ch = getchar()) >= '0' && ch <= '9' )
        res = res * 10 + ch - '0';

    return flag ? -res : res;
}
int r[MAXN],c[MAXN];
int main()
{
    int t,TOT=0;
    int rsum,csum,sum ;
    int n,m;
    int S,T;
    ///scanf("%d",&t);
    t=Scan();
    while(t--)
    {
        n=Scan(),m=Scan();
        //scanf("%d%d",&n,&m);
        init();
        rsum=csum=0;
        for(int i=1; i<=n; i++)
        {
            ///scanf("%d",&sum);
            sum=Scan();
            csum+=sum;
            c[i]=sum;
        }
        for(int i=1; i<=m; i++)
        {
            //scanf("%d",&sum);
            sum=Scan();
            rsum+=sum;
            r[i]=sum;
        }
        printf("Case #%d:",++TOT);
        if(rsum!=csum)
        {
            printf(" So naive!\n");
            continue;
        }
        S=0,T=n+m+1;
        int NodeNum=T+1;
        for(int i=1; i<=n; i++)
        {
            addedge(S,i,c[i]);
        }
        for(int i=1; i<=m; i++)
        {
            addedge(i+n,T,r[i]);
        }
        for(int i=1; i<=n; i++)
        {
            for(int j=1; j<=m; j++)
                addedge(i,j+n,9);
        }
        if(sap(S,T,NodeNum)!=rsum)
        {
            printf(" So naive!\n");
            continue;
        }
        if(check(NodeNum))
            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、付费专栏及课程。

余额充值