hdu4975(最大流判环)

202 篇文章 0 订阅
123 篇文章 0 订阅

A simple Gaussian elimination problem.

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


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!



题意:给定一个n*m的棋盘,给出每行的和以及每列的和,问是否可以确定出该棋盘(唯一,多解or无解)。

思路:源点连行,行连列,列连汇点,行连列之间的权值是9(题目说在0到9之间,所以就用9,表示流量的最大值是9),然后最大流是满流就有解,图里面存在环就有多解,因为有了环之后就能在环上进行流量的调节了。


#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include<string.h>
#include<algorithm>
#include<math.h>
#include<queue>
using namespace std;
typedef long long ll;
const   int oo=1e9;
/**oo 表示无穷大*/
const  int mm=111111111;
/**mm 表示边的最大数量,记住要是原图的两倍,在加边的时候都是双向的*/
const  int mn=2010;
/**mn 表示点的最大数量*/
int node,src,dest,edge;
/**node 表示节点数,src 表示源点,dest 表示汇点,edge 统计边数*/
int ver[mm],flow[mm],nex[mm];
int head[mn],work[mn],dis[mn],q[mn];
void prepare(int _node, int _src,int _dest)
{
    node=_node,src=_src,dest=_dest;
    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,nex[edge]=head[u],head[u]=edge++;
    ver[edge]=u,flow[edge]=0,nex[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++]=src]=0;
    for(l=0; l<r; ++l)
        for(i=head[u=q[l]]; i>=0; i=nex[i])
            if(flow[i]&&dis[v=ver[i]]<0)
            {
             dis[q[r++]=v]=dis[u]+1;
                if(v==dest)  return 1;
            }
    return 0;
}
int Dinic_dfs(  int u, int exp)
{
    if(u==dest)  return exp;
    for(  int &i=work[u],v,tmp; i>=0; i=nex[i])
        if(flow[i]&&dis[v=ver[i]]==dis[u]+1&&(tmp=Dinic_dfs(v,min(exp,flow[i])))>0)
        {
            flow[i]-=tmp;
            flow[i^1]+=tmp;
            return tmp;
        }
    return 0;
}

int Dinic_flow()
{
    int i,ret=0,delta;
    while(Dinic_bfs())
    {
        for(i=0; i<node; ++i)work[i]=head[i];
        while((delta=Dinic_dfs(src,oo)))ret+=delta;
    }
    return ret;
}

int walk[1010];
int dfs(int now,int pre)
{
    int bb=-1;
    walk[now]=1;
    for(int i=head[now];i!=-1;i=nex[i])
    {
        int v=ver[i];
        if(v==pre)continue;
        if(flow[i]>0)
        {
            if(walk[v])return 1;
            if(dfs(v,now))return 1;
        }
        if(bb==-1)head[now]=nex[i];
        else nex[bb]=nex[i];
        bb=i;
    }
    walk[now]=0;
    return 0;
}

bool judge()
{
    memset(walk,0,sizeof(walk));
    for(int i=1;i<=node;i++)
    if(dfs(i,-1))
        return 1;
    return 0;
}

int main()
{
    int t,o=1;
    scanf("%d",&t);
    while(t--)
    {
        int m,n;
        scanf("%d%d",&n,&m);
        prepare(m+n+2,0,m+n+1);
        for(int i=1;i<=n;i++)
            for(int j=1;j<=m;j++)
            addedge(i,j+n,9);
        int sum1=0,sum2=0;
        for(int i=1;i<=n;i++)
        {
            int w;scanf("%d",&w);
            sum1+=w;
            addedge(0,i,w);
        }
         for(int i=1;i<=m;i++)
        {
            int w;scanf("%d",&w);
            sum2+=w;
            addedge(i+n,m+n+1,w);
        }
        printf("Case #%d: ",o++);
        if(sum1!=sum2)
        {
            printf("So naive!\n");
            continue;
        }
        if(Dinic_flow()!=sum1)
            printf("So naive!\n");
        else
        {
            if(judge())
            printf("So young!\n");
        else printf("So simple!\n");
        }
    }
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值