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"
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, less<int>, rb_tree_tag, tree_order_statistics_node_update> ordered_set;
#define ll long long
#define mod 1000000007
ofstream fout(".out");
ifstream fin(".in");
vector<pair<int, int>> v[100001];
int d[100001], bb[100001];
int n, m, s, t;
signed main() {
ios::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
cin >> n >> m;
for(int i = 1; i <= n; i++)
d[i] = 1e9;
for(int i = 0; i < m; i++) {
int a, b, c;
cin >> a >> b >> c;
v[a].push_back({b, c});
v[b].push_back({a, c});
}
int k;
cin >> k;
set<pair<int, int>> ss;
for(int i = 0; i < k; i++) {
int x;
cin >> x;
d[x] = 0;
ss.insert({0, x});
}
while(ss.size()) {
pair<int, int> p = *ss.begin();
ss.erase(ss.begin());
int x = p.second;
int cost = p.first;
if(cost > d[x])
continue;
d[x] = cost;
for(auto i : v[x]) {
if(cost + i.second < d[i.first]) {
d[i.first] = cost + i.second;
ss.insert({cost + i.second, i.first});
}
}
}
int q;
cin >> q;
while(q--) {
cin >> s >> t;
bool b = 0;
for(auto i : v[s]) {
if(i.first == t) {
b = 1;
break;
}
}
if(b) {
cout << min(d[s], d[t]) << "\n";
continue;
}
for(int i = 1; i <= n; i++)
bb[i] = -1;
ss.clear();
ss.insert({d[s] * -1, s});
bb[s] = d[s];
while(ss.size()) {
pair<int, int> p = *ss.begin();
ss.erase(ss.begin());
int x = p.second;
int cost = p.first * -1;
if(x == t) {
cout << cost << "\n";
break;
}
if(cost < bb[x])
continue;
for(auto i : v[x]) {
if(min(cost, d[i.first]) > bb[i.first]) {
bb[i.first] = min(cost, d[i.first]);
ss.insert({min(cost, d[i.first]) * -1, i.first});
}
}
}
}
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... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |