이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define ar array
const int mxN = 1e5+1, M = 1e9+7;
int n, m, f[4], ord[mxN];
vector<ar<ll, 2>> adj[mxN];
ll dis[4][mxN], dp[mxN];
void dijkstra(ll* dist, int x){
priority_queue<ar<ll, 2>, vector<ar<ll, 2>>, greater<ar<ll, 2>>> pq;
pq.push({0, x});
dist[x] = 0;
while(pq.size()){
ar<ll, 2> u = pq.top();
pq.pop();
//quiza esto está mal
if(u[0] > dist[u[1]]) continue;
for(ar<ll, 2> v : adj[u[1]]){
if(dist[v[1]] > u[0]+v[0]){
dist[v[1]] = u[0]+v[0];
pq.push({dist[v[1]], v[1]});
}
}
}
}
ll solve(int a, int b){
iota(ord, ord+n, 0);
memset(dp, 0, sizeof(dp));
sort(ord, ord+n, [&](const int &p, const int &q){
return dis[a][p] < dis[a][q];
});
ll ans = 1e18;
for(int i = n-1; i>=0; --i){
int x = ord[i];
dp[x] = dis[3][x];
for(ar<ll, 2> u : adj[x]){
if(dis[a][x] + u[0] + dis[b][u[1]] == dis[a][f[b]]){
dp[x] = min(dp[x], dp[u[1]]);
}
}
ans = min(ans, dp[x] + dis[2][x]);
}
return ans;
}
int main(){
#ifdef _DEBUG
// freopen("input.txt", "r", stdin);
// freopen("output.txt", "w", stdout);
#endif
std::ios_base::sync_with_stdio(false); std::cin.tie(0); std::cout.tie(0);
cin >> n >> m;
for(int i = 0; i<4; ++i){
cin >> f[i];
f[i]--;
}
for(int i = 0; i<m; ++i){
int a, b, c;
cin >> a >> b >> c, --a, --b;
adj[a].push_back({c, b});
adj[b].push_back({c, a});
}
memset(dis, 0x3f, sizeof(dis));
for(int i = 0; i<4; ++i){
dijkstra(dis[i], f[i]);
}
ll ans = min(min(solve(0, 1), solve(1, 0)), dis[2][f[3]]);
cout << ans << "\n";
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |