이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
#define FastIO ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0)
#define ll long long
#define PB push_back
#define ALL(v) (v).begin(), (v).end()
#define FOR(i, a, b) for (int i = (a), _b = (b); i <= _b; i++)
#define FORD(i, b, a) for (int i = (b), _a = (a); i >= _a; i--)
#define fi first
#define se second
#define BIT(x, i) (((x) >> (i)) & 1)
using namespace std;
/** END OF TEMPLATE **/
const int mxN = 1e5 + 5;
struct edge {
int des; ll c;
};
struct cmp {
bool operator() (const edge &t1, const edge &t2) {
return t1.c > t2.c;
}
};
int n, m, S, T, U, V;
vector<ll> ds, dd[2];
ll dp[2][mxN];
vector<edge> g[mxN];
void dijkstra(int s, vector<ll> &d) {
d.resize(n+7, 1e18);
priority_queue<edge, vector<edge>, cmp> q;
q.push({s, 0}); d[s] = 0;
while(q.size()) {
int u = q.top().des;
ll du = q.top().c;
q.pop();
if(du != d[u]) continue;
for(edge &v_ : g[u]) {
int v = v_.des; ll c = v_.c;
if(d[v] > d[u] + c) {
d[v] = d[u] + c;
q.push({v, d[v]});
}
}
}
}
ll res;
bool vis[mxN];
void dfs(int u) {
if(vis[u]) return;
vis[u] = 1;
dp[0][u] = dd[0][u];
dp[1][u] = dd[1][u];
for(auto &v_ : g[u]) {
int v = v_.des; ll c = v_.c;
if(ds[v] + c != ds[u]) continue;
dfs(v);
dp[0][u] = min(dp[0][u], dp[0][v]);
dp[1][u] = min(dp[1][u], dp[1][v]);
}
res = min(res, dp[0][u] + dd[1][u]);
res = min(res, dp[1][u] + dd[0][u]);
}
int main()
{
FastIO;
//freopen(".inp", "r", stdin);
//freopen(".out", "w", stdout);
cin >> n >> m >> S >> T >> U >> V;
FOR(i, 1, m) {
int u, v, c; cin >> u >> v >> c;
g[u].PB({v, c}); g[v].PB({u, c});
}
dijkstra(S, ds);
dijkstra(U, dd[0]);
dijkstra(V, dd[1]);
res = dd[0][V];
memset(dp, 0x3f, sizeof dp);
dfs(T);
cout << res;
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... |