Redraw Beautiful Drawings(hdu4888)网络流+最大流

Redraw Beautiful Drawings

Time Limit: 3000/1500 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 2909 Accepted Submission(s):
942

Problem Description

Alice and Bob are playing together. Alice is crazy
about art and she has visited many museums around the world. She has a good
memory and she can remember all drawings she has seen.

Today Alice
designs a game using these drawings in her memory. First, she matches K+1 colors
appears in the picture to K+1 different integers(from 0 to K). After that, she
slices the drawing into grids and there are N rows and M columns. Each grid has
an integer on it(from 0 to K) representing the color on the corresponding
position in the original drawing. Alice wants to share the wonderful drawings
with Bob and she tells Bob the size of the drawing, the number of different
colors, and the sum of integers on each row and each column. Bob has to redraw
the drawing with Alice‘s information. Unfortunately, somtimes, the information
Alice offers is wrong because of Alice‘s poor math. And sometimes, Bob can work
out multiple different drawings using the information Alice provides. Bob gets
confused and he needs your help. You have to tell Bob if Alice‘s information is
right and if her information is right you should also tell Bob whether he can
get a unique drawing.

Input

The input contains mutiple testcases.

For each
testcase, the first line contains three integers N(1 ≤ N ≤ 400) , M(1 ≤ M ≤ 400)
and K(1 ≤ K ≤ 40).
N integers are given in the second line representing the
sum of N rows.
M integers are given in the third line representing the sum of
M columns.

The input is terminated by EOF.

Output

For each testcase, if there is no solution for Bob,
output "Impossible" in one line(without the quotation mark); if there is only
one solution for Bob, output "Unique" in one line(without the quotation mark)
and output an N * M matrix in the following N lines representing Bob‘s unique
solution; if there are many ways for Bob to redraw the drawing, output "Not
Unique" in one line(without the quotation mark).

Sample Input

2 2 4

4 2

4 2

4 2 2

2 2 5 0

5 4

1 4 3

9

1 2 3 3

Sample Output

Not Unique

Impossible

Unique

1 2 3 3

题意:在N*M的表格内填入数字,使得每行每列相加等于对应的值,且填入的数字小于K值;

   如果不存在这样的表格,输出Impossible;如果存在多个符合条件的表格,输出Not     Unique,否则,如果表格唯一的话,输出Unique,并输出表格;

思路:网络流可以做,当然网络流+最大流更好!

ps:http://acm.hdu.edu.cn/showproblem.php?pid=4888

网络流(687MS)

