思路:分层图,第一层图的边权为均为w,第二层图的边权均为w / 2,第一层图的点i与第二层图的点i + n对应,由于骑上马之后一定比骑上马再下马更优,因此两层图之间用单向边连接,即,如果在点i上有马,那么就在i与i + n之间连一条边权为0的单向边。最后从点1和点n分别跑dijkstra即可。最后一个点的最短时间就是min(dis[i], dis[i + n])。
用数组模拟建边会超时 注意开longlong
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
void solve() {
int n, m, h;
cin >> n >> m >> h;
vector<vector<pair<int, int>>> adj(2 * n + 1);
for(int i = 1; i <= h; i ++ ) {
int x;
cin >> x;
adj[x].push_back({x + n, 0});
}
while(m -- ) {
int u, v, w;
cin >> u >> v >> w;
adj[u].push_back({v, w});
adj[v].push_back({u, w});
adj[u + n].push_back({v + n, w / 2});
adj[v + n].push_back({u + n, w / 2});
}
vector<bool> st(2 * n + 1);
auto dijkstra = [&](int start, vector<ll> &dis) {
dis[start] = 0;
priority_queue<pair<ll, int>, vector<pair<ll, int>>, greater<>> q;
q.push({0ll, start});
while(!q.empty()) {
auto t = q.top();
q.pop();
ll dist = t.first, ver = t.second;
if(st[ver]) continue;
st[ver] = true;
for(auto t: adj[ver]) {
if(dis[t.first] > dist + t.second) {
dis[t.first] = dist + t.second;
q.push({dis[t.first], t.first});
}
}
}
};
vector<ll> dis(2 * n + 1, 1e18), redis(2 * n + 1, 1e18);
dijkstra(1, dis);
for(int i = 1; i <= 2 * n; i ++ ) st[i] = false;
dijkstra(n, redis);
ll ans = 1e18;
for(int i = 1; i <= n; i ++ ) {
ans = min(ans, max(min(dis[i], dis[i + n]), min(redis[i], redis[i + n])));
}
if(ans == 1e18) cout << "-1\n";
else cout << ans << "\n";
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0), cout.tie(0);
int t;
cin >> t;
while(t -- ) solve();
return 0;
}