惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

F
Fortinet All Blogs
爱范儿
爱范儿
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
B
Blog
WordPress大学
WordPress大学
Jina AI
Jina AI
GbyAI
GbyAI
aimingoo的专栏
aimingoo的专栏
N
Netflix TechBlog - Medium
腾讯CDC
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
阮一峰的网络日志
阮一峰的网络日志
The GitHub Blog
The GitHub Blog
V
Visual Studio Blog
Google DeepMind News
Google DeepMind News
月光博客
月光博客
博客园 - Franky
Y
Y Combinator Blog
MyScale Blog
MyScale Blog
大猫的无限游戏
大猫的无限游戏
Martin Fowler
Martin Fowler
雷峰网
雷峰网
小众软件
小众软件
H
Hackread – Cybersecurity News, Data Breaches, AI and More

OhYee 博客

小鹏辅助驾驶测评|OhYee 博客 小鹏非支持手机开启自动解锁|OhYee 博客 使用函数计算实现 301 重定向|OhYee 博客 针对 HTML 内容使用 Ant Design 图片弹框|OhYee 博客 博客进程泄露及僵尸进程解决|OhYee 博客 蓝易云服务器体验|OhYee 博客 SSH 调起本地 VSCode|OhYee 博客 【2022 秋招内推】阿里云后端研发工程师|OhYee 博客 使用函数计算获取 IP 地址信息|OhYee 博客 正确获取客户端 IP/HTTP Header 也可能重复|OhYee 博客 评测 Oculus Quest2 及 BigScreen|OhYee 博客 NextJS 热重载保留状态|OhYee 博客 如何优雅地贴 gist 代码|OhYee 博客 Linux 精细化文件权限|OhYee 博客 VSCode 容器开发环境|OhYee 博客 Clash 的不兼容更新排查|OhYee 博客 Zeek 导出 PCAP|OhYee 博客 记一次 ssh 配置问题|OhYee 博客 Git Commit 规范化工具|OhYee 博客 谈谈《星之卡比-探索发现》|OhYee 博客 VSCode 快捷键绑定 Shell 命令|OhYee 博客 ASN.1 语法及 X.509 证书格式解析解析|OhYee 博客 腾讯企业邮箱忽略 MX 记录发信|OhYee 博客 Chrome/Edge 标签组插件|OhYee 博客 【应届内推】阿里云后端研发工程师|OhYee 博客 损坏的 Typecho 备份处理为 JSON|OhYee 博客 VS Code VIM 插件高效使用|OhYee 博客 SSH 正反向代理|OhYee 博客 Let's Encrypt 根证书过期引发的问题|OhYee 博客 OpenWRT 忽略内核依赖|OhYee 博客
PAT顶级 1003.Universal Travel Sites|OhYee 博客
2018-07-05 · via OhYee 博客

题目

原题链接
{% fold 点击显/隐题目 %}

After finishing her tour around the Earth, CYLL is now planning a universal travel sites development project. After a careful investigation, she has a list of capacities of all the satellite transportation stations in hand. To estimate a budget, she must know the minimum capacity that a planet station must have to guarantee that every space vessel can dock and download its passengers on arrival.

Input Specification:
Each input file contains one test case. For each case, the first line contains the names of the source and the destination planets, and a positive integer N (<=500). Then N lines follow, each in the format:

source~i~ destination~i~ capacity~i~

where source~i~ and destination~i~ are the names of the satellites and the two involved planets, and capacity~i~ > 0 is the maximum number of passengers that can be transported at one pass from source~i~ to destination~i~. Each name is a string of 3 uppercase characters chosen from {A-Z}, e.g., ZJU.

Note that the satellite transportation stations have no accommodation facilities for the passengers. Therefore none of the passengers can stay. Such a station will not allow arrivals of space vessels that contain more than its own capacity. It is guaranteed that the list contains neither the routes to the source planet nor that from the destination planet.

Output Specification:

For each test case, just print in one line the minimum capacity that a planet station must have to guarantee that every space vessel can dock and download its passengers on arrival.

Sample Input:
EAR MAR 11
EAR AAA 300
EAR BBB 400
AAA BBB 100
AAA CCC 400
AAA MAR 300
BBB DDD 400
AAA DDD 400
DDD AAA 100
CCC MAR 400
DDD CCC 200
DDD MAR 300

Sample Output:
700

{% endfold %}

解析

基础的网络流题,不卡题意,不卡时间。
然而因为自己手写网络流,还是坑了很久。

具体踩的坑见:new、sizeof与指针
网络流算法见:EK、Dinic和ISAP

数据读入结合了string和map。
为了能在建图前获得节点个数,使用vector先把输入数据存了起来,在输入完成后重新插入到图中。

代码

C++解法

{% fold 点击显/隐题目 %}

#include <cstdio>
#include <cstring>
#include <map>
#include <queue>
#include <string>
#include <vector>
using namespace std;

#define Log(format, ...) // printf(format, ##__VA_ARGS__)

map<string, int> hashTable;
map<int, string> unHashTable;

int makeHash(char t[4]) {
    static int idx = 0;
    if (idx == 0) {
        hashTable.clear();
        unHashTable.clear();
    }
    string s = string(t);
    if (hashTable.count(s) == 0) {
        hashTable.insert(make_pair(s, idx));
        unHashTable.insert(make_pair(idx, s));
        ++idx;
    }
    int hashCode = hashTable.find(s)->second;
    Log("%s ---> %d\n", s.c_str(), hashCode);
    return hashCode;
}
string unHash(int idx) {
    auto iter = unHashTable.find(idx);
    if (iter == unHashTable.end())
        return "";
    return iter->second;
}

