hdu4888 Redraw Beautiful Drawings

14更多学校的第二个问题

网络流量   分别以行,列作为结点建图

i行表示的结点到j列表示的结点的流量便是(i, j)的值

跑遍最大流   若满流了便是有解   推断是否unique  就是在残余网络中dfs。走能够添加流量的边,找到环即不唯一

dfs的时候一定要回溯!!

。。

#include <cstdio>
#include <ctime>
#include <cstdlib>
#include <cstring>
#include <queue>
#include <string>
#include <set>
#include <stack>
#include <map>
#include <cmath>
#include <vector>
#include <iostream>
#include <algorithm>
#include <bitset>
#include <fstream>
using namespace std;

//LOOP
#define FF(i, a, b) for(int i = (a); i < (b); ++i)
#define FE(i, a, b) for(int i = (a); i <= (b); ++i)
#define FED(i, b, a) for(int i = (b); i>= (a); --i)
#define REP(i, N) for(int i = 0; i < (N); ++i)
#define CLR(A,value) memset(A,value,sizeof(A))
#define FC(it, c) for(__typeof((c).begin()) it = (c).begin(); it != (c).end(); it++)

//OTHER
#define SZ(V) (int)V.size()
#define PB push_back
#define MP make_pair
#define all(x) (x).begin(),(x).end()

//INPUT
#define RI(n) scanf("%d", &n)
#define RII(n, m) scanf("%d%d", &n, &m)
#define RIII(n, m, k) scanf("%d%d%d", &n, &m, &k)
#define RIV(n, m, k, p) scanf("%d%d%d%d", &n, &m, &k, &p)
#define RV(n, m, k, p, q) scanf("%d%d%d%d%d", &n, &m, &k, &p, &q)
#define RS(s) scanf("%s", s)

//OUTPUT
#define WI(n) printf("%d\n", n)
#define WS(n) printf("%s\n", n)

//debug
//#define online_judge
#ifndef online_judge
#define dt(a)  << (#a) << "=" << a << " "
#define debugI(a) cout dt(a) << endl
#define debugII(a, b) cout dt(a) dt(b) << endl
#define debugIII(a, b, c) cout dt(a) dt(b) dt(c) << endl
#define debugIV(a, b, c, d) cout dt(a) dt(b) dt(c) dt(d) << endl
#define debugV(a, b, c, d, e) cout dt(a) dt(b) dt(c) dt(d) dt(e) << endl
#else
#define debugI(v)
#define debugII(a, b)
#define debugIII(a, b, c)
#define debugIV(a, b, c, d)
#endif

#define sqr(x) (x) * (x)
typedef long long LL;
typedef unsigned long long ULL;
typedef vector <int> VI;
const double eps = 1e-9;
const int MOD = 1000000007;
const double PI = acos(-1.0);
const int INF = 0x3f3f3f3f;
const int maxn = 900;

bool use[maxn];
struct Edge{
    int from, to, cap, flow;
};
int MAX;

struct Dinic{
    int n, m ,s, t;
    vector<Edge> edges;
    VI G[maxn];
    bool vis[maxn];
    int d[maxn];
    int cur[maxn]   ;

    void init(int nn)
    {
        this->n = nn;
        REP(i, n) G[i].clear();
        edges.clear();
    }

    void addEdge(int from, int to, int cap)
    {
        edges.PB((Edge){from, to, cap, 0});
        edges.PB((Edge){to, from, 0, 0});
        m = edges.size();
        G[from].PB(m - 2);
        G[to].PB(m - 1);
    }

    bool bfs()
    {
        CLR(vis, 0);
        queue<int> Q;
        Q.push(s);
        d[s] = 0;
        vis[s] = 1;
        while (!Q.empty())
        {
            int x = Q.front();
            Q.pop();
            REP(i, G[x].size())
            {
                Edge& e = edges[G[x][i]];
                if (!vis[e.to] && e.cap > e.flow)
                {
                    vis[e.to] = 1;
                    d[e.to] = d[x] + 1;
                    Q.push(e.to);
                }
            }
        }
        return vis[t];
    }

