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 <iostream>
#include <iomanip>
#include <array>
#include <fstream>
#include <vector>
#include <set>
#include <queue>
#include <cmath>
#include <map>
#include <algorithm>
#include <numeric>
#include <stack>
#include <cstring>
#include <bitset>
#include <climits>
#include <valarray>
#include <list>
#include<functional>
#define int long long int
#define inf 10000000000000
#define endl '\n'
#define mod 1000000007
using namespace std;
//#include <ext/pb_ds/assoc_container.hpp>
//#include <ext/pb_ds/tree_policy.hpp>
//using namespace __gnu_pbds;
//typedef tree<
//int,
//null_type,
//greater_equal<int>,
//rb_tree_tag,
//tree_order_statistics_node_update>
//ordered_set;
signed main(){
ios_base::sync_with_stdio();
cin.tie(0);
cout.tie(0);
int n,m; cin >> n >> m;
int s,t; cin >> s >> t;
int uu,vv; cin >> uu >> vv;
map<pair<int,int>, bool> mp;
vector<pair<int,int>> adj[n+1];
int vis[n+1], dist[n+1], prev[n+1];
for (int i = 0; i < m; i++){
int u,v,w; cin >> u >> v >> w;
adj[u].push_back({v,w});
adj[v].push_back({u,w});
}
for (int i = 1; i <= n; i++){
vis[i] = 0;
dist[i] = inf;
prev[i] = -1;
}
priority_queue<pair<int,int>, vector<pair<int,int>>, greater<pair<int,int>>> pq;
pq.push({0,s});
dist[s] = 0;
while (!pq.empty()) {
int cur = pq.top().second;
int cur_d = pq.top().first;
pq.pop();
if (vis[cur]) continue;
vis[cur] = 1;
for (auto child:adj[cur]){
if (cur_d+child.second < dist[child.first]){
dist[child.first] = cur_d+child.second;
prev[child.first] = cur;
pq.push({dist[child.first], child.first});
}
}
}
int cur = t;
while (prev[cur] != -1){
mp[{cur, prev[cur]}] = mp[{prev[cur], cur}] = 1;
cout << cur << " " << prev[cur] << endl;
cur = prev[cur];
if (cur == s) break;
}
for (int i = 1; i <= n; i++){
cout << dist[i] << " ";
vis[i] = 0;
dist[i] = inf;
prev[i] = -1;
}
cout << endl;
pq.push({0,uu});
dist[uu] = 0;
while (!pq.empty()) {
int cur = pq.top().second;
int cur_d = pq.top().first;
pq.pop();
if (vis[cur]) continue;
vis[cur] = 1;
for (auto child:adj[cur]){
if (mp[{child.first, cur}]) child.second = 0;
if (cur_d+child.second < dist[child.first]){
dist[child.first] = cur_d+child.second;
prev[child.first] = cur;
pq.push({dist[child.first], child.first});
}
}
}
cout << dist[vv] << endl;
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... |