1 条题解
-
0
dijkstra
分层图模型:
将图分为两层:走路状态和骑车状态。
走路状态( ):只能走路或开始骑车。
骑车状态( ):只能骑车或停车。
状态表示:
dist[u]:到达 的最短时间。cost[u]:到达 的最少花费。建图:考虑边的构建
走路 → 走路:时间增加 ,花费不变。
骑车 → 骑车:时间增加 ,花费增加 。
走路 → 骑车(骑车 → 走路): 将新增的点 和原本对应的点直接连接。时间增加 ,花费增加 。
Dijkstra:
在上述构成的图中跑最短路。
#include<bits/stdc++.h> using namespace std; typedef long long LL; typedef pair<LL, LL> PII; #define x first #define y second const int N = 2e5 + 10, M = 30; struct node{ int nex, w, tim; }; int n, m, num, wt, bt, v; vector<node> e[N]; int st, ed; int dist[N], cost[N]; bool vis[N]; void dijkstra() { memset(dist, 0x3f, sizeof dist); memset(cost, 0x3f, sizeof dist); priority_queue<PII, vector<PII>, greater<PII>> q; q.push({0, st}); dist[st] = 0; cost[st] = 0; while(q.size()) { int u = q.top().second; q.pop(); if(vis[u]) continue; vis[u] = 1; for(auto [nex, w, tim] : e[u]) { if(dist[nex] > dist[u] + w) { dist[nex] = dist[u] + w; cost[nex] = cost[u] + tim; q.push({dist[nex], nex}); } else if(dist[nex] == dist[u] + w) { if(cost[nex] > cost[u] + tim) cost[nex] = cost[u] + tim; } } } } void solve() { cin >> n >> m >> num >> wt>> bt >> v; for(int i = 1 ; i <= m; i ++) { int x; cin >> x; e[x].push_back({n + x, 0, 0}); e[n + x].push_back({x, 0, 0}); } for(int i = 1; i <= num; i ++) { int x, y; cin >> x >> y; e[x].push_back({y, wt, 0}); e[y].push_back({x, wt, 0}); e[x + n].push_back({y + n, bt, v}); e[y + n].push_back({x + n, bt, v}); } cin >> st >> ed; dijkstra(); cout << dist[ed] << ' ' << cost[ed] << endl; } int main() { int t = 1; // cin >> t; while(t --) solve(); }
- 1
信息
- ID
- 495
- 时间
- 1000ms
- 内存
- 256MiB
- 难度
- 10
- 标签
- 递交数
- 9
- 已通过
- 3
- 上传者