P1344 [USACO4.4] 追查坏牛奶 Pollutant Control (网络流)

P1344 [USACO4.4] 追查坏牛奶 Pollutant Control (网络流)

题目链接


不会网络流的可以看 这个

题目描述

你第一天接手三鹿牛奶公司就发生了一件倒霉的事情:公司不小心发送了一批有三聚氰胺的牛奶。

很不幸,你发现这件事的时候,有三聚氰胺的牛奶已经进入了送货网。这个送货网很大,而且关系复杂。你知道这批牛奶要发给哪个零售商,但是要把这批牛奶送到他手中有许多种途径。

送货网由一些仓库和运输卡车组成,每辆卡车都在各自固定的两个仓库之间单向运输牛奶。在追查这些有三聚氰胺的牛奶的时候,有必要保证它不被送到零售商手里,所以必须使某些运输卡车停止运输,但是停止每辆卡车都会有一定的经济损失。

你的任务是,在保证坏牛奶不送到零售商的前提下,制定出停止卡车运输的方案,使损失最小。

输入格式

1 1 1 行两个整数 N N N M M M N N N 表示仓库的数目, M M M 表示运输卡车的数量。仓库 1 1 1 代表发货工厂,仓库 N N N 代表有三聚氰胺的牛奶要发往的零售商。

2 ∼ M + 1 2\sim M+1 2M+1 行,每行 3 3 3 个整数 S i S_i Si E i E_i Ei C i C_i Ci。其中 S i S_i Si E i E_i Ei 分别表示这辆卡车的出发仓库和目的仓库。 C i C_i Ci 表示让这辆卡车停止运输的损失。

输出格式

两个整数 C C C T T T C C C 表示最小的损失, T T T 表示在损失最小的前提下,最少要停止的卡车数。

样例 #1

样例输入 #1

4 5
1 3 100
3 2 50
2 4 60
1 2 40
2 3 80

样例输出 #1

60 1

提示

对于 100 % 100 \% 100% 的数据,满足 2 ≤ N ≤ 32 2 \le N \le 32 2N32 0 ≤ M ≤ 1 0 3 0 \le M \le 10^3 0M103 1 ≤ S i ≤ N 1 \le S_i \le N 1SiN 1 ≤ E i ≤ N 1 \le E_i \le N 1EiN 0 ≤ C i ≤ 2 × 1 0 6 0 \le C_i \le 2 \times 10^6 0Ci2×106

题目大意

现在有一个有向图,每一条边有一个边权表示删除这条边需要的代价,问你怎么操作,使得 1 → n 1\to n 1n

没有路径相连且代价最小,同时要求删除的最少路径。

思路

显然,这是求一个图的最小割问题,我们知道最大流等于最小割,所以求出最小割即可。

此时可能有很多种方案,我们需要求出这些方案中的最少路径数。

我们可以把所有边都乘上一个 a a a (建议取 1007 1007 1007 ),再 + 1 +1 +1

此时最大流记为 a n s ans ans

那么 c = a n s / a c = ans / a c=ans/a T = a n s m o d    a T = ans \mod a T=ansmoda

分析

我们把所有边权 ∗ a + 1 *a+1 a+1 后,答案又 / a /a /a 当路径小于 a a a 时是不影响 C C C 的。

然后 因为我们 + 1 +1 +1 了,所以最小路径数仍然是最小割,其他路径就比最小割要大,摸上 a a a 后就是 T T T 了。

code

