当前位置: 首页 > news >正文

uva 10806 Dijkstra, Dijkstra. (最小费最大流)

uva 10806 Dijkstra, Dijkstra.

题目大意:你和你的伙伴想要越狱。你的伙伴先去探路,等你的伙伴到火车站后,他会打电话给你(电话是藏在蛋糕里带进来的),然后你就能够跑去火车站了,那里有人接应你。

可是。由于你的伙伴跑去火车站的时候穿的是囚服,所以,他经过的街道都被戒严了,你必须从其它街道跑过去。

假设你能够到达火车站,请输出你和你的伙伴在路上花费的最短时间,假设不能请“Back to jail”。

解题思路:最小费最大流。设置一个超级源点连向监狱(起点1), 容量为2(两个人),设置一个超级汇点,使火车站(终点n)连向他,容量为2(两个人)。其余街道皆为无向(即正向反向都要考虑)。且容量为1(每条街道仅仅能跑一次)。最后求最大流,若最大流为2则输出最小费,否则回监狱准备下次越狱吧。

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <cstdlib>
#include <queue>
using namespace std;
typedef long long ll;
const int N = 105;
const int INF = 0x3f3f3f3f;
int n, m, s, t;
int a[N], pre[N], d[N], inq[N]; 

struct Edge{
    int from, to, cap, flow;
    ll cos;
};

vector<Edge> edges;
vector<int> G[3 * N];

void init() {
    for (int i = 0; i < 3 * N; i++) G[i].clear();
    edges.clear();
}

void addEdge(int from, int to, int cap, int flow, ll cos) {
    edges.push_back((Edge){from, to, cap, 0, cos});
    edges.push_back((Edge){to, from, 0, 0, -cos});
    int m = edges.size();
    G[from].push_back(m - 2);
    G[to].push_back(m - 1);
}
void input() {
    int from, to;
    ll cost;
    for (int i = 0; i < m; i++) {
        scanf("%d %d %lld", &from, &to, &cost); 
        addEdge(from, to, 1, 0, cost);
        addEdge(to, from, 1, 0, cost);
    }   
    addEdge(0, 1, 2, 0, 0);
    addEdge(n, n + 1, 2, 0, 0);
}
int BF(int s, int t, int& flow, ll& cost) {
    queue<int> Q;
    memset(inq, 0, sizeof(inq));
    memset(a, 0, sizeof(a));
    memset(pre, 0, sizeof(pre));
    for (int i = 0; i <= 2 * n + 1; i++) d[i] = INF;
    d[s] = 0;
    a[s] = INF;
    inq[s] = 1;
    int flag = 1;
    pre[s] = 0;
    Q.push(s);
    while (!Q.empty()) {
        int u = Q.front(); Q.pop();
        inq[u] = 0;
        for (int i = 0; i < G[u].size(); i++) {
            Edge &e = edges[G[u][i]];
            if (e.cap > e.flow && d[e.to] > d[u] + e.cos) {
                d[e.to] = d[u] + e.cos;
                a[e.to] = min(a[u], e.cap - e.flow);
                pre[e.to] = G[u][i];
                if (!inq[e.to]) {
                    inq[e.to] = 1;
                    Q.push(e.to);
                }
            }   
        }
        flag = 0;
    }
    if (d[t] == INF) return 0;
    flow += a[t];
    cost += (ll)d[t] * (ll)a[t];
    for (int u = t; u != s; u = edges[pre[u]].from) {
        edges[pre[u]].flow += a[t];
        edges[pre[u]^1].flow -= a[t];
    }
    return 1;
}

int MCMF(int s, int t, ll& cost) {
    int flow = 0;
    cost = 0;       
    while (BF(s, t, flow, cost));
    return flow;
}
int main() {
    while (scanf("%d", &n) != EOF) {
        if (n == 0) break;
        scanf("%d", &m);
        s = 0, t = n + 1;
        init();
        input();
        ll cost = 0;
        int ans = MCMF(s, t, cost);
        if (ans == 1) printf("Back to jail\n");
        else printf("%lld\n", cost);
    }   
    return 0;
}

相关文章:

  • rsync 同步mac机器目录数据到windows2008R2
  • 全景展现智慧银川成就
  • oracle数据库未打开解决的方法
  • 【Unity笔记】物体的Transform操作:速度、旋转、平移
  • Linux rz sz
  • 【162天】黑马程序员27天视频学习笔记【Day02-上】
  • 分享一些PHP开发者实用工具(上)
  • 从TensorFlow到PyTorch:九大深度学习框架哪款最适合你?
  • jmeter添加自定义扩展函数之图片base64编码
  • Vue 2.3、2.4 知识点小结
  • 颜色模式
  • 自作聪明的开发
  • 记录一次MySQL进程崩溃,无法重启故障排查
  • 我的IntelliJ IDEA 设置
  • shell自定义函数
  • 4个实用的微服务测试策略
  • crontab执行失败的多种原因
  • Git同步原始仓库到Fork仓库中
  • Javascript基础之Array数组API
  • Java超时控制的实现
  • Python_网络编程
  • Quartz实现数据同步 | 从0开始构建SpringCloud微服务(3)
  • Xmanager 远程桌面 CentOS 7
  • 基于HAProxy的高性能缓存服务器nuster
  • 如何胜任知名企业的商业数据分析师?
  • 入口文件开始,分析Vue源码实现
  • 提醒我喝水chrome插件开发指南
  • LevelDB 入门 —— 全面了解 LevelDB 的功能特性
  • mysql 慢查询分析工具:pt-query-digest 在mac 上的安装使用 ...
  • 哈罗单车融资几十亿元,蚂蚁金服与春华资本加持 ...
  • # 透过事物看本质的能力怎么培养?
  • #每日一题合集#牛客JZ23-JZ33
  • $Django python中使用redis, django中使用(封装了),redis开启事务(管道)
  • (笔记)Kotlin——Android封装ViewBinding之二 优化
  • (附源码)springboot码头作业管理系统 毕业设计 341654
  • (附源码)ssm智慧社区管理系统 毕业设计 101635
  • (转)mysql使用Navicat 导出和导入数据库
  • (转)Oracle 9i 数据库设计指引全集(1)
  • ***检测工具之RKHunter AIDE
  • .libPaths()设置包加载目录
  • .NET 3.0 Framework已经被添加到WindowUpdate
  • .NET Framework 和 .NET Core 在默认情况下垃圾回收(GC)机制的不同(局部变量部分)
  • .NET 使用 ILRepack 合并多个程序集(替代 ILMerge),避免引入额外的依赖
  • .NET/C# 如何获取当前进程的 CPU 和内存占用?如何获取全局 CPU 和内存占用?
  • .NET/C# 中设置当发生某个特定异常时进入断点(不借助 Visual Studio 的纯代码实现)
  • .NET运行机制
  • .net之微信企业号开发(一) 所使用的环境与工具以及准备工作
  • .Net中的设计模式——Factory Method模式
  • /proc/interrupts 和 /proc/stat 查看中断的情况
  • @converter 只能用mysql吗_python-MySQLConverter对象没有mysql-connector属性’...
  • @TableLogic注解说明,以及对增删改查的影响
  • []新浪博客如何插入代码(其他博客应该也可以)
  • [23] 4K4D: Real-Time 4D View Synthesis at 4K Resolution
  • [C#]DataTable常用操作总结【转】
  • [EFI]Acer Aspire A515-54g电脑 Hackintosh 黑苹果efi引导文件