hdu 4975 A simple Gaussian elimination problem【最大流Dinic+思维+判环】好题!

本文介绍了一种利用网络流模型解决高斯消元问题的方法。通过构建特定的网络流图,实现对矩阵中缺失的一位数元素进行有效还原。讨论了如何确保解的存在性及其唯一性,并提供了AC代码。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

A simple Gaussian elimination problem.

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

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!

 

 

Author

BJTU

 

 

Source

2014 Multi-University Training Contest 10

 

题目大意:

给你一个n*m矩阵的行和以及列和,让你判断,是否能够通过这个已知条件还原出原来棋盘上的数字。数字范围(0-9)。如果不能,输出So naive,否则继续判断,如果能够还原多种原棋盘,输出So young! 否则输出So simple!


思路:


1、想了半天,最终还是看了题解的提示,做题的时候真的在向图论方向思考了,但是真的没有想到网络流模型。


2、首先我们不难理解:所有行和!=所有列和,那么一定无解。


3、建立网络流模型:

①建立源点S,连入各个行点,其权值为当前行的行和。

②建立汇点T,将各个列点连入汇点T,其权值为当前列的列和。

③将各个行点连入各个汇点T,其权值为9.表示最大可以取值为9、


4、然后跑一遍Dinic求最大流,如果满流,那么说明有解、否则说明无解,那么如果有解又怎样来判断是否多解呢?我们就需要在残余网络中判断是否存在有向环,如果有,那么说明多解,否则说明单解。


Ac代码:


#include<stdio.h>
#include<string.h>
#include<queue>
#include<vector>
using namespace std;
#define INF 0x3f3f3f3f
struct node
{
    int from;
    int to;
    int w;
    int next;
}e[1000005];
int vis[555*555];
int cur[555*555];
int head[555*555];
int row[555];
int col[555];
int divv[555*555];
vector<int >mp[555*555];
int r,c,ss,tt,cont,sum,ok,sum2;
void add(int from,int to,int w)
{
    e[cont].from=from;
    e[cont].to=to;
    e[cont].w=w;
    e[cont].next=head[from];
    head[from]=cont++;
}
void getmap()
{
    ss=r+c+1;
    tt=ss+1;
    cont=0;
    sum=0;
    sum2=0;
    memset(head,-1,sizeof(head));
    for(int i=1;i<=r;i++)
    {
        scanf("%d",&row[i]);
        add(ss,i,row[i]);
        add(i,ss,0);
        sum+=row[i];
    }
    for(int i=1;i<=c;i++)
    {
        scanf("%d",&col[i]);
        add(i+r,tt,col[i]);
        add(tt,i+r,0);
        sum2+=col[i];
    }
    for(int i=1;i<=r;i++)
    {
        for(int j=1;j<=c;j++)
        {
            int u=i,v=j+r;
            add(u,v,9);
            add(v,u,0);
        }
    }
}
int makedivv()
{
    memset(divv,0,sizeof(divv));
    divv[ss]=1;
    queue<int >s;
    s.push(ss);
    while(!s.empty())
    {
        int u=s.front();
        if(u==tt)return 1;
        s.pop();
        for(int i=head[u];i!=-1;i=e[i].next)
        {
            int v=e[i].to;
            int w=e[i].w;
            if(w&&divv[v]==0)
            {
                divv[v]=divv[u]+1;
                s.push(v);
            }
        }
    }
    return 0;
}
int Dfs(int u,int maxflow,int tt)
{
    if(tt==u)return maxflow;
    int ret=0;
    for(int &i=cur[u];i!=-1;i=e[i].next)
    {
        int v=e[i].to;
        int w=e[i].w;
        if(w&&divv[v]==divv[u]+1)
        {
            int f=Dfs(v,min(maxflow-ret,w),tt);
            e[i].w-=f;
            e[i^1].w+=f;
            ret+=f;
            if(ret==maxflow)return ret;
        }
    }
    return ret;
}
int Dinic()
{
    int ans=0;
    while(makedivv()==1)
    {
        memcpy(cur,head,sizeof(head));
        ans+=Dfs(ss,INF,tt);
    }
    return ans;
}
int Dfs_huan(int u,int from)
{
    vis[u]=1;
    for(int i=0;i<mp[u].size();i++)
    {
        int v=mp[u][i];
        if(v==from)continue;
        if(vis[v]==1)return 1;
        if(Dfs_huan(v,u))return 1;
    }
    vis[u]=0;
    return 0;
}
int judge()
{
    for(int i=0;i<=tt;i++)mp[i].clear();
    memset(vis,0,sizeof(vis));
    for(int i=0;i<cont;i++)
    {
        if(e[i].w>0)
        {
            mp[e[i].from].push_back(e[i].to);
        }
    }
    for(int i=1;i<=r;i++)
    {
        if(Dfs_huan(i,-1))return 1;
    }
    return 0;
}
int main()
{
    int t;
    int kase=0;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d%d",&r,&c);
        getmap();
        int tmp=Dinic();
        printf("Case #%d: ",++kase);
        if(sum!=sum2)
        {
            printf("So naive!\n");
            continue;
        }
        if(sum!=tmp)
        {
            printf("So naive!\n");
            continue;
        }
        else
        {
            if(judge())printf("So young!\n");
            else printf("So simple!\n");
        }
    }
}





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值