洛谷__P4568 [JLOI2011] 飞行路线 (分层图最短路)
题目链接:P4568 [JLOI2011] 飞行路线 - 洛谷
题目大意:
,分别表示城市数,航线数和最多在 免费种航线上搭乘飞机
下来一行两个整数 ,表示的起点和终点
接下来 行,每行三个整数
求:从起点到终点的最短花费
分层图最短路模板:
建图:
建图的层数 = 免费次数 + 1
-
当前层点 通向当前层点 花费为 。( 不使用免费机会 )
-
当前层点 通向当前层点 花费为 。
-
由当前层的点 通往下一层点 ,免费。( 使用一次免费机会 )
-
由当前层的点 通往下一层点 ,免费。
- 不能往上层走,每往下一层走视为使用一次免费次数
建完图后跑一遍 dijkstra 算法就好了
代码:
#include<iostream>
#include<algorithm>
#include<cstring>
#include<cstdlib>
#include<cmath>
#include<vector>
#include<queue>
#include<deque>
#include<stack>
#include<set>
#include<map>
#include<unordered_set>
#include<unordered_map>
#include<bitset>
#include<tuple>
#define inf 72340172838076673
#define int long long
#define endl '\n'
#define F first
#define S second
#define mst(a,x) memset(a,x,sizeof (a))
using namespace std;
typedef pair<int, int> pii;
const int N = 110086, M = 50086 * 60, mod = 998244353;
int n, m, k;
int h[N], ne[M], e[M], w[M], idx;
bool v[N];
int st, fl;
void add(int a, int b, int c) {
e[idx] = b;
w[idx] = c;
ne[idx] = h[a];
h[a] = idx++;
}
priority_queue<pii, vector<pii>, greater<pii >> q;
vector<int> dis(N, inf);
void dij() {
dis[st] = 0;
q.push({dis[st], st});
while (q.size()) {
auto[d, u] = q.top();
q.pop();
if (v[u]) continue;
v[u] = true;
for (int i = h[u]; ~i; i = ne[i]) {
int j = e[i];
if (dis[j] > d + w[i]) {
dis[j] = d + w[i];
q.push({dis[j], j});
}
}
}
}
void solve() {
mst(h, -1);
cin >> n >> m >> k;
cin >> st >> fl;
for (int i = 1; i <= m; i++) {
int a, b, c;
cin >> a >> b >> c;
add(a, b, c), add(b, a, c);
for (int j = 1; j <= k; j++) {
int t = j * n;
add(a + t, b + t, c), add(b + t, a + t, c);
add(a + t - n, b + t, 0);
add(b + t - n, a + t, 0);
}
}
dij();
int res = inf;
for (int i = 0; i <= k; i++) {
res = min(res, dis[fl + n * i]);
}
cout << res << endl;
}
signed main() {
ios::sync_with_stdio(false);
cin.tie(nullptr), cout.tie(nullptr);
int T = 1;
// cin >> T;
while (T--) solve();
return 0;
}

浙公网安备 33010602011771号