//#pragma comment(linker, "/stack:200000000")
//#pragma GCC optimize("Ofast")
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#include <map>
#include <set>
#include <list>
#include <cmath>
#include <ctime>
#include <deque>
#include <queue>
#include <stack>
#include <string>
#include <bitset>
#include <cstdio>
#include <limits>
#include <vector>
#include <climits>
#include <cstring>
#include <cstdlib>
#include <fstream>
#include <numeric>
#include <sstream>
#include <cassert>
#include <iomanip>
#include <iostream>
#include <algorithm>
#include <stdio.h>
#include <fstream>
#include <unordered_map>
using namespace std;
typedef long long ll;
typedef long double ld;
#define all(x) x.begin(),x.end()
#define pb push_back
#define ent "\n"
#define int long long
const int maxn = (int)2e5 + 13;
const ll inf = (long long)1e18 + 20;
const int mod = (int)1e9 + 7;
int n,m,k,q;
vector<pair<int,int>>g[maxn];
int a[maxn],b[maxn],c[maxn];
int dst[maxn];
int pr[maxn];
int sz[maxn];
int get(int x){
if(x == pr[x])return x;
return pr[x] = get(pr[x]);
}
void unite(int x,int y){
x = get(x),y= get(y);
if(x == y)return ;
if(sz[x] > sz[y])swap(x,y);
pr[x] = y;
sz[y] += sz[x];
}
void solve(){
cin >> n >> m ;
for(int i = 1 ; i <= m ; i++){
int x,y,w;cin >> x >> y >> w;
g[x].pb({y,w});
g[y].pb({x,w});
a[i] = x,b[i] = y,c[i] = w;
}
cin >> k;
set<pair<int,int>>s;
for(int i = 1 ; i <= n ; i ++){
dst[i] = inf;
pr[i] = i;
}
for(int i = 1 ; i <= k ; i ++){
int x;cin >> x;
s.insert({0,x});
dst[x] = 0;
}
while(s.size()){
pair<int,int>v = *s.begin();
s.erase(v);
for(auto to : g[v.second]){
int val = dst[v.second] + to.second;
if(dst[to.first] > val){
dst[to.first] = val;
s.insert({val,to.first});
}
}
}
cin >> q;
for(int i = 1 ; i <= q ; i ++){
int x,y;cin >> x >> y;
int l = 1,r = 1000,ans = 0;
while(l <= r){
int mid = (l + r) >> 1;
for(int j = 1 ; j <= n ; j++){
pr[j] = j;
}
for(int j = 1 ; j <= m ; j ++){
if(dst[a[j]] >= mid && dst[b[j]] >= mid){
unite(a[j],b[j]);
}
}
if(get(x)!=get(y))r = mid - 1;
else l = mid + 1,ans = mid;
}
cout << ans << ent;
}
}
signed main(){
// freopen ("hps.in", "r", stdin);
// freopen ("hps.out", "w", stdout);
ios_base::sync_with_stdio(0);
cin.tie(0);
int t = 1;
//cin >> t;
int test = 0;
while(t --){
// cout << "Case " << ++test << ": ";
solve();
}
return 0;
}
Compilation message
plan.cpp: In function 'int main()':
plan.cpp:123:6: warning: unused variable 'test' [-Wunused-variable]
123 | int test = 0;
| ^~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
14168 KB |
Output is correct |
2 |
Correct |
50 ms |
14680 KB |
Output is correct |
3 |
Correct |
39 ms |
14428 KB |
Output is correct |
4 |
Correct |
3 ms |
14172 KB |
Output is correct |
5 |
Correct |
31 ms |
14428 KB |
Output is correct |
6 |
Correct |
13 ms |
14428 KB |
Output is correct |
7 |
Correct |
3 ms |
14172 KB |
Output is correct |
8 |
Correct |
3 ms |
14172 KB |
Output is correct |
9 |
Incorrect |
123 ms |
14444 KB |
Output isn't correct |
10 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
14168 KB |
Output is correct |
2 |
Correct |
50 ms |
14680 KB |
Output is correct |
3 |
Correct |
39 ms |
14428 KB |
Output is correct |
4 |
Correct |
3 ms |
14172 KB |
Output is correct |
5 |
Correct |
31 ms |
14428 KB |
Output is correct |
6 |
Correct |
13 ms |
14428 KB |
Output is correct |
7 |
Correct |
3 ms |
14172 KB |
Output is correct |
8 |
Correct |
3 ms |
14172 KB |
Output is correct |
9 |
Incorrect |
123 ms |
14444 KB |
Output isn't correct |
10 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
3 ms |
14360 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
220 ms |
31544 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
14168 KB |
Output is correct |
2 |
Correct |
50 ms |
14680 KB |
Output is correct |
3 |
Correct |
39 ms |
14428 KB |
Output is correct |
4 |
Correct |
3 ms |
14172 KB |
Output is correct |
5 |
Correct |
31 ms |
14428 KB |
Output is correct |
6 |
Correct |
13 ms |
14428 KB |
Output is correct |
7 |
Correct |
3 ms |
14172 KB |
Output is correct |
8 |
Correct |
3 ms |
14172 KB |
Output is correct |
9 |
Incorrect |
123 ms |
14444 KB |
Output isn't correct |
10 |
Halted |
0 ms |
0 KB |
- |