HDU 4888 Redraw Beautiful Drawings 網路流 建圖,hduredraw

來源:互聯網
上載者:User

HDU 4888 Redraw Beautiful Drawings 網路流 建圖,hduredraw

題意:

給定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 1152921504606846976struct 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 813 163 11 152 4 815 163 11 15 23 4 815 16 104 12 18 63 4 815 16 104 13 18 63 5 816 16 114 13 18 6 23 4 11 3 43 2 1 2*/





聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.