HDU 4975 A simple Gaussian elimination 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!

题目分析:

m*n的矩阵(0~9组成),知道每行每列的和,求这个矩阵。
其中: 若有唯一的解输出 So simple;无解输出 So naive;有多个可行解输出So young。
设源点s,汇点t,将行之和与源点s建边,列之和与汇点t建边,并将行和与列和建边,流为9(因为每个单元最大数是9)。
用dinic跑网络流,满流有解,不满流无解。判定残余网络是否有环,有环则有多解。

代码如下:

#include <stdio.h>
#include <string.h>
#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
#include <stack>
#include <set>
#include <map>
#include <string>
#include <math.h>
#include <stdlib.h>
#include <time.h>
using namespace std;
typedef long long ll;
const int INF = 0x3f3f3f3f;
const double eps = 1e-4;
const int MAXN=2010;

struct node
{
    int to,flow,next;
}mp[MAXN<<5];

int head[MAXN];
int id;
int level[MAXN];
int q[MAXN];
void init()
{
    memset(head,-1,sizeof(head));
    id=0;
}

void addedge(int u, int v, int flow)
{
    mp[id].to=v;
    mp[id].flow=flow;
    mp[id].next=head[u];
    head[u]=id++;

    mp[id].to=u;
    mp[id].flow=0;
    mp[id].next=head[v];
    head[v]=id++;
}

int bfs(int s, int t) //构建层次网络
{
    memset(level,0,sizeof(level));
    level[s]=1;
    int front=0,rear=1;
    q[front]=s;
    while(front < rear)
    {
        int x=q[front++];
        if(x==t) return 1;
        for(int e=head[x]; e!=-1; e=mp[e].next)
        {
            int v=mp[e].to, f=mp[e].flow;
            if(!level[v] && f)
            {
                level[v]=level[x]+1;
                q[rear++]=v;
            }
        }
    }
    return 0;
}

int dfs(int u,int maxf,int t)
{
    if(u==t) return maxf;
    int ret=0;
    for(int e=head[u]; e!=-1; e=mp[e].next)
    {
        int v=mp[e].to, f=mp[e].flow;
        if(level[u]+1==level[v] && f)
        {
            int Min=min(maxf-ret,f);
            f=dfs(v,Min,t);
            mp[e].flow-=f;
            mp[e^1].flow+=f;
            ret+=f;
            if(ret==maxf) return ret;
        }
    }
    return ret;
}

int dinic(int s, int t) //s源点 t汇点
{
    int ans = 0;
    while(bfs(s,t))
       ans+=dfs(s,INF,t);
    return ans;
}

int vis[MAXN];
bool dfs2(int s,int t)
{
    for(int i=head[s]; i!=-1; i=mp[i].next)
    {
        if ((i^1)!=t)
        {
            if (vis[mp[i].to]==-1)  return true;
            vis[mp[i].to]=-1;
            if (dfs2(mp[i].to,i))    return true;
            vis[mp[i].to]=0;
        }
    }
    return false;
}

int main()
{
    int T;
    scanf("%d",&T);
    int m,n,s,t;
    for(int tt=1; tt<=T; tt++)
    {
        scanf("%d%d",&n,&m);
        s=0;t=m+n+1;
        int sr=0,sc=0;
        for(int i=1; i<=n; i++)
        {
            int x;
            scanf("%d",&x);
            addedge(s,i,x);
            sr+=x;
        }
        for(int i=1; i<=m; i++)
        {
            int x;
            scanf("%d",&x);
            addedge(i+n,t,x);
            sc+=x;
        }
        if (sr!=sc)
        {
            printf("Case #%d: So naive!\n",tt);
            continue;
        }
        for(int i=1; i<=n; i++)
        {
            for(int j=1; j<=m; j++)
            {
                addedge(i,j+n,9);
            }
        }
        int ans=dinic(s,t);
        if (ans!=sc || ans!=sr)
        {
            printf("Case #%d: So naive!\n",tt);
            continue;
        }
        init();
        memset(vis,0,sizeof(vis));
        bool flag=false;
        for (int i=1; i<=n; i++)
        {
            if (vis[i]==0 && dfs2(i,-1))
            {
                flag=true;
                break;
            }
        }
        if (flag)
        {
            printf("Case #%d: So young!\n",tt);
        }
        else
        {
            printf("Case #%d: So simple!\n",tt);
        }
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值