    int dfs(int x, int a)
    {
        if (x == t || a == 0)   return a;
        int flow = 0, f;
        for (int& i = cur[x]; i < G[x].size(); i++)
        {
            Edge& e = edges[G[x][i]];
            if (d[x] + 1 == d[e.to] && (f = dfs(e.to, min(a, e.cap - e.flow))) > 0)
            {
                e.flow += f;
                edges[G[x][i] ^ 1].flow -= f;
                flow += f;
                a -= f;
                if (a == 0) break;
            }
        }
        return flow;
    }

    int maxflow(int s, int t)
    {
        this-> s = s, this-> t = t;
        int flow = 0;
        while (bfs())
        {
            CLR(cur, 0);
            flow += dfs(s, INF);
        }
        return flow;
    }

    bool visit(int u, int fa)
    {
        if (u == 0 || u == MAX) return false;
        use[u] = 1;
        REP(i, G[u].size())
        {
            Edge& e = edges[G[u][i]];
//            debugII(e.to, use[e.to]);
            if (e.to != fa && e.cap > e.flow)
                if (use[e.to] || visit(e.to, u))
                    return true;
        }
        use[u] = 0;
        return false;
    }

}di;

int main()
{
    int n, m, k;
    while (~RIII(n, m, k))
    {
        int x, sum1 = 0, sum2 = 0;
        MAX = n + m + 1;
        di.init(n + m + 2);
        FE(i, 1, n)
        {
            RI(x);
            sum1 += x;
            di.addEdge(0, i, x);
        }
        FE(i, n + 1, n + m)
        {
            RI(x);
            sum2 += x;
            di.addEdge(i, n + m + 1, x);
        }
        FE(i, 1, n)
            FE(j, n + 1, n + m)
                di.addEdge(i, j, k);
        if (sum2 != sum1)
        {
            puts("Impossible");
            continue;
        }
        int ans = di.maxflow(0, n + m + 1);
        if (ans < sum1)
        {
            puts("Impossible");
            continue;
        }
        FE(i, 1, n)
        {
            CLR(use, 0);
            if (di.visit(i, -1))
            {
                puts("Not Unique");
                goto end;
            }
        }
        puts("Unique");
        for (int i = 2 * n + 2 * m; i < di.edges.size(); i += 2)
        {
            printf("%d", di.edges[i].flow);
            if (i + 2 >= di.edges.size())
                printf("\n");
            else
                printf(" ");
        }
        end:;
    }
    return 0;
}

版权声明:本文博主原创文章。博客,未经同意不得转载。

时间: 2024-10-25 04:36:49

hdu4888 Redraw Beautiful Drawings的相关文章

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

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(最大流唯一性判定:残量网络删边判环)

题目 Source http://acm.hdu.edu.cn/showproblem.php?pid=4888 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.

hdu4888 Redraw Beautiful Drawings(最大流)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4888 题意:给一个矩阵没行的和和每列的和,问能否还原矩阵,如果可以还原解是否唯一,若唯一输出该矩阵. 思路:设一个源点和汇点,每行的和和源点加边,权值为该行的和,每列的和和汇点加点,权值为该列的和. 每行和每列加边, 权值为k,跑最大流,如果满流(从源点流出的等于流入汇点的)则证明可以还原该矩阵.   判断是否有多节,就dfs搜,看残余网络中是否有环,无环则解唯一. AC代码: #include<i

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 h

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&

HDU Redraw Beautiful Drawings 推断最大流是否唯一解

点击打开链接 Redraw Beautiful Drawings Time Limit: 3000/1500 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 1660    Accepted Submission(s): 357 Problem Description Alice and Bob are playing together. Alice is crazy about

HDOJ 4888 Redraw Beautiful Drawings

最大流判断多解 建图: 源点连接到每一个代表行的节点容量为行总和,每一个代表列的节点连接到汇点容量为列总和,行和列之间互相连接容量为Limit 多解: 做一遍ISAP后,在残量图上DFS看能否找到点数大于2的环即可 Redraw Beautiful Drawings Time Limit: 3000/1500 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 3519    Acc

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

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