class maxFlow {
    struct Edge {
        int cap, flow;
        Edge(int _cap = 0, int _flow = 0) : cap(_cap), flow(_flow) {}
    };
    static const int INF = 0x7FFFFFFF;
    static const bool DEBUG = false;
    int s, v, n;

    Edge **edges;
    int *dis, *num, *pre, *cur;

  public:
    void addEdge(int from, int to, int cap) {
        Log("%d -> %d cap = %d\n", from, to, cap);
        edges[from][to].cap += cap;
    }

    int ISAP() {
        bfs();
        layerCalc();
        return dfs();
    }
    maxFlow(int s, int v, int n) {
        this->s = s;
        this->v = v;
        this->n = n;

        Log("init %d -> %d  n = %d\n", s, v, n);

        edges = new Edge *[n];
        for (int i = 0; i < n; ++i) {
            edges[i] = new Edge[n];
            memset(edges[i], 0, sizeof(Edge) * n);
        }

        dis = new int[n];
        num = new int[n + 1];
        pre = new int[n];
        cur = new int[n];
    }
    ~maxFlow() {
        for (int i = 0; i < n; ++i)
            delete[] edges[i];
        delete[] edges;

        delete[] dis;
        delete[] num;
        delete[] pre;
        delete[] cur;
    }

  private:
    queue<int> Q;

    void bfs() {
        while (!Q.empty())
            Q.pop();
        memset(dis, 0, sizeof(int) * n);
        Q.push(v);
        dis[v] = 1;
        while (!Q.empty()) {
            int t = Q.front();
            Q.pop();
            for (int i = 0; i < n; ++i) {
                Edge &e = edges[i][t];
                if (e.cap > e.flow && !dis[i]) {
                    dis[i] = dis[t] + 1;
                    Q.push(i);
                }
            }
        }
    }

    void layerCalc() {
        memset(num, 0, sizeof(int) * (n + 1));
        for (int i = 0; i < n; ++i)
            ++num[dis[i]];
    }

    int Augumemt() {
        int t = v, delta = INF;
        while (t != s) {
            int &lastNode = pre[t];
            Edge &e = edges[lastNode][t];
            delta = min(delta, e.cap - e.flow);
            t = lastNode;
        }
        t = v;
        while (t != s) {
            int &lastNode = pre[t];
            Edge &e = edges[lastNode][t];
            Edge &e2 = edges[t][lastNode];
            e.flow += delta;
            e2.flow -= delta;
            t = lastNode;
        }
        return delta;
    }

    int dfs() {
        memset(pre, 0, sizeof(int) * n);
        memset(cur, 0, sizeof(int) * n);

        int flow = 0;
        int t = s;

        while (dis[s] <= n) {
            if (DEBUG)
                test(t);

            if (t == v) {
                flow += Augumemt();
                t = s;
                Log("At the destnation flow = %d\n", flow);
            }

            int finish = true;
            for (int i = cur[t]; i < n; ++i) {
                Edge &e = edges[t][i];
                if (e.cap > e.flow && dis[t] == dis[i] + 1) {
                    finish = false;
                    pre[i] = t;
                    cur[t] = i;
                    t = i;
                    break;
                }
            }

            if (finish) {
                Log("finish\n");
                int m = n;
                for (int i = 0; i < n; i++) {
                    Edge &e = edges[t][i];
                    if (e.cap > e.flow)
                        m = min(m, dis[i]);
                }
                if (--num[dis[t]] == 0)
                    break;
                ++num[dis[t] = m + 1];
                cur[t] = 0;
                if (t != s)
                    t = pre[t];
            }
        }
        return flow;
    }

    void test(int t) {
        Log("At %d\n", t);
        Log("idx:\t");
        for (int i = 0; i < n; ++i)
            Log("%4d ", i);
        Log("\n");
        disTest();
        preTest();
        numTest();
        curTest();
        edgeTest();
        Log("\n\n");
    }
    void disTest() {
        Log("dis:\t");
        for (int i = 0; i <= n; ++i)
            Log("%4d ", dis[i]);
        Log("\n");
    }

    void preTest() {
        Log("pre:\t");
        for (int i = 0; i < n; ++i)
            Log("%4d ", pre[i]);
        Log("\n");
    }

    void numTest() {
        Log("num:\t");
        for (int i = 0; i <= n; ++i)
            Log("%4d ", num[i]);
        Log("\n");
    }
    void curTest() {
        Log("cur:\t");
        for (int i = 0; i < n; ++i)
            Log("%4d ", cur[i]);
        Log("\n");
    }

    void edgeTest() {
        Log("\t");
        for (int i = 0; i < n; ++i)
            Log("%6d    ", i);
        Log("\n");
        for (int i = 0; i < n; ++i) {
            Log("%d\t", i);
            for (int j = 0; j < n; ++j) {
                Log("(%3d,%3d) ", edges[i][j].cap, edges[i][j].flow);
            }
            Log("\n");
        }
    }
};

const int maxn = 505;
struct Node {
    int from, to, cap;
    Node(int _from, int _to, int _cap) : from(_from), to(_to), cap(_cap) {}
};
vector<Node> vec;

int main() {
    int n;
    char a[4], b[4];
    scanf("%s%s%d", a, b, &n);
    int s = makeHash(a), v = makeHash(b);
    vec.clear();
    for (int i = 0; i < n; ++i) {
        int cap;
        scanf("%s%s%d", a, b, &cap);
        int from = makeHash(a), to = makeHash(b);
        vec.push_back(Node(from, to, cap));
    }

    maxFlow ans = maxFlow(s, v, hashTable.size());
    for (auto it = vec.begin(); it != vec.end(); ++it)
        ans.addEdge(it->from, it->to, it->cap);

    printf("%d\n", ans.ISAP());

    return 0;
}

{% endfold %}