跳到正文

题解

Delay Constrained Maximum Capacity Path

lqh1122约 9 分钟
原文
文章信息与版本

文章信息

发布
2026年7月15日
收录
2026年7月15日
更新
2026年11月14日
篇幅
约 9 分钟

版本

  1. v1当前

题目大意

考虑一个有N个顶点、编号从1到N的无向图,以及M条边。编号为1的顶点对应于一个矿山,从中开采出一些珍贵的矿物。编号为N的顶点对应于一个矿物加工厂。每条边都有一个关联的旅行时间(时间单位)和容量(矿物单位)。已经决定从矿山开采出的矿物将通过一条路径运送到工厂。这条路径应该具有尽可能高的容量,以便能够同时运输尽可能多的矿物单位。路径的容量等于其任何一条边的最小容量。然而,这些矿物非常敏感,一旦从矿山开采出来,除非它们在T时间单位内到达工厂,否则它们将开始分解。因此,所选路径的总旅行时间(其边的旅行时间之和)应小于或等于T。

Consider an undirected graph with N vertices, numbered from 1 to N, and M edges. The vertex numbered with 1 corresponds to a mine from where some precious minerals are extracted. The vertex numbered with N corresponds to a minerals processing factory. Each edge has an associated travel time (in time units) and capacity (in units of minerals). It has been decided that the minerals which are extracted from the mine will be delivered to the factory using a single path. This path should have the highest capacity possible, in order to be able to transport simultaneously as many units of minerals as possible. The capacity of a path is equal to the smallest capacity of any of its edges. However, the minerals are very sensitive and, once extracted from the mine, they will start decomposing after T time units, unless they reach the factory within this time interval. Therefore, the total travel time of the chosen path (the sum of the travel times of its edges) should be less or equal to T.

输入

输入的第一行包含一个整数X,表示接下来要进行的测试用例数。每个测试用例的第一行包含3个整数,用空格分隔:N(2 <= N <= 10,000)、M(1 <= M <= 50,000)和T(1 <= T <= 500,000)。接下来的每个M行将包含四个整数,每个用空格分隔:A、B、C和D,表示顶点A和B之间有一条边,容量为C(1 <= C <= 2,000,000,000),旅行时间为D(1 <= D <= 50,000)。A和B是1到N之间不同的整数。任意两个顶点之间最多存在一条边。

The first line of input contains an integer number X, representing the number of test cases to follow. The first line of each test case contains 3 integer numbers, separated by blanks: N (2 <= N <= 10.000), M (1 <= M <= 50.000) and T (1 <= T <= 500.000). Each of the next M lines will contain four integer numbers each, separated by blanks: A, B, C and D, meaning that there is an edge between vertices A and B, having capacity C (1 <= C <= 2.000.000.000) and the travel time D (1 <= D <= 50.000). A and B are different integers between 1 and N. There will exist at most one edge between any two vertices.

输出

对于输入顺序给定的每个X个测试用例,依次打印一行,其中包含从矿山到工厂的路径的最大容量,考虑旅行时间约束。始终存在至少一条路径在遵守旅行时间约束的情况下从矿山到工厂。

For each of the X test cases, in the order given in the input, print one line containing the highest capacity of a path from the mine to the factory, considering the travel time constraint. There will always exist at least one path between the mine and the factory obbeying the travel time constraint.

题目思路

这是一道二分答案 + SPFA判定的经典问题

过程 二分最大的可行容量 使用 S P F A SPFA,只走容量限制大于等于 m i d mid 的边,如果能到达终点,则说明容量 m i d mid 可行 一直循环以上步骤,直到求出最大的容量,在此容量下的最短路就是答案

CodeCode

C++
#include<bits/stdc++.h>
using namespace std;

const int INF = 0x3f3f3f3f;
const int N = 10005;

struct Edge {
    int v, cap, time;
};

int n, m, T;
vector<Edge> g[N];
int dist[N];
bool vis[N];
int limit;

void spfa(int s) {
    memset(dist, 0x3f, sizeof(dist));
    memset(vis, 0, sizeof(vis));
    queue<int> q;
    dist[s] = 0;
    q.push(s);
    vis[s] = true;

    while (!q.empty()) {
        int u = q.front();
        q.pop();
        vis[u] = false;

        for (auto e : g[u]) {
            if (e.cap < limit) continue;
            int v = e.v;
            if (dist[v] > dist[u] + e.time) {
                dist[v] = dist[u] + e.time;
                if (!vis[v]) {
                    vis[v] = true;
                    q.push(v);
                }
            }
        }
    }
}

int main() {
    ios::sync_with_stdio(0);
    cin.tie(0);
    int t;
    cin >> t;
    while (t--) {
        cin >> n >> m >> T;
        for (int i = 1; i <= n; i++) g[i].clear();

        vector<int> a(m); 
        for (int i = 0; i < m; i++) {
            int u, v, c, d;
            cin >> u >> v >> c >> d;
            g[u].push_back({v, c, d});
            g[v].push_back({u, c, d});
            a[i] = c;
        }

        sort(a.begin(), a.end());

        int l = 0, r = m - 1, ans = a[0];
        while (l <= r) {
            int mid = (l + r) / 2;
            limit = a[mid];
            spfa(1);

            if (dist[n] <= T) {
                ans = a[mid];
                l = mid + 1;
            } else {
                r = mid - 1;
            }
        }
        cout << ans << '\n';
    }
    return 0;
}//666
原文