이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using pi = pair<int, int>;
using pl = pair<long long, long long>;
using vi = vector<int>;
using vl = vector<long long>;
using vpi = vector<pair<int, int>>;
using vpl = vector<pair<long long, long long>>;
#define fur(i, a, b) for(ll i = a; i <= (ll)b; ++i)
#define ruf(i, a, b) for(ll i = a; i >= (ll)b; --i)
#define fr first
#define sc second
#define mp make_pair
#define pb emplace_back
#define nl "\n"
#define all(x) (x).begin(),(x).end()
#define rall(x) (x).rbegin(),(x).rend()
const ll mxn = 1e5L + 1;
const ll inf = 1e16L;
ll n, m, s, t, u, v;
vpl adj[mxn];
int main()
{
ios::sync_with_stdio(0);
cin.tie(0);
cin >> n >> m >> s >> t >> u >> v;
fur(i, 1, m) {
ll a, b, c;
cin >> a >> b >> c;
adj[a].pb(b, c);
adj[b].pb(a, c);
}
priority_queue<pl> pq;
ll dist[n + 1];
fur(i, 1, n) {
dist[i] = inf;
}
ll from[n + 1] = {};
dist[s] = 0;
pq.emplace(-dist[s], s);
while(!pq.empty()) {
ll a = pq.top().sc;
if(dist[a] + pq.top().fr != 0) {
pq.pop();
continue;
}
pq.pop();
for(auto [b, c] : adj[a]) {
if(dist[b] > dist[a] + c) {
dist[b] = dist[a] + c;
pq.emplace(-dist[b], b);
from[b] = a;
}
}
}
bool zer[n + 1] = {};
ll cur = t;
while(cur != 0) {
zer[cur] = 1;
cur = from[cur];
}
// shortest path from u to a zero node as well as to t
fur(i, 1, n) {
dist[i] = inf;
}
dist[u] = 0;
pq.emplace(-dist[u], u);
ll to_zer = inf;
while(!pq.empty()) {
ll a = pq.top().sc;
if(dist[a] + pq.top().fr != 0) {
pq.pop();
continue;
}
pq.pop();
if(zer[a]) {
to_zer = min(to_zer, dist[a]);
}
for(auto [b, c] : adj[a]) {
if(zer[a] && zer[b]) {
c = 0;
}
if(dist[a] + c < dist[b]) {
dist[b] = dist[a] + c;
pq.emplace(-dist[b], b);
}
}
}
// cout << to_zer << nl;
// fur(i, 1, n) {
// cout << dist[i] << ' ';
// }
// cout << nl;
cout << to_zer + dist[v] << nl;
}
# | 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... |