HDU 4289 最大流

HDU 4289
题目链接:
http://acm.hdu.edu.cn/showproblem.php?pid=4289
题意:
给一个无向图,图上有带权值点。
问去除权值和最小的点,使得s到t点不连通。
思路:
拆点最大流。
一个点拆成两个点,i-i’,容量为点权值。源点s,汇点t’
如果点之间有连边,则i’-j,j-’i,容量为无穷大,表示能连通。
原理的大致理解:
一个点拆两个点自然不用解释。
假设流流入u,若u点已经去除,则从连通的v也就是拆点后的v’流出,且没有经过u点。
源码:
#include <cstdio>
#include <cstring>
#include <cmath>
#include <cstdlib>
#include <algorithm>
#include <iostream>
#include <queue>
#include <vector>
#include <stack>
using namespace std;
#define inf (10000001)
#define LL long long
const int MAXN = 400 + 5;
const int MAXM = 20000 + 5;
vector<int>lin[MAXN];
int head[MAXN], cnt;
int n, m;
struct Edge
{
    int u, v;
    int ne, flow;
    Edge(){}
    Edge(int _u, int _v, int _flow){u = _u, v = _v, flow = _flow, ne = head[u];}
}edge[MAXM * 4];
void add_edge(int u, int v, int flow)
{
    edge[cnt] = Edge(u, v, flow);
    head[u] = cnt++;
    edge[cnt] = Edge(v, u, 0);
    head[v] = cnt++;
}
int data[MAXN];
void build_graph(int s, int t)
{
    memset(head, -1, sizeof(head));
    cnt = 0;
    for(int i = 1 ; i <= n ; i++)
        scanf("%d", &data[i]), add_edge(i, i + n, data[i]);
    int u, v;
    for(int i = 0 ; i < m ; i++){
        scanf("%d%d", &u, &v);
        add_edge(u + n, v, inf);
        add_edge(v + n, u, inf);
    }
}
int d[MAXN], vis[MAXN], num[MAXN];
queue<int>que;
void BFS(int t)
{
    memset(vis, 0, sizeof(vis));
    while(!que.empty()) que.pop();
    que.push(t);
    vis[t] = 1, d[t] = 0;
    while(!que.empty()){
        int u = que.front();    que.pop();
        num[d[u]]++;
        for(int now = head[u] ; now != -1 ; now = edge[now].ne){
            int v = edge[now].v;
            if(vis[v] == 0) vis[v] = 1, d[v] = d[u] + 1, que.push(v);
        }
    }
}
int p[MAXN], cur[MAXN];
int Augment(int s, int t)
{
    int u = t;
    int flow = inf;
    while(u != s){
        flow = min(flow, edge[p[u]].flow);
        u = edge[p[u]].u;
    }
    u = t;
    while(u != s){
        edge[p[u]].flow -= flow;
        edge[p[u]^1].flow += flow;
        u = edge[p[u]].u;
    }
    return flow;
}
LL ISAP(int s, int t)
{
    LL flow = 0;
    BFS(t);
    int u = s;
    for(int i = 1 ; i <= n * 2 ; i++)
        cur[i] = head[i];
    while(d[s] < 2 * n + 1){
// printf("u = %d\n", u);
// system("pause");
        if(u == t){
            flow += Augment(s, t);
            u = s;
        }
        int ok = 0;
        for(int now = cur[u] ; now != -1 ; now = edge[now].ne){
            int v = edge[now].v;
            if(edge[now].flow && d[u] == d[v] + 1){
                p[v] = now;
                cur[u] = now;
                u = v;
                ok = 1; break;
            }
        }
        if(!ok){
            int ts = 2 * n + 1;
            for(int now = head[u] ; now != -1 ; now = edge[now].ne){
                if(edge[now].flow)  ts = min(ts, d[edge[now].v]);
            }
            if(--num[d[u]] == 0)    break;
            cur[u] = head[u];
            num[d[u] = ts + 1]++;
            if(u != s)  u = edge[p[u]].u;
        }
    }
    return flow;
}
int main()
{
    int s, t;
    while(scanf("%d%d", &n, &m) != EOF){
        scanf("%d%d", &s, &t);
        build_graph(s, t);
        LL ans = ISAP(s, t + n);
        printf("%I64d\n", ans);
    }
    return 0;
}

你可能感兴趣的:(HDU 4289 最大流)