网络流+删边判环hdu4975

F.A.Q
Hand In Hand
Online Acmers
Forum | Discuss
Statistical Charts
Problem Archive
Realtime Judge Status
Authors Ranklist
 
      C/C++/Java Exams     
ACM Steps
Go to Job
Contest LiveCast
ICPC@China
Best Coder beta
VIP | STD Contests
Virtual Contests 
    DIY | Web-DIY beta
Recent Contests
BestCoder官方QQ群:385386683 欢迎加入~
寻人启事:2014级新生看过来!

A simple Gaussian elimination problem.

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


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!
这个题跟hdu4888基本一样的,但是简单的dfs判环会超时,参考了别人的代码,删边判换,就是把容量为0或者已经走过但是没有环的边删掉,避免重复走

#include<iostream>
#include<cstdio>
#include<cstring>
#include<vector>
#include<cmath>
#include<queue>
#include<stack>
#include<map>
#include<set>
#include<algorithm>
using namespace std;
const int maxn=1010;
const int INF=1000000000;
int sumx[maxn/2],sumy[maxn/2];
int n,m,k,nn,st,en;
int vis[maxn],pre[maxn],cnt;
int dis[maxn],gap[maxn],cur[maxn],head[maxn];

struct node
{
    int v,f,next;
}edge[510*500*2];
void add_edge(int x,int y,int f)
{
    edge[cnt].v=y;
    edge[cnt].f=f;
    edge[cnt].next=head[x];
    head[x]=cnt++;
    edge[cnt].v=x;
    edge[cnt].f=0;
    edge[cnt].next=head[y];
    head[y]=cnt++;
}
int SAP(int st,int en)
{
    for(int i=0;i<=nn;i++)
    {
        cur[i]=head[i];
        dis[i]=gap[i]=0;
    }
    int u=0;
    int flow=0,aug=INF;
    gap[st]=nn;
    u=pre[st]=st;
    bool flag;
    while(dis[st]<nn)
    {
        flag=0;
        for(int &j=cur[u];j!=-1;j=edge[j].next)
        {
            int v=edge[j].v;
            if(edge[j].f>0&&dis[u]==dis[v]+1)
            {
                flag=1;
                if(edge[j].f<aug)aug=edge[j].f;
                pre[v]=u;
                u=v;
                if(u==en)
                {
                    flow+=aug;
                    while(u!=st)
                    {
                        u=pre[u];
                        edge[cur[u]].f-=aug;
                        edge[cur[u]^1].f+=aug;
                    }
                    aug=INF;
                }
                break;
            }
        }

        if(flag)continue;
        int mindis=nn;
        for(int j=head[u];j!=-1;j=edge[j].next)
        {
            int v=edge[j].v;
            if(dis[v]<mindis&&edge[j].f>0)
            {
                mindis=dis[v];
                cur[u]=j;
            }
        }
        if((--gap[dis[u]])==0)break;
        gap[dis[u]=mindis+1]++;
        u=pre[u];

    }
    return flow;
}
bool dfs(int u,int fa)
{
    int flag=-1;
    vis[u]=1;
    for(int i=head[u];i!=-1;i=edge[i].next)
    {
        int v=edge[i].v;
        if(v==fa)continue;
        if(edge[i].f>0)
        {
            if(vis[v]||dfs(v,u))return true;
        }

        if(flag==-1)head[u]=edge[i].next;//删边,如果走这条边没有换或者是容量为0,则删除,以后就不用走了
        else edge[flag].next=edge[i].next;
        flag=i;
    }

    vis[u]=0;
    return false;
}
bool findcircle(int fn)
{
    memset(vis,0,sizeof(vis));
    for(int i=1;i<=fn;i++)
    {
        if(!vis[i])
        {
            vis[i]=1;
            if(dfs(i,i))
                return true;
            vis[i]=0;
        }
    }
    return false;
}
int main()
{freopen("in.txt","r",stdin);
    int T,cas=1;
    scanf("%d",&T);
    while(T--)
    {
        scanf("%d%d",&n,&m);
        k=9;
        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];
        }
        printf("Case #%d: ",cas++);
        if(sumx[0]!=sumy[0])
        {
            printf("So naive!\n");
            continue;
        }
        cnt=0;
        memset(head,-1,sizeof(head));
        for(int i=1;i<=n;i++)
            for(int j=1;j<=m;j++)
            add_edge(i,n+j,k);
        int num=cnt;
        for(int i=1;i<=n;i++)
            add_edge(0,i,sumx[i]);
        for(int j=1;j<=m;j++)
            add_edge(n+j,n+m+1,sumy[j]);
        nn=n+m+2;
        st=0,en=n+m+1;//cout<<1;
        int ans=SAP(st,en);//cout<<2;
        if(ans!=sumx[0])
        {
            printf("So naive!\n");
            continue;
        }
        if(findcircle(n))
        {
            printf("So young!\n");
            continue;
        }
        printf("So simple!\n");
    }
    return 0;
}




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值