This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
#define ll long long
#define pb push_back
#define mp make_pair
using namespace std;
const int maxn = 100100;
ll dist[maxn][4], n, m, s, t, u, v;
ll dp[maxn][2];
bool visited[maxn];
vector<pair<ll,ll> > g[maxn];
/*
dist[i][0] - dist to i from u
dist[i][1] - dist to i from v
*/
void dijkstra(int start, int x) {
memset(visited,false,sizeof(visited));
for(int i=0;i<=n;i++) {
dist[i][x] = 10000000LL * INT_MAX;
}
dist[start][x] = 0LL;
//visited[start] = true;
priority_queue<pair<ll,ll>, vector<pair<ll,ll> >, greater<pair<ll,ll> > > pq;
pq.push(mp(0, start));
while(!pq.empty()) {
int curr = pq.top().second;
pq.pop();
if(visited[curr]) continue;
visited[curr] = true;
for(auto i:g[curr]) {
if(dist[curr][x] + i.second < dist[i.first][x] && !visited[i.first]) {
dist[i.first][x] = dist[curr][x] + i.second;
pq.push(mp(dist[i.first][x], i.first));
}
}
}
}
int main() {
//ios_base::sync_with_stdio(false);
//cin.tie(0);
cin>>n>>m;
cin>>s>>t;
cin>>u>>v;
ll x, y, z;
for(int i=0;i<m;i++) {
cin>>x>>y>>z;
g[x].pb(mp(y,z));
g[y].pb(mp(x,z));
}
dijkstra(u, 0);
dijkstra(v, 1);
for(int i=0;i<=n;i++) {
dist[i][2] = dist[i][3] = LLONG_MAX;
dp[i][0] = dp[i][1] = dist[i][1];
}
priority_queue<pair<ll,ll>, vector<pair<ll,ll> >, greater<pair<ll,ll> > > pq;
pq.push(mp(0LL, s));
dist[s][2] = 0LL;
dp[s][0] = dist[s][1];
while(!pq.empty()) {
int curr = pq.top().second;
int cdist = pq.top().first;
pq.pop();
for(auto i:g[curr]) {
int ndist = cdist + i.second;
if(dist[i.first][2] > ndist) {
dist[i.first][2] = ndist;
dp[i.first][0] = min(dp[i.first][0], dp[curr][0]);
pq.push(mp(dist[i.first][2], i.first));
}
else if(dist[i.first][2] == ndist) {
dp[i.first][0] = min(dp[i.first][0], dp[curr][0]);
}
}
}
pq.push(mp(0LL, t));
dist[t][3] = 0LL;
dp[t][1] = dist[t][1];
while(!pq.empty()) {
int curr = pq.top().second;
int cdist = pq.top().first;
pq.pop();
for(auto i:g[curr]) {
int ndist = cdist + i.second;
if(dist[i.first][3] > ndist) {
dist[i.first][3] = ndist;
dp[i.first][1] = min(dp[i.first][1], dp[curr][1]);
pq.push(mp(dist[i.first][3], i.first));
}
else if(dist[i.first][3] == ndist) {
dp[i.first][1] = min(dp[i.first][1], dp[curr][1]);
}
}
}
ll result = dist[v][0];
for(int i=1;i<=n;i++) {
if(dist[i][2] + dist[i][3] == dist[t][2]) {
//cout<<i<<": "<<dist[i][0]<<" "<<dp[i][0]<<" "<<dp[i][1]<<"\n";
result = min(result, dist[i][0] + min(dp[i][0], dp[i][1]));
}
}
cout<<result;
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... |