Uva-10480 Sabotage

题目链接:Sabotage

题目大意:求s与t点之间消除哪些边使s与t不相连且所删除的边的权值和最小。

解题思路:这道题其实是求最小割的边集,由于最小割等于最大流,所以跑一遍Dinic就可以得到最小权值和,但是如何求最小割的边集呢?我们把最后的残余网络找到,会发现那些最小权值且能使s与t联通的边权已经变为了0,那么我们就可以把与s点相连且相连边的权不为零的点集找到,再把这些点相连的且边权为零的边找到,即为所求答案。

代码如下:

#include <map>
#include <set>
#include <cmath>
#include <queue>
#include <stack>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <algorithm>
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
const int inf  = 0x3f3f3f3f;
const int maxn = 100 + 15;
const int maxm = 1e6 + 15;
struct Edge {
    int to, cap, next;
};

int ecnt, st, ed, m, n;
Edge es[maxm];
int cur[maxn], dep[maxn], head[maxn], vis[maxn]; 

class Dinic {
public:
    void init(){
        memset(head, -1, sizeof head);
        ecnt = 0;
    }

    void add_edge(int u, int v, int cap) {
        es[ecnt].to = v;
        es[ecnt].next = head[u];
        es[ecnt].cap = cap;
        head[u] = ecnt++;
    }

    void add_double(int u, int v, int w1, int w2 = 0) {
        add_edge(u, v, w1);
        add_edge(v, u, w2);
    }

    bool BFS() {
        queue<int> q; q.push(st);
        memset(dep, 0x3f, sizeof dep); dep[st] = 0;
        while(q.size() && dep[ed] == inf) {
            int u = q.front(); q.pop();
            for(int i = head[u]; ~i; i = es[i].next) {
                Edge& e = es[i];
                if(e.cap > 0 && dep[e.to] == inf) {
                    dep[e.to] = dep[u] + 1;
                    q.push(e.to);
                }
            }
        }
        return dep[ed] < inf;
    }

    int DFS(int u, int maxflow) {
        if(u == ed) return maxflow;
        int res = 0;
        for(int i = cur[u]; ~i; i = es[i].next) {
            Edge& e = es[i];
            if(dep[e.to] == dep[u] + 1 && e.cap > 0) {
                int flow = DFS(e.to, min(maxflow, e.cap));  
                cur[u] = i; 
                res += flow; maxflow -= flow;
                es[i].cap -= flow; 
                es[i ^ 1].cap += flow;
                if(!maxflow) return res;
            }
        }       
        dep[u] = inf;
        return res;
    }

    ll MaxFlow(){
        ll ans = 0; 
        while(BFS()) {   
            for(int i = st; i <= ed; i++) cur[i] = head[i];
            ans += DFS(st, inf);
        }
        return ans;
    }
};

inline int read() {
    char c = getchar();
    while(!isdigit(c)) c = getchar();
    int x = 0;
    while(isdigit(c)) {
        x = x * 10 + c - '0';
        c = getchar();
    }
    return x;
}

int main(){
#ifdef NEKO
    freopen("Nya.txt", "r", stdin);
#endif
    Dinic dic;
    while(scanf("%d%d", &n, &m) != EOF && n + m) {
        st = 0, ed = n + 1; dic.init();
        dic.add_double(st, 1, inf);
        dic.add_double(2, ed, inf);
        for(int i = 1; i <= m; i++) {
            int u, v, w; u = read();
             v = read(); w = read();
            dic.add_double(u, v, w, w);
        }
        dic.MaxFlow();
        set<int> sp; vector<P> ans;
        queue<int> que; que.push(st);
        while(que.size()) {
            int u = que.front(); que.pop();
            sp.insert(u);
            for(int i = head[u]; ~i; i = es[i].next) {
                int v = es[i].to;
                if(es[i].cap && !sp.count(v)) {
                    sp.insert(v);   
                    que.push(v);
                }
            }
        }
        que.push(st); memset(vis, 0, sizeof vis);
        while(que.size()) {
            int u = que.front(); que.pop(); vis[u] = 1;
            for(int i = head[u]; ~i; i = es[i].next) {
                int v = es[i].to;
                if(!es[i].cap && !sp.count(v))
                    ans.push_back(P(u, v));
                else if(!vis[v]) {  
                    vis[v] = 1;
                    que.push(v);
                }
            }
        }       
        sort(ans.begin(), ans.end());
        ans.resize(unique(ans.begin(), ans.end()) - ans.begin());
        for(int i = 0; i < ans.size(); i++)
            printf("%d %d\n", ans[i].first, ans[i].second); 
        puts("");

    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值