이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
using namespace std;
#define ll long long int
#define mod 1000000007
#define pb push_back
#define ff first
#define ss second
typedef pair<int,int> pp;
bool com(pp x, pp y){
if(x.ff == y.ff) return x.ss < y.ss;
return x.ff < y.ff;
}
ll power(ll x, ll y){
ll prod = 1;
while(y){
if(y & 1) prod = (prod * x) % mod;
x = (x * x) % mod;
y /= 2;
}
return prod;
}
const int N = 2e5 + 7;
int tc = 1;
vector<pair<int, ll>> vtx[N];
ll ds[N];
void dijsktra(int s, ll *d, int n){
vector<int> vis(n + 1);
priority_queue<pair<ll, int>> q;
q.push({0, s});
while(!q.empty()){
ll c = q.top().ff;
int u = q.top().ss;
q.pop();
if(!vis[u]){
d[u] = -c;
vis[u] = 1;
for(auto v : vtx[u]){
q.push({c - v.ss, v.ff});
}
}
}
}
void dijstra2(int s, int t, ll *du, ll *dv, int n, ll &ans){
vector<int> vis(n + 1);
ll dp[2][n + 2];
for(int i = 0; i <= n; i++) dp[0][i] = dp[1][i] = 1e15;
priority_queue<pair<ll, pair<int, int>>> q;
q.push({0, {s, 0}});
while(!q.empty()){
ll c;
int node, par;
c = q.top().first;
node = q.top().second.first;
par = q.top().second.second;
q.pop();
if (!vis[node]) {
vis[node] = 1;
ds[node] = -c;
dp[0][node] = min(du[node], dp[0][par]);
dp[1][node] = min(dv[node], dp[1][par]);
for (auto v : vtx[node]) q.push({c - v.second, {v.first, node}});
} else if (-c == ds[node]) {
if (min(du[node], dp[0][par]) + min(dv[node], dp[1][par]) <= dp[0][node] + dp[1][node]) {
dp[0][node] = min(du[node], dp[0][par]);
dp[1][node] = min(dv[node], dp[1][par]);
}
}
}
ans = min(ans, dp[0][t] + dp[1][t]);
}
void solve(){
int n, m;
cin >> n >> m;
int s, t;
cin >> s >> t;
int u, v;
cin >> u >> v;
for(int i = 0; i < m; i++){
int uu, vv;
ll w;
cin >> uu >> vv >> w;
vtx[uu].pb({vv, w});
vtx[vv].pb({uu, w});
}
ll du[n + 1], dv[n + 1];
dijsktra(u, du, n);
dijsktra(v, dv, n);
ll ans = du[v];
dijstra2(s, t, du, dv, n, ans);
dijstra2(t, s, du, dv, n, ans);
cout << ans << "\n";
// cout << "Case #" << tc << ": " << ans + k << "\n";
// tc++;
}
int main(){
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int t = 1;
// cin >> t;
while (t--)
solve();
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... |