四大文化赛道完整展开
03-execution/run-002/source-snapshot/main.cpp
main.cpp
站内文件视图直接读取仓库内容,Markdown 使用文档排版渲染,其余文本文件保持原始排版,方便校对训练证据链。
文件类型.cpp
10-cases/s2-jh-01-route-supply/03-execution/run-002/source-snapshot/main.cpp
#include <algorithm>
#include <iostream>
#include <limits>
#include <queue>
#include <string>
#include <utility>
#include <vector>
using namespace std;
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int n, m, start, target;
if (!(cin >> n >> m >> start >> target)) {
return 0;
}
vector<long long> supply(n + 1, 0);
for (int i = 1; i <= n; ++i) {
cin >> supply[i];
}
vector<vector<pair<int, int>>> graph(n + 1);
for (int i = 0; i < m; ++i) {
int u, v, w;
cin >> u >> v >> w;
graph[u].push_back({v, w});
graph[v].push_back({u, w});
}
const long long INF = numeric_limits<long long>::max() / 4;
vector<long long> dist(n + 1, INF);
vector<int> prev(n + 1, -1);
priority_queue<pair<long long, int>, vector<pair<long long, int>>, greater<pair<long long, int>>> pq;
dist[start] = 0;
pq.push({0, start});
while (!pq.empty()) {
auto [cost, node] = pq.top();
pq.pop();
if (cost != dist[node]) {
continue;
}
for (auto [nxt, weight] : graph[node]) {
long long new_cost = cost + weight + supply[nxt];
if (new_cost < dist[nxt]) {
dist[nxt] = new_cost;
prev[nxt] = node;
pq.push({new_cost, nxt});
}
}
}
if (dist[target] == INF) {
cout << "min_cost=-1\n";
cout << "path=IMPOSSIBLE\n";
return 0;
}
vector<int> path;
for (int cur = target; cur != -1; cur = prev[cur]) {
path.push_back(cur);
}
reverse(path.begin(), path.end());
cout << "min_cost=" << dist[target] << "\n";
cout << "path=";
for (size_t i = 0; i < path.size(); ++i) {
if (i) {
cout << "->";
}
cout << path[i];
}
cout << "\n";
return 0;
}