#include <bits/stdc++.h>
#define LL long long
#define fu(x, y, z) for (int x = y; x <= z; x++)
using namespace std;
const int N = 35, M = 1005, mod = 1007;
const LL Max = 1e15;
int n, m, d[N], vd[M], cnt = 1, hd[N];
LL ans, ans1, p[M << 1];
struct E {
    int to, nt, flg;
    LL w;
} e[M << 1];
void add(int x, int y, LL z) { e[++cnt].to = y, e[cnt].nt = hd[x], e[cnt].w = z, hd[x] = cnt; }
LL dfs(int x, LL pt) {
    LL now = pt, del = 0;
    int y, mind = n - 1;
    if (x == n) return pt;
    for (int i = hd[x]; i; i = e[i].nt) {
        if (!e[i].w) continue;
        y = e[i].to;
        if (d[x] == d[y] + 1) {
            del = min(e[i].w, now);
            del = dfs(y, del);
            now -= del;
            e[i].w -= del, e[i ^ 1].w += del;
            if (d[1] >= n)
                return pt - now;
            if (!now)
                break;
        }
        mind = min(mind, d[y]);
    }
    if (now == pt) {
        vd[d[x]]--;
        if (!vd[d[x]])
            d[1] = n;
        d[x] = mind + 1;
        vd[d[x]]++;
    }
    return pt - now;
}
LL flow() {
    LL sum = 0;
    fu(i, 1, n) d[i] = 0;
    vd[0] = n;
    while (d[1] < n) sum += dfs(1, Max);
    return sum;
}
int main() {
    int u, v;
    LL w;
    scanf("%d%d", &n, &m);
    fu(i, 1, m) {
        scanf("%d%d%lld", &u, &v, &w);
        p[cnt + 1] = w * mod + 1;
        add(u, v, w * mod + 1), add(v, u, 0);
    }
    ans = flow();
    printf("%lld %lld\n", ans / mod, ans % mod);
    // for (int i = 2; i <= cnt; i += 2) {
    //     fu(j, 2, cnt) if (!e[j].flg) e[j].w = p[j];
    //     e[i].w = 0;
    //     ans1 = flow();
    //     if (ans1 + p[i] == ans) {
    //         cout << i / 2 << "\n";
    //         e[i].flg = 1, e[i ^ 1].flg = 1, ans -= p[i];
    //     }
    // }
    return 0;
}

双倍经验

[OJ](追查坏牛奶 - 题目 - DYOJ)

思路

要求输出方案。

也就是看看那条是满流边。

我们把这条边暂时删掉之后看看现在的答案记为 a n s 1 ans1 ans1 ,边权记为 e [ i ] . w e[i].w e[i].w 是否满足
a n s 1 + e [ i ] . w = a n s ans1 + e[i].w = ans ans1+e[i].w=ans
如果满足就是路径。

code

#include <bits/stdc++.h>
#define LL long long
#define fu(x , y , z) for(int x = y ; x <= z ; x ++)
using namespace std;
const int N = 35 , M = 1005 , mod = 1007;
const LL Max = 1e15;
int d[N] , vd[M] , n , m , hd[N] , cnt = 1;
LL ans , t , ans1 , p[M << 1];
struct E {
    int to , nt , flg;
    LL w;
} e[M << 1];
void add (int x , int y , int z) { e[++cnt].to = y , e[cnt].nt = hd[x] , e[cnt].w = z , hd[x] = cnt; }
LL dfs (int x , LL pt) {
    LL now = pt , del = 0;
    int mind = n - 1 , y;
    if (x == n) return pt;
    for (int i = hd[x] ; i ; i = e[i].nt) {
        if (!e[i].w) continue;
        y = e[i].to;
        if (d[x] == d[y] + 1) {
            del = min (now , e[i].w);
            del = dfs (y , del);
            now -= del;
            e[i].w -= del , e[i ^ 1].w += del;
            if (d[1] >= n) return pt - now;
            if (!now) break;
        }
        mind = min (mind , d[y]);
    }
    if (now == pt) {
        vd[d[x]] --;
        if (!vd[d[x]]) d[1] = n;
        d[x] = mind + 1;
        vd[d[x]] ++;
    }
    return pt - now;
}
LL flow () {
    LL sum = 0;
    vd[0] = n;
    fu (i , 1 , n) d[i] = 0;
    while (d[1] < n) {
        sum += dfs (1 , Max);
    }
    return sum;
}
int main () {
    int u , v;
    LL w;
    scanf ("%d%d" , &n , &m);
    fu (i , 1 , m) {
        scanf ("%d%d%lld" , &u , &v , &w);
        p[cnt + 1] = w * mod + 1;
        add (u , v , w * mod + 1) , add (v , u , 0);
    }
    ans = flow ();
    printf ("%lld %lld\n" , ans / mod , ans % mod);
    for (int i = 2 ; i <= cnt ; i += 2) {
        fu (j , 2 , cnt) if (!e[j].flg) e[j].w = p[j];
        e[i].w = 0;
        ans1 = flow ();
        if (ans1 + p[i] == ans) {
            cout << i / 2 << endl;
            e[i].flg = 1 , e[i ^ 1].flg = 1 , ans -= p[i];
        }
    }
    return 0;
}

后记

原题检测真的恶心!!!

  • 5
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值