【POJ2125】【网络流】【最小割】Destroying The Graph 题解

本文介绍了一款基于图论的游戏,玩家需通过操作顶点来移除所有边,目标是最小化操作成本。文章详细解析了游戏背后的算法实现,包括最小费用流算法的应用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Alice and Bob play the following game. First, Alice draws some directed graph with N vertices and M arcs. After that Bob tries to destroy it. In a move he may take any vertex of the graph and remove either all arcs incoming into this vertex, or all arcs outgoing from this vertex.
Alice assigns two costs to each vertex: Wi + and Wi -. If Bob removes all arcs incoming into the i-th vertex he pays Wi + dollars to Alice, and if he removes outgoing arcs he pays Wi - dollars.
Find out what minimal sum Bob needs to remove all arcs from the graph.
Input
Input file describes the graph Alice has drawn. The first line of the input file contains N and M (1 <= N <= 100, 1 <= M <= 5000). The second line contains N integer numbers specifying Wi +. The third line defines Wi - in a similar way. All costs are positive and do not exceed 10 6 . Each of the following M lines contains two integers describing the corresponding arc of the graph. Graph may contain loops and parallel arcs.
Output
On the first line of the output file print W — the minimal sum Bob must have to remove all arcs from the graph. On the second line print K — the number of moves Bob needs to do it. After that print K lines that describe Bob’s moves. Each line must first contain the number of the vertex and then ‘+’ or ‘-’ character, separated by one space. Character ‘+’ means that Bob removes all arcs incoming into the specified vertex and ‘-’ that Bob removes all arcs outgoing from the specified vertex.
Sample Input
3 6
1 2 3
4 2 1
1 2
1 1
3 2
1 2
3 1
2 3
Sample Output
5
3
1 +
2 -
2 +

#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <set>
#include <queue>
#include <algorithm>
#include <vector>
#include <cstdlib>
#include <cmath>
#include <ctime>
#include <stack>
#define INF 2100000000
#define ll long long
#define clr(x)  memset(x,0,sizeof(x))

using namespace std;

const int N = 105;
const int M = 10005;
const int inf = 0x3f3f3f3f;
int m,n,num;
struct node { int to,c,f,next,pre; } arc[M];
int head[N],que[N],sta[N],cnt[N],dis[N],rpath[N];
bool flag[N][N];

template <class T> inline void read(T &x) {
    x = 0;
    int flag = 1;
    char ch = (char)getchar();
    while(ch <  '0' || ch >  '9') { if(ch == '-')  flag = -1; ch = (char)getchar(); }
    while(ch >= '0' && ch <= '9') { x = (x<<1)+(x<<3)+ch-'0'; ch = (char)getchar(); }
    x *= flag;
}

void build(int s, int e, int cap) {
    arc[num].to = e;
    arc[num].c = cap;
    arc[num].f = 0;
    arc[num].next = head[s];
    head[s] = num ++;
    arc[num - 1].pre = num;
    arc[num].pre = num - 1;
    arc[num].to = s;
    arc[num].c = arc[num].f = 0;
    arc[num].next = head[e];
    head[e] = num ++;
}

void Bfs() {
    int front,rear;
    for(int i = 0; i <= n+n+1; i++) dis[i] = (n<<1)+2, cnt[i] = 0;
    front = rear = 0;
    dis[(n<<1)+1] = 0;
    cnt[0] = 1;
    que[rear++] = (n<<1)+1;
    while(front != rear) {
        int u = que[front++];
        for(int i = head[u]; i != -1; i = arc[i].next) {
            if(arc[arc[i].pre].c == 0 || dis[arc[i].to] < n+n+2) continue;
            dis[arc[i].to] = dis[u]+1;
            cnt[dis[arc[i].to]] ++;
            que[rear++] = arc[i].to;
        }
    }
}

void dfs(int u) {
    if(cnt[u]) return;
    cnt[u] = 1;
    for(int i = head[u]; i != -1; i = arc[i].next) if(arc[i].c > 0 && !cnt[arc[i].to]) dfs(arc[i].to);
}

void show(int maxflow) {
    clr(cnt);
    dfs(0);
    int rear = 0;
    for(int i = 1; i <= n; i++) {
        if(!cnt[i]) que[rear++] = i;
        if(cnt[i+n]) que[rear++] = i+n;
    }
    printf("%d\n",rear);
    for(int i = 0; i < rear; i++)
        if(que[i] <= n) printf("%d -\n",que[i]);
        else printf("%d +\n",que[i]-n);
}

int main() {
    memset(head, -1, sizeof(head));
    memset(flag, false, sizeof(flag));
    num = 0;
    int a,b,d;
    read(n); read(m);
    for(int i = 1; i <= n; i++) scanf("%d",&d), build(i+n, n+n+1, d);
    for(int i = 1; i <= n; i++) scanf("%d",&d), build(0, i, d);
    while(m--) {
        scanf("%d%d",&a,&b);
        if(flag[a][b+n]) continue;
        flag[a][b+n] = true;
        build(a, b+n, inf);
    }
    Bfs();
    int u,maxflow = 0;
    for(int i = 0; i <= n+n+1; i++) sta[i] = head[i];
    u = 0;
    while(dis[0] < n+n+2) {
        if(u == n+n+1) {
            int curflow = inf;
            for(int i = 0; i != n+n+1; i = arc[sta[i]].to) curflow = min(curflow, arc[sta[i]].c);
            for(int i = 0; i != n+n+1; i = arc[sta[i]].to) {
                arc[sta[i]].c -= curflow;
                arc[sta[i]].f += curflow;
                arc[arc[sta[i]].pre].c += curflow;
                arc[arc[sta[i]].pre].f -= curflow;
            }
            maxflow += curflow;
            u = 0;
        }
        int ii;
        for(ii = sta[u]; ii != -1; ii = arc[ii].next) if(arc[ii].c > 0 && dis[u] == dis[arc[ii].to]+1) break;
        if(ii != -1) {
            sta[u] = ii;
            rpath[arc[ii].to] = arc[ii].pre;
            u = arc[ii].to;
        }
        else {
            if(!(--cnt[dis[u]]))  break;
            int Min = n+n+2;
            sta[u] = head[u];
            for(int i = sta[u]; i != -1; i = arc[i].next) if(arc[i].c > 0) Min = min(Min, dis[arc[i].to]);
            dis[u] = Min+1;
            cnt[dis[u]]++;
            if(u != 0) u = arc[rpath[u]].to;
        }
    }
    printf("%d\n",maxflow);
    show(maxflow);
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值