이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
using namespace std;
const int N = 100005;
const int M = 200005;
const long long inf = (long long)1e18;
vector<int> g[N];
int a[M], b[M], c[M], vis[N];
long long dists[N], distt[N];
long long distu[N], distv[N];
long long pref[N], suff[N];
inline void distances(int n, long long dist[], int s){
for(int i = 1; i <= n; i++){
dist[i] = inf;
vis[i] = 0;
}
set<pair<long long, int> > st;
st.insert({0, s});
dist[s] = 0;
while(!st.empty()){
auto pp = *st.begin();
int u = pp.second;
st.erase(pp);
if(vis[u])continue;
vis[u] = 1;
for(int i : g[u]){
int v = a[i]^b[i]^u;
if(dist[v] > dist[u] + c[i]){
dist[v] = dist[u] + c[i];
st.insert({dist[v], v});
}
}
}
}
long long compute(int n, int s, int t, int flg){
vector<pair<long long, int> > vec;
for(int i = 1; i <= n; i++){
pref[i] = dists[i];
suff[i] = distt[i];
if(flg){
pref[i] += distu[i];
suff[i] += distv[i];
}else{
pref[i] += distv[i];
suff[i] += distu[i];
}
vec.push_back({dists[i], i});
}
sort(vec.begin(), vec.end());
for(auto pp : vec){
int u = pp.second;
for(int i : g[u]){
int v = a[i]^b[i]^u;
if(dists[u] + c[i] + distt[v] == dists[t]){
pref[v] = min(pref[v], pref[u] + c[i]);
}
}
}
reverse(vec.begin(), vec.end());
for(auto pp : vec){
int u = pp.second;
for(int i : g[u]){
int v = a[i]^b[i]^u;
if(dists[v] + c[i] + distt[u] == dists[t]){
suff[v] = min(suff[v], suff[u] + c[i]);
}
}
}
long long ret = inf;
for(int i = 1; i <= n; i++){
ret = min(ret, pref[i] + suff[i]);
}
return ret;
}
int main(){
ios::sync_with_stdio(false);
cin.tie(NULL);
int n, m;
cin >> n >> m;
int s, t;
cin >> s >> t;
int u, v;
cin >> u >> v;
for(int i = 1; i <= m; i++){
cin >> a[i] >> b[i] >> c[i];
g[a[i]].push_back(i);
g[b[i]].push_back(i);
}
distances(n, dists, s);
distances(n, distt, t);
distances(n, distu, u);
distances(n, distv, v);
long long ans = inf;
ans = min(ans, compute(n, s, t, 0));
ans = min(ans, compute(n, s, t, 1));
ans = min(ans - dists[t], distu[v]);
cout << ans << '\n';
return 0;
}
# | 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... |