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>
#include<math.h>
#include<algorithm>
#include<queue>
#include<vector>
using namespace std;

#define ll int

#define N 1005
#define M 200000
#define inf 107374182
#define inf64 1152921504606846976
struct Edge{
    ll from, to, cap, nex;
}edge[M*2];//注意这个一定要够大 不然会re 还有反向弧

ll head[N], edgenum;
void add(ll u, ll v, ll cap, ll rw = 0){ //如果是有向边则:add(u,v,cap); 如果是无向边则:add(u,v,cap,cap);
    Edge E = { u, v, cap, head[u]};
    edge[ edgenum ] = E;
    head[u] = edgenum ++;

    Edge E2= { v, u, rw,  head[v]};
    edge[ edgenum ] = E2;
    head[v] = edgenum ++;
}
ll sign[N];
bool BFS(ll from, ll to){
    memset(sign, -1, sizeof(sign));
    sign[from] = 0;

    queue<ll>q;
    q.push(from);
    while( !q.empty() ){
        ll u = q.front(); q.pop();
        for(ll i = head[u]; i!=-1; i = edge[i].nex)
        {
            ll v = edge[i].to;
            if(sign[v]==-1 && edge[i].cap)
            {
                sign[v] = sign[u] + 1, q.push(v);
                if(sign[to] != -1)return true;
            }
        }
    }
    return false;
}
ll Stack[N], top, cur[N];
ll Dinic(ll from, ll to){
    ll ans = 0;
    while( BFS(from, to) )
    {
        memcpy(cur, head, sizeof(head));
        ll u = from;      top = 0;
        while(1)
        {
            if(u == to)
            {
                ll flow = inf, loc;//loc 表示 Stack 中 cap 最小的边
                for(ll i = 0; i < top; i++)
                    if(flow > edge[ Stack[i] ].cap)
                    {
                        flow = edge[Stack[i]].cap;
                        loc = i;
                    }

                    for(ll i = 0; i < top; i++)
                    {
                        edge[ Stack[i] ].cap -= flow;
                        edge[Stack[i]^1].cap += flow;
                    }
                    ans += flow;
                    top = loc;
                    u = edge[Stack[top]].from;
            }
            for(ll i = cur[u]; i!=-1; cur[u] = i = edge[i].nex)//cur[u] 表示u所在能增广的边的下标
                if(edge[i].cap && (sign[u] + 1 == sign[ edge[i].to ]))break;
            if(cur[u] != -1)
            {
                Stack[top++] = cur[u];
                u = edge[ cur[u] ].to;
            }
            else
            {
                if( top == 0 )break;
                sign[u] = -1;
                u = edge[ Stack[--top] ].from;
            }
        }
    }
    return ans;
}
void init(){memset(head,-1,sizeof head);edgenum = 0;}


int n, m, k;
int a[500], b[500], suma, sumb;
int mp[505][505], dou[505][505]; //dou[0][i] i这列存在一个可增的点
int hehe(){
    if(suma != sumb)return -1;
    init();
    int from = 0, to = n+m + 10;
    for(int i = 1; i <= n; i++)
        for(int j = 1; j <= m; j++)
            add(i, n+j, k);
    for(int i = 1; i <= n; i++)
        add(from, i, a[i]);
    for(int i = 1; i <= m; i++)
        add(n+i, to, b[i]);
    int flow = Dinic(from, to);
    if(flow != suma) return -1;
    int tt = 1;
    for(int i = 1; i <= n; i++)for(int j = 1; j <= m; j++, tt+=2)
        mp[i][j] = edge[tt].cap;
    memset(dou, 0, sizeof dou);
    for(int i = 1; i <= n; i++)
    {
        for(int j = 1; j <= m; j++)
            for(int z = j+1; z <= m; z++)
            {
                bool v1=0,v2=0;
                if(mp[i][j]!=k&&mp[i][z]!=0)
                {
                    if(dou[z][j])return 0;
                    v1=1;
                }
                if(mp[i][j]!=0&&mp[i][z]!=k)
                {
                    if(dou[j][z])return 0;
                    v2=1;
                }
                if(v1)dou[j][z]=1;
                if(v2)dou[z][j]=1;
            }

    }
    return 1;
}
void input(){
    suma = sumb = 0;
    for(int i = 1; i <= n; i++)scanf("%d",&a[i]), suma += a[i];
    for(int i = 1; i <= m; i++)scanf("%d",&b[i]), sumb += b[i];
}
int main(){
    int u, v, i, j;
    while(~scanf("%d %d %d",&n,&m,&k)) {
        input();
        int ans = hehe();
        if(ans == -1)puts("Impossible");
        else if(ans == 0)puts("Not Unique");
        else
        {
            puts("Unique");
            for(i = 1; i <= n; i++)
                for(j = 1; j <= m; j++)
                    printf("%d%c",mp[i][j],j==m?'\n':' ');
        }
    }
    return 0;
}
/*
2 3 8
13 16
3 11 15

2 4 8
15 16
3 11 15 2

3 4 8
15 16 10
4 12 18 6

3 4 8
15 16 10
4 13 18 6

3 5 8
16 16 11
4 13 18 6 2

3 4 1
1 3 4
3 2 1 2

*/


HDU 4888 Redraw Beautiful Drawings 网络流 建图,布布扣,bubuko.com

HDU 4888 Redraw Beautiful Drawings 网络流 建图

上一篇:PS鼠绘超酷的舞台聚光灯教程


下一篇:【转载】错误 CS0016: 未能写入输出文件“c:/WINDOWS/Microsoft.NET/Framework/v2.0.50727/Temporary ASP.NET Files/.........dll”--“拒绝访问。 ”