This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
/*
Code written by Talant I.D.
*/
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair <int, int> pii;
typedef pair <ll, ll> pll;
#define precision(n) fixed << setprecision(n)
#define pb push_back
#define ub upper_bound
#define lb lower_bound
#define mp make_pair
#define eps (double)1e-9
#define PI 2*acos(0.0)
#define endl "\n"
#define sz(v) int((v).size())
#define all(v) v.begin(),v.end()
#define rall(v) v.rbegin(),v.rend()
#define do_not_disturb ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define OK cout << "OK" << endl;
inline bool isvowel(char ch){
ch = tolower(ch);
return (ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u');
}
inline bool isprime(int n){
if(n < 2 || (n%2 == 0 && n != 2)) return false;
for(int i = 3; i*i <= n; i++)
if(n%i == 0) return false;
return true;
}
const int N = 1e5+7;
set <int> st[N];
vector <int> npp;
vector <vector <pii>> graph(N);
int dist[N], n, ans[N];
class Union{
private:
vector <int> saizu, link;
public:
Union(int n){
saizu.assign(n+1, 1); link.resize(n+1);
iota(all(link), 0);
}
int find(int n){
if(link[n] == n) return n;
return link[n] = find(link[n]);
}
int same(int a, int b){
return find(a) == find(b);
}
void unite(int a, int b, int val){
if(same(a, b)) return;
a = find(a); b = find(b);
if(sz(st[a]) < sz(st[b])) swap(a, b);
for(auto to : st[b]){
if(st[a].count(to)){
ans[to] = val;
}
else{
st[a].insert(to);
}
}
st[b].clear();
link[b] = a;
}
int getsize(int a){
return saizu[find(a)];
}
};
const int mod = 1e9+7;
ll mode(ll a){
a %= mod;
if(a < 0) a += mod;
return a;
}
ll subt(ll a, ll b){
return mode(mode(a)-mode(b));
}
ll add(ll a, ll b){
return mode(mode(a)+mode(b));
}
ll mult(ll a, ll b){
return mode(mode(a)*mode(b));
}
ll binpow(ll a, ll b){
ll res = 1;
while(b){
if(b&1) res = mult(res, a);
a = mult(a, a);
b >>= 1;
}
return res;
}
void dijkstra(){
for(int i = 1; i <= n; i++){
dist[i] = 2e9;
}
for(auto to : npp) dist[to] = 0;
priority_queue <pii> K;
for(auto to : npp) K.push(mp(0, to));
while(sz(K)){
auto v = K.top(); K.pop();
for(auto to : graph[v.second]){
if(dist[to.first] > dist[v.second]+to.second){
dist[to.first] = dist[v.second]+to.second;
K.push(mp(-dist[to.first], to.first));
}
}
}
}
bool cmp(int a, int b){
return dist[a] > dist[b];
}
int main(){
do_not_disturb
/*
int n, m;
int i, j;
cin >> n >> m;
int grid[n+1][m+1];
int pref[2][n+2][m+2], suff[2][n+2][m+2];
for(i = 1; i <= n; i++){
for(j = 1; j <= m; j++){
char ch;
cin >> ch;
if(ch == '0') grid[i][j] = 0;
else grid[i][j] = 1;
}
}
*/
int m;
cin >> n >> m;
for(int i = 0; i < m; i++){
int x, y, w;
cin >> x >> y >> w;
graph[x].pb(mp(y, w));
graph[y].pb(mp(x, w));
}
int k;
cin >> k;
npp.resize(k);
for(auto &to : npp) cin >> to;
dijkstra();
int q;
cin >> q;
for(int i = 1; i <= q; i++){
int x, y;
cin >> x >> y;
st[x].insert(i);
st[y].insert(i);
}
Union t(n+1);
int id[n+1], vis[n+1];
for(int i = 1; i <= n; i++){
id[i] = i;
vis[i] = 0;
}
sort(id+1, id+n+1, cmp);
for(int i = 1; i <= n; i++){
vis[id[i]] = 1;
for(auto to : graph[id[i]]){
if(vis[to.first]){
t.unite(id[i], to.first, dist[id[i]]);
}
}
}
for(int i = 1; i <= q; i++){
cout << ans[i] << endl;
}
return 0;
}
/*
6 6
000111
011111
011111
111000
111000
111000
6 6
000000
111000
111100
001111
000111
000111
*/
# | 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... |