HDU 1839 Delay Constrained Maximum Capacity Path 二分+最短路
题目链接:
题目
Delay Constrained Maximum Capacity Path
Time Limit: 10000/10000 MS (Java/Others)
Memory Limit: 65535/65535 K (Java/Others)
问题描述
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.
输入
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.
输出
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.
样例
input
2
2 1 10
1 2 13 10
4 4 20
1 2 1000 15
2 4 999 6
1 3 100 15
3 4 99 4
output
13
99
题意
找一条1到N的路线,使得时间花费<=T的前提下容量最小的边的容量值最大。
题解
最小值最大,想到二分答案。
每次二分出容量后,跑残图的最短路,判断可行性。
代码
#include<queue>
#include<vector>
#include<cstdio>
#include<cstring>
#include<iostream>
using namespace std;
const int maxn = 1e4 + 10;
const int maxm = 10 * maxn;
typedef long long LL;
const LL INF = 0x3f3f3f3f3f3f3f3fLL;
struct Edge {
int u, v, c, w;
Edge(int u, int v, int c, int w) :u(u), v(v), c(c), w(w) {}
};
int n, m, T;
vector<int> G[maxn];
vector<Edge> egs;
void addEdge(int u, int v, int c, int w) {
egs.push_back(Edge(u, v, c, w));
G[u].push_back(egs.size() - 1);
}
int mid;
int inq[maxn];
LL d[maxn];
bool spfa() {
memset(inq, 0, sizeof(inq));
for (int i = 0; i <= n; i++) d[i] = INF;
queue<int> Q;
d[0] = 0, inq[0] = 1, Q.push(0);
while (!Q.empty()) {
int u = Q.front(); Q.pop();
inq[u] = 0;
for (int i = 0; i < G[u].size(); i++) {
Edge& e = egs[G[u][i]];
if (e.c < mid) continue;
if (d[e.v]>d[u] + e.w) {
d[e.v] = d[u] + e.w;
if (!inq[e.v]) inq[e.v] = 1, Q.push(e.v);
}
}
}
if (d[n - 1] <= T) return true;
return false;
}
void init() {
for (int i = 0; i <= n; i++) G[i].clear();
egs.clear();
}
int main() {
int tc;
scanf("%d", &tc);
while (tc--) {
scanf("%d%d%d", &n, &m, &T);
init();
int ma= -1;
while (m--) {
int u, v, c,w;
scanf("%d%d%d%d", &u, &v, &c, &w), u--, v--;
ma = max(ma, c);
addEdge(u, v, c,w);
addEdge(v, u, c,w);
}
int l = 1, r = ma+1;
while (l + 1 < r) {
mid = l + (r - l) / 2;
if (spfa()) l = mid;
else r = mid;
}
printf("%d\n", l);
}
return 0;
}
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· DeepSeek 解答了困扰我五年的技术问题
· 为什么说在企业级应用开发中,后端往往是效率杀手?
· 用 C# 插值字符串处理器写一个 sscanf
· Java 中堆内存和栈内存上的数据分布和特点
· 开发中对象命名的一点思考
· 为什么说在企业级应用开发中,后端往往是效率杀手?
· DeepSeek 解答了困扰我五年的技术问题。时代确实变了!
· 本地部署DeepSeek后,没有好看的交互界面怎么行!
· 趁着过年的时候手搓了一个低代码框架
· 推荐一个DeepSeek 大模型的免费 API 项目!兼容OpenAI接口!