您的位置:首页 > 理论基础 > 计算机网络

HDU 4975 A simple Gaussian elimination problem 网络流

2014-08-21 19:56 417 查看


A simple Gaussian elimination problem.

Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)

Total Submission(s): 181 Accepted Submission(s): 65



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



别问我n=min(n,200);

m=min(m,200);是怎么来的。。

#include <cstdlib>
#include <cctype>
#include <cstring>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <vector>
#include <string>
#include <iostream>
#include <sstream>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <fstream>
#include <numeric>
#include <iomanip>
#include <bitset>
#include <list>
#include <stdexcept>
#include <functional>
#include <utility>
#include <ctime>
using namespace std;

const int INF=0x3f3f3f3f;

int n,m,k;

const int mn=1200;

const int mm=mn*mn*5;

int node,s,t,edge;

int ver[mm],flow[mm],next[mm];

int head[mn],work[mn],dis[mn],q[mn];

int sumx[mn],sumy[mn];

int fl[mn][mn];
int vis[mn][mn];

void init(int _node,int _s,int _t)
{
    node=_node, s=_s, t=_t;
    for(int i=0;i<node;++i)
        head[i]=-1;
    edge=0;
}

void addedge(int u,int v,int c)
{
    ver[edge]=v,flow[edge]=c,next[edge]=head[u],head[u]=edge++;
    ver[edge]=u,flow[edge]=0,next[edge]=head[v],head[v]=edge++;
}

bool Dinic_bfs()
{
    int i,u,v,l,r=0;
    for(i=0;i<node;++i)  dis[i]=-1;
    dis[ q[r++]=s ] = 0;
    for(l=0;l<r;l++)
    {
        for(i=head[ u=q[l] ]; ~i ;i=next[i])
            if(flow[i] && dis[ v=ver[i] ]<0)
             {
                 dis[ q[r++]=v ]=dis[u]+1;
                 if(v==t) return 1;
             }
    }
    return 0;
}

int Dinic_dfs(int u,int exp)
{
    if(u==t) return exp;
    for(int &i=work[u],v,temp; ~i ;i=next[i])
    {
        if(flow[i] && dis[ v=ver[i] ]==dis[u]+1 && ( temp=Dinic_dfs(v,min(exp,flow[i])) )>0)
        {
            flow[i]-=temp;
            flow[i^1]+=temp;
            return temp;
        }
    }
    return 0;
}

int Dinic_flow()
{
    int ans=0,res,i;
    while(Dinic_bfs())
    {
        for(i=0;i<node;++i) work[i]=head[i];
        while( res=Dinic_dfs(s,INF) )  ans+=res;
    }
    return ans;
}

bool check()
{
    for(int u=1;u<=n;u++)
    {
        for(int i=head[u];~i;i=next[i])
        {
            int v=ver[i];
            if(v>n && v<=n+m)
            {
                fl[u][v-n]=k-flow[i];
            }
        }
    }
    n=min(n,200);
    m=min(m,200);
    memset(vis,0,sizeof(vis));
    for(int i=1;i<=n;i++)
    {
        for(int j=1;j<=m;j++)
        {
            for(int p=j+1;p<=m;p++)
            {
                bool v1=0,v2=0;
                if(fl[i][j]!=k && fl[i][p]!=0)
                {
                    if(vis[p][j])
                        return true;
                    v1=1;
                }
                if(fl[i][j]!=0 && fl[i][p]!=k)
                {
                    if(vis[j][p])
                        return true;
                    v2=1;
                }
                if(v1) vis[j][p]=1;
                if(v2) vis[p][j]=1;
            }
        }
    }
    return false;
}

int main()
{
    int cas=1;
    k=9;
    int T;
    cin>>T;
    while(T--)
    {
        scanf("%d%d",&n,&m);
        init(n+m+2,0,n+m+1);
        memset(sumx,0,sizeof(sumx));
        memset(sumy,0,sizeof(sumy));
        for(int i=1;i<=n;i++)
        {
            scanf("%d",&sumx[i]);
            sumx[0]+=sumx[i];
        }
        for(int i=1;i<=m;i++)
        {
            scanf("%d",&sumy[i]);
            sumy[0]+=sumy[i];
        }
        if(sumx[0]!=sumy[0])
        {
            printf("Case #%d: So naive!\n",cas++);
            continue;
        }

        for(int i=1;i<=n;i++)
        {
            addedge(s,i,sumx[i]);
            for(int j=1;j<=m;j++)
            {
                addedge(i,n+j,k);
            }
        }
        for(int j=1;j<=m;j++)
            addedge(n+j,t,sumy[j]);
        int ret=Dinic_flow();

        //cout<<ret<<endl;

        if(ret!=sumx[0] || ret!=sumy[0])
        {
            printf("Case #%d: So naive!\n",cas++);
            continue;
        }
        else
        {
            if(check())
            {
                printf("Case #%d: So young!\n",cas++);
                continue;
            }
            else
            {
                printf("Case #%d: So simple!\n",cas++);
                /*for(int i=1;i<=n;i++)
                {
                   for(int j=1;j<=m;j++)
                   {
                     printf("%d",fl[i][j]);
                     if(j==m)
                        printf("\n");
                     else
                        printf(" ");
                   }
                }*/
            }
        }

    }
    return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: