이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
#define ll long long
#define ull unsigned ll
#define pb push_back
#define pa pair<int, int>
#define pall pair<ll, int>
#define fi first
#define se second
#define TASK "test"
#define all(x) x.begin(), x.end()
using namespace std;
template<typename T1, typename T2> bool mini (T1 &a, T2 b) {if(a > b) a = b; else return 0; return 1;}
template<typename T1, typename T2> bool maxi (T1 &a, T2 b) {if(a < b) a = b; else return 0; return 1;}
const int MOD = 1e9 + 7;
const int LOG = 20;
const int maxn = 2e5 + 7;
const ll oo = 1e18 + 69;
int n, m, u, v, w, s, t, a, b;
ll f[4][maxn], ans, dp[2][maxn];
vector<pa> g[maxn];
vector<int> adj[2][maxn];
bool ok[maxn];
void dijkstra(int s, int id) {
fill_n(f[id], n + 1, oo);
f[id][s] = 0;
priority_queue<pall, vector<pall>, greater<pall>> q;
q.push({0, s});
while(!q.empty()) {
int u = q.top().se;
ll fu = q.top().fi;
q.pop();
if(f[id][u] != fu) continue;
for(auto e:g[u]) {
int v = e.fi, w = e.se;
if(mini(f[id][v], fu + w)) {
q.push({fu + w, v});
}
}
}
}
void dfs(int u, int id) {
dp[id][u] = f[2][u];
for(auto v:adj[id][u]) {
if(dp[id][v] == -1) dfs(v, id);
mini(dp[id][u], dp[id][v]);
}
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
//freopen(TASK".inp", "r", stdin);
//freopen(TASK".out", "w", stdout);
cin>>n>>m>>s>>t>>a>>b;
while(m--) {
cin>>u>>v>>w;
g[u].pb({v, w});
g[v].pb({u, w});
}
dijkstra(s, 0);
dijkstra(a, 1);
dijkstra(b, 2);
dijkstra(t, 3);
ans = f[1][b];
for(int u = 1; u <= n; u++) {
for(auto e:g[u]) {
int v = e.fi, w = e.se;
if(f[0][u] + w + f[3][v] == f[0][t]) {
adj[0][u].pb(v);
adj[1][v].pb(u);
ok[u] = ok[v] = 1;
}
}
}
for(int i = 0; i < 2; i++)
for(int j = 1; j <= n; j++) dp[i][j] = -1;
dfs(s, 0);
dfs(t, 1);
for(int i = 1; i <= n; i++) {
if(!ok[i]) continue;
mini(ans, f[1][i] + min(dp[0][i], dp[1][i]));
}
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... |