이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
using namespace std;
#define int ll
typedef long long ll;
typedef long double ld;
typedef pair<int, int> pi;
typedef pair<ll, ll> pl;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef vector<double> vd;
typedef vector<bool> vb;
typedef vector<char> vc;
typedef vector<string> vs;
typedef vector<pi> vp;
typedef vector<pl> vpl;
const int maxn = 1e5+5;
vp e[maxn];
signed main()
{
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
cerr.tie(nullptr);
int n, m;
cin >> n >> m;
for (int i = 0; i < m; ++i){
int u, v, w;
cin >> u >> v >> w;
e[u].push_back({v, w});
e[v].push_back({u, w});
}
int k;
cin >> k;
queue<pi> q;
vi d(n+1,INT64_MAX/10);
for (int i = 0; i < k; ++i){
int x;
cin >> x;
d[x] = 0;
q.push({0, x});
}
while(!q.empty()){
int c = q.front().second, dist = q.front().first;
q.pop();
if (d[c] < dist) continue;
for (pi p : e[c]){
if (d[p.first] > p.second + dist){
d[p.first] = p.second+dist;
q.push({d[p.first], p.first});
}
}
}
int Q;
cin >> Q;
for (int i = 0; i < Q; ++i){
int u, v;
cin >> u >> v;
cout << min(d[u], d[v]) << '\n';
}
}
/*
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
5
1 6
5 3
4 8
5 8
1 5
*/
# | 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... |