이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#pragma GCC optimize("Ofast")
#include <bits/stdc++.h>
using namespace std;
mt19937_64 rng((int)std::chrono::steady_clock::now().time_since_epoch().count());
const int MAXN = 3e5 + 10;
const int MOD = 1e9 + 7;
#define int long long
int rnd(int x, int y) { // random number generator
int u= uniform_int_distribution<int>(x, y)(rng); return u;
}
int dist[4][MAXN];
bool cmp(int a, int b) {
return dist[0][a] > dist[0][b];
}
void solve(int tc) {
int N, M;
cin >> N >> M;
int r[4]; for(int i=0; i<4; i++)cin >> r[i];
vector<pair<int, int> > adj[N+1];
vector<int> ok[N+1];
for(int i=0; i<M; i++) {
int a, b, c; cin >> a >> b >> c;
adj[a].push_back({b, c});
adj[b].push_back({a, c});
}
for(int i=0; i<4; i++) {
for(int j=1; j<=N; j++) dist[i][j] = 1e17;
}
for(int i=0; i<4; i++) dist[i][r[i]] = 0;
for(int i=0; i<4; i++) {
bool vis[N+1] = {};
priority_queue<pair<int, int>,vector<pair<int, int> >, greater<pair<int,int> > > pq;
for(int j=1; j<=N; j++) pq.push({dist[i][j], j});
while(pq.size()) {
pair<int, int> t= pq.top(); pq.pop();
if(!vis[t.second]) {
vis[t.second] = 1;
for(pair<int, int> x: adj[t.second]) {
if(!vis[x.first] && dist[i][t.second] != 1e17 && dist[i][x.first] > dist[i][t.second] + x.second) {
dist[i][x.first] = dist[i][t.second] + x.second;
pq.push({dist[i][x.first], x.first});
}
}
}
}
}
vector<int> rg[N+1];
for(int i=1; i<=N; i++) {
for(pair<int,int> x: adj[i]) {
if(dist[0][i] + x.second == dist[0][x.first] && dist[0][x.first] + dist[1][x.first] == dist[0][r[1]]) {
ok[i].push_back(x.first);
rg[x.first].push_back(i);
}
}
}
int p[N+1]; for(int i=1; i<=N; i++) p[i] = i;
sort(p+1, p+N+1, cmp);
int dp[N+1];
for(int i=1; i<=N; i++) dp[i] = 1e17;
int ans = dist[2][r[3]];
for(int i=1; i<=N; i++) {
int tar = p[i];
dp[tar] = dist[3][tar];
for(int x: ok[tar]) {
dp[tar] = min(dp[tar], dp[x]);
}
ans = min(ans, dp[tar] + dist[2][tar]);
}
reverse(p+1, p+N+1);
for(int i=1; i<=N; i++) {
int tar = p[i];
dp[tar] = dist[3][tar];
for(int x: rg[tar]) {
dp[tar] = min(dp[tar], dp[x]);
}
ans = min(ans, dp[tar] + dist[2][tar]);
}
cout << ans << "\n";
}
int32_t main(){
ios::sync_with_stdio(0); cin.tie(0);
int t = 1; //cin >> t;
for(int i=1; i<=t; i++) solve(i);
}
# | 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... |