This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <iostream>
#include <vector>
#include <queue>
#include <algorithm>
using namespace std;
const int oo = 2e9;
int n, m, k, q, s, t, d[100001], mind, ans, cc[100001];
bool f[100001];
struct elem {int id, cost;};
vector<elem> g[100001];
vector<int> g2[100001];
struct comp
{
bool operator()(int i, int j)
{
return d[i] > d[j];
}
};
priority_queue<int, vector<int>, comp> pq;
struct edge {int x, y;};
vector<edge> e;
bool comp2(const edge& a, const edge& b)
{
return min(d[a.x], d[a.y]) > min(d[b.x], d[b.y]);
}
void read()
{
cin >> n >> m;
for(int i = 0, x, y, c; i < m; ++i)
{
cin >> x >> y >> c;
g[x].push_back({y, c});
g[y].push_back({x, c});
e.push_back({x, y});
}
for(int i = 1; i <= n; ++i)
d[i] = oo, cc[i] = i;
cin >> k;
for(int i = 0, x; i < k; ++i)
{
cin >> x;
pq.push(x);
f[x] = 1;
d[x] = 0;
}
cin >> q;
}
void dijkstra()
{
int x;
while(!pq.empty())
{
x = pq.top();
f[x] = 0;
pq.pop();
for(auto i : g[x])
if(d[x] + i.cost < d[i.id])
{
d[i.id] = d[x] + i.cost;
if(f[i.id] == 0)
{
pq.push(i.id);
f[i.id] = 1;
}
}
}
}
void U(int x, int y)
{
int val = cc[y];
for(int i = 1; i <= n; ++i)
if(cc[i] == val)
cc[i] = cc[x];
}
void kruskal()
{
int nr = 0;
for(auto i : e)
{
if(cc[i.x] != cc[i.y])
{
U(i.x, i.y);
g2[i.x].push_back(i.y);
g2[i.y].push_back(i.x);
if(++nr == n-1) break;
}
}
}
void dfs(int nod)
{
if(nod == t) ans = min(mind, d[t]);
else
{
int aux = mind;
mind = min(mind, d[nod]);
f[nod] = 1;
for(auto i : g2[nod])
if(!f[i])
dfs(i);
f[nod] = 0;
mind = aux;
}
}
void solve()
{
cin >> s >> t;
if(d[s] == 0 || d[t] == 0)
{
cout << 0 << '\n';
return;
}
for(auto i : g[s])
if(i.id == t)
{
cout << min(d[s], d[t]) << '\n';
return;
}
mind = d[s];
dfs(s);
cout << ans << '\n';
}
int main()
{
read();
dijkstra();
sort(e.begin(), e.end(), comp2);
kruskal();
for(int i = 0; i < q; ++i)
solve();
return 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... |