分层图最短路
[JLOI2011] 飞行路线
题目描述
Alice 和 Bob 现在要乘飞机旅行,他们选择了一家相对便宜的航空公司。该航空公司一共在
Alice 和 Bob 现在要从一个城市沿着航线到达另一个城市,途中可以进行转机。航空公司对他们这次旅行也推出优惠,他们可以免费在最多
输入格式
第一行三个整数
接下来一行两个整数
接下来
输出格式
输出一行一个整数,为最少花费。
样例 #1
样例输入 #1
5 6 1
0 4
0 1 5
1 2 5
2 3 5
3 4 5
2 3 3
0 2 100样例输出 #1
8提示
数据规模与约定
对于
对于
对于
另外存在一组 hack 数据。
题解
数组开大一点
#include <bits/stdc++.h>
using namespace std;
using i64 = long long;
vector<array<int, 2>> adj[200005];
int dis[200005];
bool vis[200005];
void solve() {
int n, m, k;
cin >> n >> m >> k;
int s, t;
cin >> s >> t;
for (int i = 1; i <= m; i++) {
int u, v, w;
cin >> u >> v >> w;
adj[u].push_back({v, w});//原图建边
adj[v].push_back({u, w});
for (int j = 1; j <= k; j++) {//建k层图
adj[u + (j - 1) * n].push_back({v + j * n, 0});//在第j-1层建边权为0的边到j层
adj[v + (j - 1) * n].push_back({u + j * n, 0});
adj[u + j * n].push_back({v + j * n, w});//在第j层正常建边权为w的边
adj[v + j * n].push_back({u + j * n, w});
}
}
for (int i = 1; i <= k; i++) {//将每一层的终点连起来,有可能提前到终点
adj[t + (i - 1) * n].push_back({t + i * n, 0});
}
memset(dis, 0x3f3f, sizeof(dis));
dis[s] = 0;
priority_queue<array<int, 2>> pq;
pq.push({0, s});
while (!pq.empty()) {
auto [dist, u] = pq.top();
pq.pop();
if (vis[u]) {
continue;
}
vis[u] = true;
for (auto [v, w] : adj[u]) {
if (dis[v] > dis[u] + w) {
dis[v] = dis[u] + w;
pq.push({-dis[v], v});
}
}
}
cout << dis[t + k * n] << "\n";//求出在第k层终点的距离
}
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
int _ = 1;
// cin >> _;
while (_--) {
solve();
}
return 0;
}[USACO09FEB] Revamping Trails G
题面翻译
约翰一共有
通过每条小径都需要消耗一定的时间。约翰打算升级其中
请帮助约翰决定对哪些小径进行升级,使他每天从
题目描述
Farmer John dutifully checks on the cows every day. He traverses some of the M (1 <= M <= 50,000) trails conveniently numbered 1..M from pasture 1 all the way out to pasture N (a journey which is always possible for trail maps given in the test data). The N (1 <= N <= 10,000) pastures conveniently numbered 1..N on Farmer John's farm are currently connected by bidirectional dirt trails. Each trail i connects pastures P1_i and P2_i (1 <= P1_i <= N; 1 <= P2_i <= N) and requires T_i (1 <= T_i <= 1,000,000) units of time to traverse.
He wants to revamp some of the trails on his farm to save time on his long journey. Specifically, he will choose K (1 <= K <= 20) trails to turn into highways, which will effectively reduce the trail's traversal time to 0. Help FJ decide which trails to revamp to minimize the resulting time of getting from pasture 1 to N.
TIME LIMIT: 2 seconds
输入格式
* Line 1: Three space-separated integers: N, M, and K
* Lines 2..M+1: Line i+1 describes trail i with three space-separated integers: P1_i, P2_i, and T_i
输出格式
* Line 1: The length of the shortest path after revamping no more than K edges
样例 #1
样例输入 #1
4 4 1
1 2 10
2 4 10
1 3 1
3 4 100样例输出 #1
1提示
K is 1; revamp trail 3->4 to take time 0 instead of 100. The new shortest path is 1->3->4, total traversal time now 1.
题解
#include <bits/stdc++.h>
using namespace std;
using i64 = long long;
vector<array<i64, 2>> adj[500005];
i64 dis[500005];
bool vis[500005];
void solve() {
int n, m, k;
cin >> n >> m >> k;
int s = 1, t = n;
for (int i = 1; i <= m; i++) {
int u, v, w;
cin >> u >> v >> w;
adj[u].push_back({v, w}); // 原图建边
adj[v].push_back({u, w});
for (int j = 1; j <= k; j++) { // 向下建k层图
adj[u + (j - 1) * n].push_back({v + j * n, 0}); // 在第j-1层建边权为0的边到j层
adj[v + (j - 1) * n].push_back({u + j * n, 0});
adj[u + j * n].push_back({v + j * n, w}); // 在第j层正常建边权为w的边
adj[v + j * n].push_back({u + j * n, w});
}
}
for (int i = 1; i <= k; i++) { // 将每一层的重点连起来,有可能提前到终点
adj[t + (i - 1) * n].push_back({t + i * n, 0});
}
memset(dis, 0x3f3f3f3f, sizeof(dis));
dis[s] = 0;
priority_queue<array<i64, 2>> pq;
pq.push({0, s});
while (!pq.empty()) {
auto [dist, u] = pq.top();
pq.pop();
if (vis[u]) {
continue;
}
vis[u] = true;
for (auto [v, w] : adj[u]) {
if (dis[v] > dis[u] + w) {
dis[v] = dis[u] + w;
pq.push({-dis[v], v});
}
}
}
cout << dis[t + k * n] << "\n"; // 求出在第k层终点的距离
}
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
int _ = 1;
// cin >> _;
while (_--) {
solve();
}
return 0;
}