#include<iostream>//网络流
#include<cstdio>
#include<cstring>
#include<string>
#include<algorithm>
#include<map>
#include<vector>
#include<queue>
#include<cmath>
#define maxn 1<<29
using namespace std;
struct edge
{
    int from,to,cap,flow;
};
vector<int>g[888];
vector<edge>edges;
int m,n,ma;
bool vis[888];
int d[888];
int cur[888];
int fl[444][444];
bool cc[444][444];
void init()
{
    edges.clear();
    int mm=m+n+1;
    for(int i=0;i<=mm;i++)g[i].clear();
}
void add(int u,int v,int c)
{
    edges.push_back((edge){u,v,c,0});
    g[u].push_back(edges.size()-1);
    edges.push_back((edge){v,u,0,0});
    g[v].push_back(edges.size()-1);
}
bool bfs(int s,int t)
{
    memset(vis,0,sizeof(vis));
    queue<int>q;
    q.push(s);
    d[s]=0;
    vis[s]=1;
    while(!q.empty())
    {
        int u=q.front();
        q.pop();
        int size=g[u].size();
        for(int i=0;i<size;i++)
        {
            edge &e=edges[g[u][i]];
            if(!vis[e.to]&&e.cap>e.flow)
            {
                vis[e.to]=1;
                d[e.to]=d[u]+1;
                q.push(e.to);
            }
        }
    }
    return vis[t];
}
int dfs(int u,int t,int mi)
{
    if(u==t||mi==0)return mi;
    int flow=0,f;
    int size=g[u].size();
    for(int &i=cur[u];i<size;i++)
    {
        edge &e=edges[g[u][i]];
        if(d[u]+1==d[e.to]&&(f=dfs(e.to,t,min(mi,e.cap-e.flow)))>0)
        {
            e.flow+=f;
            edges[g[u][i]^1].flow-=f;
            flow+=f;
            mi-=f;
            if(mi==0)break;
        }
    }
    return flow;
}
int dinic(int s,int t)
{
    int flow=0;
    while(bfs(s,t))
    {
        memset(cur,0,sizeof(cur));
        flow+=dfs(s,t,maxn);
    }
    return flow;
}
bool go()
{
    for(int i=1;i<=n;i++)
    {
        int size=g[i].size();
        for(int j=0;j<size;j++)
        {
            edge &e=edges[g[i][j]];
            if(e.to>n&&e.to<=m+n)
            {
                //cout<<e.from<<" "<<e.to<<" "<<e.flow<<endl;
                fl[i][e.to-n]=e.flow;
            }
        }
    }
    memset(cc,0,sizeof(cc));
    for(int i=1;i<=n;i++)
    {
        for(int j=1;j<=m;j++)
        {
            for(int k=j+1;k<=m;k++)
            {
                bool v1=0,v2=0;
                if(fl[i][j]!=ma&&fl[i][k]!=0)
                {
                    if(cc[k][j])return true;
                    v1=1;
                }
                if(fl[i][j]!=0&&fl[i][k]!=ma)
                {
                    if(cc[j][k])return true;
                    v2=1;
                }
                if(v1)cc[j][k]=1;
                if(v2)cc[k][j]=1;
            }
        }
    }
    return false;
}
int main()
{
    int u,v,c;
    int s1,s2;
    while(scanf("%d%d%d",&n,&m,&ma)!=EOF)
    {
        init();
        s1=s2=0;
        for(int i=1;i<=n;i++)
        {
            scanf("%d",&c);
            add(0,i,c);
            s1+=c;
            for(int j=1;j<=m;j++)
            {
                add(i,n+j,ma);
            }
        }
        for(int i=1;i<=m;i++)
        {
            scanf("%d",&c);
            add(n+i,m+n+1,c);
            s2+=c;
        }
        int ans=dinic(0,m+n+1);
        if(ans!=s1||ans!=s2)printf("Impossible\n");
        else if(go())printf("Not Unique\n");
        else
        {
            printf("Unique\n");
            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;
}

网络流+最大流(234MS)

#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <queue>
#include <numeric>
using namespace std;
typedef long long LL;
const int MAXN = 510;
const int MAXV = MAXN << 1;
const int MAXE = 2 * MAXN * MAXN;
const int INF = 0x3f3f3f3f;
struct ISAP
{
    int head[MAXV], cur[MAXV], gap[MAXV], dis[MAXV], pre[MAXV];
    int to[MAXE], next[MAXE], flow[MAXE];
    int n, ecnt, st, ed;
    void init(int n)
    {
        this->n = n;
        memset(head + 1, -1, n * sizeof(int));
        ecnt = 0;
    }
    void add_edge(int u, int v, int c)
    {
        to[ecnt] = v;
        flow[ecnt] = c;
        next[ecnt] = head[u];
        head[u] = ecnt++;
        to[ecnt] = u;
        flow[ecnt] = 0;
        next[ecnt] = head[v];
        head[v] = ecnt++;

    }
    void bfs()
    {
        memset(dis + 1, 0x3f, n * sizeof(int));
        queue<int> que;
        que.push(ed);
        dis[ed] = 0;
        while(!que.empty())
        {
            int u = que.front();
            que.pop();
            gap[dis[u]]++;
            for(int p = head[u]; ~p; p = next[p])
            {
                int v = to[p];
                if(flow[p ^ 1] && dis[u] + 1 < dis[v])
                {
                    dis[v] = dis[u] + 1;
                    que.push(v);
                }
            }
        }
    }  int max_flow(int ss, int tt)
    {
        st = ss, ed = tt;
        int ans = 0, minFlow = INF;
        for(int i = 0; i <= n; ++i)
        {
            cur[i] = head[i];
            gap[i] = 0;

        }
        bfs();
        int u = pre[st] = st;
        while(dis[st] < n)
        {
            bool flag = false;
            for(int &p = cur[u]; ~p; p = next[p])
            {
                int v = to[p];
                if(flow[p] && dis[u] == dis[v] + 1)
                {
                    flag = true;
                    minFlow = min(minFlow, flow[p]);
                    pre[v] = u;
                    u = v;
                    if(u == ed)
                    {
                        ans += minFlow;
                        while(u != st)
                        {
                            u = pre[u];
                            flow[cur[u]] -= minFlow;
                            flow[cur[u] ^ 1] += minFlow;

                        }
                        minFlow = INF;

                    }
                    break;

                }

            }
            if(flag) continue;
            int minDis = n - 1;
            for(int p = head[u]; ~p; p = next[p])
            {
                int &v = to[p];
                if(flow[p] && dis[v] < minDis)
                {
                    minDis = dis[v];
                    cur[u] = p;

                }
            }
            if(--gap[dis[u]] == 0) break;
            ++gap[dis[u] = minDis + 1];
            u = pre[u];

        }
        return ans;

    } int stk[MAXV], top;
    bool sccno[MAXV], vis[MAXV];
    bool dfs(int u, int f, bool flag)
    {
        vis[u] = true;
        stk[top++] = u;
        for(int p = head[u]; ~p; p = next[p]) if(flow[p])
            {
                int v = to[p];
                if(v == f) continue;
                if(!vis[v])
                {
                    if(dfs(v, u, flow[p ^ 1])) return true;

                }
                else if(!sccno[v]) return true;

            }
        if(!flag)
        {
            while(true)
            {
                int x = stk[--top];
                sccno[x] = true;
                if(x == u) break;

            }

        }
        return false;

    }
    bool acycle()
    {
        memset(sccno + 1, 0, n * sizeof(bool));
        memset(vis + 1, 0, n * sizeof(bool));
        top = 0;
        return dfs(ed, 0, 0);

    }
} G;
int row[MAXN], col[MAXN];
int mat[MAXN][MAXN];
int n, m, k, ss, tt;
void solve()
{
    int sumr = accumulate(row + 1, row + n + 1, 0);
    int sumc = accumulate(col + 1, col + m + 1, 0);
    if(sumr != sumc)
    {
        puts("Impossible");
        return ;

    }
    int res = G.max_flow(ss, tt);
    if(res != sumc)
    {
        puts("Impossible");
        return ;

    }
    if(G.acycle())
    {
        puts("Not Unique");

    }
    else
    {
        puts("Unique");
        for(int i = 1; i <= n; ++i)
        {
            for(int j = 1; j < m; ++j) printf("%d ", G.flow[mat[i][j]]);
            printf("%d\n", G.flow[mat[i][m]]);

        }

    }

}
int main()
{
    while(scanf("%d%d%d", &n, &m, &k) != EOF)
    {
        for(int i = 1; i <= n; ++i) scanf("%d", &row[i]);
        for(int i = 1; i <= m; ++i) scanf("%d", &col[i]);
        ss = n + m + 1, tt = n + m + 2;
        G.init(tt);
        for(int i = 1; i <= n; ++i) G.add_edge(ss, i, row[i]);
        for(int i = 1; i <= m; ++i) G.add_edge(n + i, tt, col[i]);
        for(int i = 1; i <= n; ++i)
        {
            for(int j = 1; j <= m; ++j)
            {
                mat[i][j] = G.ecnt ^ 1;
                G.add_edge(i, n + j, k);

            }
        }
        solve();

    }
}

这类题目,这个作为模版哦!

类似的题目有hdu4975;解题报告;寻找&星空の孩子

时间: 2024-10-18 03:35:40

Redraw Beautiful Drawings(hdu4888)网络流+最大流的相关文章

HDU 4888 (杭电多校#3)Redraw Beautiful Drawings(网络流之最大流)

题目地址:HDU 4888 自己之所以弱真心是态度的问题,以后不能再偷懒了!!那次这个题一直没补,结果这次又遇到了..还有这次遇到的最小割权闭合问题,也一直没刷,所以这次遇到了也不会,连是最小割都不知道!!(突然想起来前面还有好多题拖到现在也没做...T U T)以后绝不能再拖拉了! 这题的建图是很容易的,主要是判断唯一性不好判断.这里是用的dfs找环来判断是否唯一,因为假如有环的话,说明环 中的数字是可以相互流动而且可以保证解依然正确.. 代码如下: #include <cstdio> #i

【多校赛第三场】Redraw Beautiful Drawings【网络流】【谜のWA】

参考题解:http://blog.csdn.net/qian99/article/details/38276887 #include <iostream> #include <cstdio> #include <cstdlib> #include <cmath> #include <cstring> #include <queue> #include <vector> #include <algorithm>

HDU 4888 Redraw Beautiful Drawings(网络流求矩阵的解)

论文<为什么很多网络流问题总有整数解>http://diaorui.net/archives/189: 参考:http://www.cnblogs.com/yuiffy/p/3929369.html 题意:n*m的矩阵,给出每行的和以及每列的和,判断这样的矩阵是否存在,若存在,是否唯一:若唯一,输出解: 思路:网络流,最大流+判环.网络流常用于求多项式整数解. #include<cstdio> #include<cstring> #include<algorith

hdoj 4888 Redraw Beautiful Drawings 【最大流满流+唯一性判断】

题目:hdoj 4888 Redraw Beautiful Drawings 分类:最大流满流 , 最大流唯一性 来源:2014 Multi-University Training Contest 3 题意:一个矩阵的每行每列的和都知道,然后让你求能不能填,是否唯一,唯一的话输出解. 分析:这个题目能看出来是最大流,但是难点有2. 首先:题中矩阵400 * 400 ,好在题目中矩阵和没有特殊要求,所以可以直接以行列建图,建图方案: 1. 源点 -> 每一行对应的点,流量限制为该行的和 2. 每一

hdu4888 Redraw Beautiful Drawings 最大流+判环

hdu4888 Redraw Beautiful Drawings Time Limit: 3000/1500 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 2007    Accepted Submission(s): 447 Problem Description Alice and Bob are playing together. Alice is crazy abou

HDU4888 Redraw Beautiful Drawings(2014 Multi-University Training Contest 3)

Redraw Beautiful Drawings Time Limit: 3000/1500 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Problem Description Alice and Bob are playing together. Alice is crazy about art and she has visited many museums around the world. She has

HDU 4888 Redraw Beautiful Drawings(最大流+判最大流网络是否唯一)

Problem Description Alice and Bob are playing together. Alice is crazy about art and she has visited many museums around the world. She has a good memory and she can remember all drawings she has seen. Today Alice designs a game using these drawings

【HDU】4888 Redraw Beautiful Drawings 网络流【推断解是否唯一】

传送门:pid=4888">[HDU]4888 Redraw Beautiful Drawings 题目分析: 比赛的时候看出是个网络流,可是没有敲出来.各种反面样例推倒自己(究其原因是不愿意写暴力推断的).. 首先是简单的行列建边.源点向行建边.容量为该行元素和,汇点和列建边.容量为该列元素和.全部的行向全部的列建边,容量为K. 跑一次最大流.满流则有解,否则无解. 接下来是推断解是否唯一. 这个题解压根没看懂.还是暴力大法好. 最简单的思想就是枚举在一个矩形的四个端点.设A.D为主对角

HDU 4888 Redraw Beautiful Drawings 网络流 建图

题意: 给定n, m, k 下面n个整数 a[n] 下面m个整数 b[n] 用数字[0,k]构造一个n*m的矩阵 若有唯一解则输出这个矩阵,若有多解输出Not Unique,若无解输出Impossible 思路:网络流,,, n行当成n个点,m列当成m个点 从行-列连一条流量为k的边,然后源点-行连一条a[i]的边, 列-汇点 流量为b[i] 瞎了,该退役了 T^T #include<stdio.h> #include<string.h> #include<iostream&