이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include<bits/stdc++.h>
#define endl '\n'
#define mod 1000000007
#define boost ios_base::sync_with_stdio(false), cin.tie(NULL);
using namespace std;
vector<pair<int,int>>adj[200001];
int dist[200001],npp[200001];
vector<pair<int,int>> queries[200001];
vector<int> cmp[200001]; int vs[200001],cm[200001], ans[200001];
void merge(int a, int b, int dg){
if(cm[a] == cm[b]) return;
if(cmp[cm[a]].size() > cmp[cm[b]].size()) swap(a,b);
for(auto s: cmp[cm[a]]){
for(auto f : queries[s]){
if(cm[f.first] == cm[b]) ans[f.second] = dg;
}
}
for(auto s: cmp[cm[a]]){
cm[s] = cm[b];
cmp[cm[b]].push_back(s);
}
}
signed main(){
boost;
int n,m; cin >> n >> m;
set<pair<int,int>>d;
for(int i = 0; i < m; i++){
int a,b,c; cin >> a >> b >> c;
adj[a].push_back({b,c});
adj[b].push_back({a,c});
if(a > b) swap(a,b);
d.insert({a,b});
}
int k; cin >> k;
set<pair<int,int>>bfs;
for(int i = 1; i <= n; i++) npp[i] = INT_MAX;
for(int i = 1; i <= k; i++) {
int x; cin >> x;
npp[x] = 0;
bfs.insert({0,x});
}
while(bfs.size()){
auto x = *bfs.begin();
bfs.erase(x);
if(npp[x.second] < x.first) continue;
for(auto s : adj[x.second]){
if(npp[x.second] + s.second < npp[s.first]){
npp[s.first] = npp[x.second] + s.second;
bfs.insert({npp[s.first], s.first});
}
}
}
int q; cin >> q;
for(int i = 0; i < q; i++){
int a,b; cin >> a >> b;
queries[a].push_back({b,i});
queries[b].push_back({a,i});
}
vector<pair<int,int>>x;
for(int i = 1; i <= n; i++){
cm[i] = i;
cmp[i].push_back(i);
x.push_back({npp[i],i});
}
sort(x.begin(), x.end(), greater<pair<int,int>>());
for(int i = 0; i < x.size(); i++){
int k = x[i].second;
for(auto s: adj[k]){
if(vs[s.first]){
merge(k,s.first,x[i].first);
}
}
vs[k] = 1;
}
for(int i = 0; i < q; i++)cout << ans[i] << endl;
return 0;
}
컴파일 시 표준 에러 (stderr) 메시지
plan.cpp: In function 'int main()':
plan.cpp:86:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
86 | for(int i = 0; i < x.size(); i++){
| ~~^~~~~~~~~~
# | 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... |