이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
using namespace std;
const int N = 100005;
const int INF = 1e9;
vector<pair<int, int>> adj[N];
int32_t main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
int n, m;
cin >> n >> m;
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});
}
vector<int> dis(N, INF);
priority_queue<pair<int, int>, vector<pair<int, int>>, greater<pair<int, int>>> pq;
int shaoji;
cin >> shaoji;
for (int i = 0; i < shaoji; i++) {
int k;
cin >> k;
dis[k]=0;
pq.push({0,k});
}
while(pq.size()){
int a,b;tie(a,b)=pq.top();
pq.pop();
if(a!=dis[b]){
continue;
}
for(auto &e:adj[b]){
if(dis[e.first]>a+e.second){
dis[e.first]=a+e.second;
pq.push({dis[e.first],e.first});
}
}
}
int q;cin>>q;
for(int i=0;i<q;i++){
int a,b;cin>>a>>b;
cout<<min(dis[a],dis[b])<<endl;
}
return 0;
}
/*
9 12
1 9 4
1 2 5
2 3 7
2 4 3
4 3 6
3 6 4
8 7 10
6 7 5
5 8 1
9 5 7
5 4 12
6 8 2
2
4 7
*/
# | 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... |