이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define pii pair<ll, int>
const int N = 1e5+1;
const ll INF = 1e18;
vector<pii> G[N];
vector<int> G2[N];
int n, m;
vector<int> par[N];
ll ans=INF, dx[N], dy[N];
void dijk(int s, ll *d) {
for (int i=1; i<=n; ++i) d[i]=INF;
d[s]=0;
priority_queue<pii, vector<pii>, greater<pii>> pq;
pq.push({0, s});
while (!pq.empty()) {
ll x=pq.top().first, v=pq.top().second; pq.pop();
if (x > d[v]) continue;
for (auto z : G[v]) {
ll w=z.first, u=z.second;
if (d[u] > d[v]+w) {
d[u]=d[v]+w;
pq.push({d[u], u});
//par[u].clear(); par[u].push_back(v);
} //else if (d[u] == d[v]+w) par[u].push_back(v);
}
}
}
void gen(int v) {
for (auto u : par[v]) {
G2[v].push_back(u);
gen(u);
}
}
void dfs(int v, ll mx, ll my) {
mx=min(mx, dx[v]), my=min(my, dy[v]);
ans=min(ans, mx+my);
for (auto u : G2[v]) dfs(u, mx, my);
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(NULL);
cout.tie(NULL);
cin>>n>>m;
int s, t; cin>>s>>t;
int x, y; cin>>x>>y;
ll d[n+1]={};
for (int i=1; i<=m; ++i) {
int a, b, c; cin>>a>>b>>c;
G[a].push_back({c, b});
G[b].push_back({c, a});
} dijk(s, d);
gen(t);
for (int i=1; i<=n; ++i) par[i].clear();
for (int i=1; i<=n; ++i) dx[i]=dy[i]=INF;
dijk(x, dx), dijk(y, dy);
dfs(t, INF, INF);
ans=min(ans, dx[y]);
cout<<ans<<"\n";
}
# | 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... |