이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
//#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;
int ans[maxn];
vector<pair<int,int>>g[maxn];
int a[maxn],b[maxn],c[maxn];
int dst[maxn];
int pr[maxn];
int sz[maxn];
int x[maxn],y[maxn];
pair<int,int>qur[maxn];
vector<int>pos[maxn],mid[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});
}
}
}
for(int i = 1 ; i <= m ; i ++){
pos[min(dst[a[i]],dst[b[i]])].pb(i);
}
cin >> q;
for(int i = 1 ; i <= q ; i ++){
cin >> x[i] >> y[i];
qur[i] = {0,2000};
}
for(int I = 1 ; I <= 20 ; I ++){
for(int i = 0 ; i <= 2000 ; i ++)mid[i].clear();
for(int i = 1 ; i <= n ; i ++){
pr[i] = i;
sz[i] = 1;
}
for(int i = 1 ; i <= q ; i ++){
if(qur[i].first <= qur[i].second){
int m = (qur[i].first + qur[i].second) >> 1;
mid[m].pb(i); }
}
for(int i = 2000 ; i >= 0 ; i --){
for(auto to : pos[i]){
unite(a[to],b[to]);
}
for(auto to : mid[i]){
if(get(x[to]) == get(y[to])){
qur[to].first = i + 1;
ans[to] = i;
}
else{
qur[to].second = i - 1;
}
}
}
}
for(int i = 1 ; i <= q ; i++){
cout << ans[i] << 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;
}
컴파일 시 표준 에러 (stderr) 메시지
plan.cpp: In function 'int main()':
plan.cpp:145:6: warning: unused variable 'test' [-Wunused-variable]
145 | int test = 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... |