이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
//In the name of Allah
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair < int , int > pii;
typedef pair < ll , ll > pll;
#define X first
#define Y second
#define mp make_pair
#define pb push_back
#define all(x) (x).begin(), (x).end()
#define sz(x) (int)(x.size())
const int N = 1e5 + 10;
const ll mod = 1e9 + 7;
const ll inf = 1e18 + 10;
int S, T, U, V, mark[N];
ll ds[N], dt[N], du[N], dv[N], dpu[N], dpv[N], ans;
vector < pll > adj[N];
vector < int > dag[N], dag2[N], vec;
priority_queue < pll , vector < pll > , greater < pll > > pq;
void dij(int s, ll *dis){
fill(dis, dis + N, inf);
dis[s] = 0;
pq.push(mp(0, s));
while (sz(pq)){
ll v = pq.top().Y, c = pq.top().X;
pq.pop();
if (c != dis[v])
continue;
for (pll e : adj[v]){
ll u = e.X, w = e.Y;
ll cp = c + w;
if (cp < dis[u]){
dis[u] = cp;
pq.push(mp(dis[u], u));
}
}
}
}
void dfs(int v){
mark[v] = 1;
for (int u : dag[v])
if (!mark[u])
dfs(u);
vec.pb(v);
}
void dfs2(int v){
mark[v] = 1;
for (int u : dag2[v])
if (!mark[u])
dfs2(u);
vec.pb(v);
}
int main(){
ios::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
int n, m;
cin >> n >> m >> S >> T >> U >> V;
for (int i = 0; i < m; i ++){
int u, v, w;
cin >> u >> v >> w;
adj[u].pb(mp(v, w));
adj[v].pb(mp(u, w));
}
dij(S, ds);
dij(T, dt);
dij(U, du);
dij(V, dv);
for (int i = 1; i <= n; i ++)
for (pll e : adj[i])
if (ds[i] + e.Y == ds[e.X])
dag[i].pb(e.X);
for (int i = 1; i <= n; i ++)
for (pll e : adj[i])
if (dt[i] + e.Y == dt[e.X])
dag2[i].pb(e.X);
fill(dpu, dpu + N, inf);
fill(dpv, dpv + N, inf);
ans = du[V];
dfs(S);
reverse(all(vec));
for (int v : vec){
dpu[v] = min(dpu[v], du[v]);
dpv[v] = min(dpv[v], dv[v]);
if (ds[v] + dt[v] == ds[T]){
ans = min(ans, du[v] + dpv[v]);
ans = min(ans, dv[v] + dpu[v]);
}
for (int u : dag[v]){
dpu[u] = min(dpu[u], dpu[v]);
dpv[u] = min(dpv[u], dpv[v]);
}
}
vec.clear();
fill(mark, mark + N, 0);
fill(dpu, dpu + N, inf);
fill(dpv, dpv + N, inf);
dfs2(T);
reverse(all(vec));
for (int v : vec){
dpu[v] = min(dpu[v], du[v]);
dpv[v] = min(dpv[v], dv[v]);
if (ds[v] + dt[v] == ds[T]){
ans = min(ans, du[v] + dpv[v]);
ans = min(ans, dv[v] + dpu[v]);
}
for (int u : dag2[v]){
dpu[u] = min(dpu[u], dpu[v]);
dpv[u] = min(dpv[u], dpv[v]);
}
}
cout << ans;
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... |