题目理解
我们需要解决的问题是:在一个有向图中,从起点S出发,携带初始火焰强度F,沿着图中的边移动,每经过一条边,火焰强度会减去该边的衰减值。在到达终点T时,火焰强度必须不小于H。此外,在路径上的每一个中间节点 (包括起点 和终点 ),到达时的火焰强度必须不小于该节点的接收下限 。
解题思路
1. 问题转化:
这实际上是一个带约束的最短路径问题。我们需要找到一条从 到 的路径,使得在路径上的每一步,火焰强度都满足相应的约束条件。具体来说:
- 初始火焰强度为 。
- 对于路径上的每一个节点 ,到达u时的火焰强度必须 。
- 到达T时的火焰强度必须 。
2. 关键观察:
- 火焰强度在路径上是递减的(因为每次经过边都会减去衰减值)。
- 我们需要确保在到达每个节点时,火焰强度足够高以满足 的要求,同时最终到达 时的火焰强度足够高以满足 的要求。
3. 算法选择:
- 可以使用 算法的变种。传统的 算法用于寻找最短路径,但这里我们需要确保路径上的火焰强度始终满足约束。
- 我们可以维护一个数组
max_flame[u],表示到达节点u时能够保持的最大火焰强度。初始时,max_flame[S] = F。 - 对于从 到 的边,衰减值为 ,如果当前火焰强度为
flame,那么到达 时的火焰强度为flame - w。这个值必须 才能继续传递。 - 我们需要优先扩展能够保持更高火焰强度的路径,因为更高的火焰强度更有可能满足后续节点的 约束。
4. 具体步骤:
- 初始化
max_flame[S] = F,其他节点的max_flame为 。 - 使用优先队列(最大堆),按照当前火焰强度从大到小排序。
- 每次从队列中取出火焰强度最大的节点u,遍历其所有邻边。
- 计算到达v的火焰强度:
new_flame = max_flame[u] - w。 - 如果 且 ,则更新 并将 加入队列。
- 如果在过程中发现,则可以立即返回
Yes。 - 如果队列为空且没有满足条件的路径,则返回
No。
Python
import heapq
def solve():
n, m = map(int, input().split())
S, T, F, H = map(int, input().split())
S -= 1 # 转换为0-based
T -= 1
L = list(map(int, input().split()))
adj = [[] for _ in range(n)]
for _ in range(m):
u, v, w = map(int, input().split())
u -= 1
v -= 1
adj[u].append((v, w))
max_flame = [-1] * n
max_flame[S] = F
heap = []
heapq.heappush(heap, (-max_flame[S], S)) # 使用最大堆
found = False
while heap:
neg_flame, u = heapq.heappop(heap)
current_flame = -neg_flame
if u == T and current_flame >= H:
found = True
break
if current_flame < max_flame[u]:
continue # 已经有过更优的解
for (v, w) in adj[u]:
new_flame = current_flame - w
if new_flame >= L[v] and new_flame > max_flame[v]:
max_flame[v] = new_flame
heapq.heappush(heap, (-max_flame[v], v))
print("Yes" if found else "No")
solve()C++
#include <iostream>
#include <vector>
#include <queue>
#include <climits>
using namespace std;
void solve() {
int n, m;
cin >> n >> m;
int S, T, F, H;
cin >> S >> T >> F >> H;
S--; // convert to 0-based
T--;
vector<int> L(n);
for (int i = 0; i < n; i++) {
cin >> L[i];
}
vector<vector<pair<int, int>>> adj(n);
for (int i = 0; i < m; i++) {
int u, v, w;
cin >> u >> v >> w;
u--; v--;
adj[u].emplace_back(v, w);
}
vector<int> max_flame(n, -1);
max_flame[S] = F;
priority_queue<pair<int, int>> pq; // max-heap
pq.push({max_flame[S], S});
bool found = false;
while (!pq.empty()) {
auto [current_flame, u] = pq.top();
pq.pop();
if (u == T && current_flame >= H) {
found = true;
break;
}
if (current_flame < max_flame[u]) {
continue;
}
for (auto [v, w] : adj[u]) {
int new_flame = current_flame - w;
if (new_flame >= L[v] && new_flame > max_flame[v]) {
max_flame[v] = new_flame;
pq.push({max_flame[v], v});
}
}
}
cout << (found ? "Yes" : "No") << endl;
}
int main() {
solve();
return 0;
}Java
import java.util.*;
public class FireOfLove {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int m = sc.nextInt();
int S = sc.nextInt() - 1; // convert to 0-based
int T = sc.nextInt() - 1;
int F = sc.nextInt();
int H = sc.nextInt();
int[] L = new int[n];
for (int i = 0; i < n; i++) {
L[i] = sc.nextInt();
}
List<List<int[]>> adj = new ArrayList<>();
for (int i = 0; i < n; i++) {
adj.add(new ArrayList<>());
}
for (int i = 0; i < m; i++) {
int u = sc.nextInt() - 1;
int v = sc.nextInt() - 1;
int w = sc.nextInt();
adj.get(u).add(new int[]{v, w});
}
int[] maxFlame = new int[n];
Arrays.fill(maxFlame, -1);
maxFlame[S] = F;
PriorityQueue<int[]> pq = new PriorityQueue<>((a, b) -> b[0] - a[0]); // max-heap
pq.add(new int[]{maxFlame[S], S});
boolean found = false;
while (!pq.isEmpty()) {
int[] current = pq.poll();
int currentFlame = current[0];
int u = current[1];
if (u == T && currentFlame >= H) {
found = true;
break;
}
if (currentFlame < maxFlame[u]) {
continue;
}
for (int[] edge : adj.get(u)) {
int v = edge[0];
int w = edge[1];
int newFlame = currentFlame - w;
if (newFlame >= L[v] && newFlame > maxFlame[v]) {
maxFlame[v] = newFlame;
pq.add(new int[]{maxFlame[v], v});
}
}
}
System.out.println(found ? "Yes" : "No");
}
}代码解释
1. 输入处理:
- 读取节点数 和边数 。
- 读取起点 、终点 、初始火焰强度 和终点火焰强度要求 。
- 读取每个节点的接收下限 。
- 构建图的邻接表 。
2. 初始化:
- 数组记录到达每个节点的最大火焰强度,初始时只有起点的火焰强度为 ,其余为
-1(表示不可达)。 - 使用优先队列(最大堆)来优先处理火焰强度更高的路径。
3. Dijkstra变种算法:
- 从优先队列中取出当前火焰强度最高的节点 。
- 如果 是终点且火焰强度满足要求,则返回
Yes。 - 遍历 的所有邻边,计算到达邻接节点 的新火焰强度。
- 如果新火焰强度满足 的接收下限且比之前记录的更高,则更新 并将 加入优先队列。
4. 输出结果:
- 如果找到满足条件的路径,输出
Yes;否则输出No。