线性规划与网络流24题 07试题库问题

这个题多解。。。。。


#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
#define inf 1<<30
#define M 1200
#define N 100000
#define cc(m,v) memset(m,v,sizeof(m))

struct node {
    int u, v, f, next;
} edge[N];
int head[M], p, lev[M], cur[M];
int que[N];

void ainit() {
    p = 0, cc(head, -1);
}

void addedge(int u, int v, int f) {
    edge[p].u = u, edge[p].v = v, edge[p].f = f, edge[p].next = head[u], head[u] = p++;
    edge[p].u = v, edge[p].v = u, edge[p].f = 0, edge[p].next = head[v], head[v] = p++;
}

bool bfs(int s, int t) {
    int i, u, v, qin = 0, qout = 0;
    cc(lev, 0), lev[s] = 1, que[qin++] = s;
    while (qout != qin) {
        u = que[qout++];
        for (i = head[u]; i != -1; i = edge[i].next)
            if (edge[i].f > 0 && lev[v = edge[i].v] == 0) {
                lev[v] = lev[u] + 1, que[qin++] = v;
                if (v == t) return 1;
            }
    }
    return 0;
}

int dinic(int s, int t) {
    int i, f, k, qin, u;
    int flow = 0;
    while (bfs(s, t)) {
        memcpy(cur, head, sizeof (head));
        u = s, qin = 0;
        while (1) {
            if (u == t) {
                for (k = 0, f = inf; k < qin; k++)
                    if (edge[que[k]].f < f) f = edge[que[i = k]].f;
                for (k = 0; k < qin; k++)
                    edge[que[k]].f -= f, edge[que[k]^1].f += f;
                flow += f, u = edge[que[qin = i]].u;
            }
            for (i = cur[u]; cur[u] != -1; i = cur[u] = edge[cur[u]].next)
                if (edge[i].f > 0 && lev[u] + 1 == lev[edge[i].v]) break;
            if (cur[u] != -1)
                que[qin++] = cur[u], u = edge[cur[u]].v;
            else {
                if (qin == 0) break;
                lev[u] = -1, u = edge[que[--qin]].u;
            }
        }
    }
    return flow;
}

int main() {
    int n, m, i, j, u, sum;
    while (scanf("%d%d", &m, &n) != -1) {
        ainit();
        for (i = 1, sum = 0; i <= m; i++) {
            scanf("%d", &u);
            sum += u, addedge(0, i, u);
        }
        for (i = m + 1; i <= n + m; i++) {
            scanf("%d", &j);
            while (j--) {
                scanf("%d", &u);
                addedge(u, i, 1);
            }
        }
        for (i = m + 1; i <= m + n; i++) addedge(i, n + m + 1, 1);
        if (dinic(0, m + n + 1) != sum) printf("No Solution!\n");
        else {
            for (i = 1; i <= m; i++) {
                printf("%d:", i);
                for (j = head[i]; j != -1; j = edge[j].next)
                    if (edge[j].f == 0 && edge[j].v != 0)
                        printf(" %d", edge[j].v-m);
                printf("\n");
            }
        }
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值