首页 > 其他分享 >P1344 [USACO4.4] 追查坏牛奶 Pollutant Control (网络流)

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

时间:2023-05-18 16:11:06浏览次数:39  
标签:Control le vd 牛奶 USACO4.4 ans now P1344 mod

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

题目链接

目录

不会网络流的可以看这个

题目描述

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

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

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

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

输入格式

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

第 \(2\sim M+1\) 行,每行 \(3\) 个整数 \(S_i\)、\(E_i\) 和 \(C_i\)。其中 \(S_i\)、\(E_i\) 分别表示这辆卡车的出发仓库和目的仓库。\(C_i\) 表示让这辆卡车停止运输的损失。

输出格式

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

样例 #1

样例输入 #1

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

样例输出 #1

60 1

提示

对于 \(100 \%\) 的数据,满足 \(2 \le N \le 32\),\(0 \le M \le 10^3\),\(1 \le S_i \le N\),\(1 \le E_i \le N\),\(0 \le C_i \le 2 \times 10^6\)。

题目大意

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

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

思路

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

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

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

此时最大流记为 \(ans\)

那么 \(c = ans / a\) ,\(T = ans \mod a\)

分析

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

然后 因为我们 \(+1\) 了,所以最小路径数仍然是最小割,其他路径就比最小割要大,摸上 \(a\) 后就是 \(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)

思路

要求输出方案。

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

我们把这条边暂时删掉之后看看现在的答案记为 \(ans1\) ,边权记为 \(e[i].w\) 是否满足

\[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;
}

后记

原题检测真的恶心!!!

标签:Control,le,vd,牛奶,USACO4.4,ans,now,P1344,mod
From: https://www.cnblogs.com/2020fengziyang/p/17412264.html

相关文章

  • How to use micro:bit V2 to control built-in sensors All In One
    Howtousemicro:bitV2tocontrolbuilt-insensorsAllInOnemicro:bitV2&PythonMakeCodeMakeCodeformicro:bithttps://makecode.microbit.org/https://www.microsoft.com/zh-cn/makecodeAdafruitCircuitPlaygroundExpresshttps://makecode.a......
  • Jmeter部署到Linux实现分发压测时,controller机器回收测试报告时卡死
    问题检查与定位:检查slave-A和slave-B两台执行机,执行机已经finished,无报错,说明执行机已完成测试任务采集到的日志批量分析后得出的结论:在完成并发测试后,vuser要进行释放,由于没有完全释放导致controller机器一直等待(像卡死),而实际上是等待,问题定位后,进行检查发现:问题1: reportge......
  • AMD Xilinx AXI Interrupt Controller 中断优先级
    中断优先级AXIInterruptController支持中断优先级。在VivadoBlockDesign中,bit-0连接的中断优先级最高,越靠近bit-0的中断优先级最高。AXIInterruptController的手册pg099中的描述如下:Prioritybetweeninterruptrequestsisdeterminedbyvectorposition.Theleas......
  • flutter系列之:使用AnimationController来控制动画效果
    目录简介构建一个要动画的widget让图像动起来总结简介之前我们提到了flutter提供了比较简单好用的AnimatedContainer和SlideTransition来进行一些简单的动画效果,但是要完全实现自定义的复杂的动画效果,还是要使用AnimationController。今天我们来尝试使用AnimationController来......
  • How to use the Raspberry Pi and Python to control a DHT11 wet and temperate modu
    HowtousetheRaspberryPiandPythontocontrolaDHT11wetandtemperatemoduleAllInOne如何使用树莓派和Python来控制温湿度模块demos(......
  • 问题记录之mysql:Job for mysqld.service failed because the control process exited
    今天服务器连接mysql发现一直超时(查出的原因是磁盘满了)清了磁盘以后,mysqld.service 还是无法启动执行命令及报错如下:(注意,因为磁盘满的问题,我的mysql并不是正常途径关闭的)控制进程以错误代码退出导致无法以正常的方式启动它了,错误说明:Jobformysqld.servicefailedbecauset......
  • How to use the Raspberry Pi and Python to control a buzzer All In One
    HowtousetheRaspberryPiandPythontocontrolabuzzerAllInOne如何使用树莓派和Python来控制蜂鸣器蜂鸣器有源蜂鸣器vs无源蜂鸣器现在有很多人对有源蜂鸣器和无源蜂鸣器的概念不是很清楚,这里做简单介绍,希望对大家日后使用有所帮助。注意,这里的“源”不是指......
  • C#中TabControl相关用法
    https://blog.csdn.net/fenglearning/article/details/118330487https://blog.csdn.net/u011555996/article/details/53199362?spm=1001.2101.3001.6650.1&utm_medium=distribute.pc_relevant.none-task-blog-2%7Edefault%7ECTRLIST%7ERate-1-53199362-blog-118330487.23......
  • 跨域问题解决记录Access-Control-Allow-Origin方法
      more_set_headers 'Access-Control-Allow-Origin: http://devops.opsenv.com';    more_set_headers 'Access-Control-Allow-Methods: GET, POST, PUT, DELETE, OPTIONS';    more_set_headers 'Access-Control-Allow-Headers: Authorization,DNT,......
  • Intel(R) Ethernet Controller X710驱动升级
    环境CentOSLinuxrelease7.9.2009(Core)升级先查看原驱动版本[root@xcdcs~]#lspci|grepnet01:00.0Ethernetcontroller:IntelCorporationEthernetControllerX710for10GbESFP+(rev02)01:00.1Ethernetcontroller:IntelCorporationEthernetController......