이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include<bits/stdc++.h>
#define int long long
#define pb push_back
#define fi first
#define se second
using namespace std;
using ll = long long;
using ld = long double;
using ull = unsigned long long;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
const int maxN = 1e5 + 5;
const int mod = 1e9 + 7;
const ll oo = 1e9;
int n, m;
int lab[maxN];
int d[maxN];
int id[maxN];
int Findset(int u)
{
return lab[u] < 0 ? u : lab[u] = Findset(lab[u]);
}
void Unite(int u, int v)
{
int r = Findset(u), s = Findset(v);
if(r == s) return;
if(lab[r] > lab[s]) swap(r, s);
lab[r] += lab[s];
lab[s] = r;
}
struct TEdge
{
int u, v, w;
} e[maxN];
struct TAdj
{
int v, w;
};
vector<TAdj> adj[maxN];
int k, a[maxN];
struct TPQRect
{
int vertex, dlab;
bool operator < (const TPQRect& other) const
{
return other.dlab < dlab;
}
bool Valid() const
{
return d[vertex] == dlab;
}
};
bool Relax(int x, int y, int w)
{
if(d[y] > d[x] + w)
{
d[y] = d[x] + w;
return true;
}
return false;
}
void Dijkstra()
{
priority_queue<TPQRect> pq;
memset(d, 3, sizeof d);
for(int i=1; i<=k; i++)
{
d[a[i]] = 0;
pq.push({a[i], 0});
}
do
{
TPQRect r = pq.top();
pq.pop();
if(!r.Valid()) continue;
int u = r.vertex;
for(TAdj a : adj[u])
{
if(Relax(u, a.v, a.w))
pq.push({a.v, d[a.v]});
}
}
while(!pq.empty());
}
void ReadInput()
{
cin >> n >> m;
for(int i=1; i<=m; i++)
{
int u, v, w;
cin >> u >> v >> w;
e[i] = {u, v, w};
adj[u].pb({v, w});
adj[v].pb({u, w});
}
cin >> k;
for(int i=1; i<=k; i++) cin >> a[i];
}
bool chk(int s, int t, int val)
{
memset(lab, -1, sizeof lab);
for(int i=1; i<=m; i++)
if(d[e[i].u] >= val && d[e[i].v] >= val) Unite(e[i].u, e[i].v);
return Findset(s) == Findset(t);
}
void Solve()
{
Dijkstra();
int q;
cin >> q;
while(q--)
{
int s, t;
cin >> s >> t;
int low = 1, high = oo, mid;
while(low <= high)
{
mid = (low + high) / 2;
if(chk(s, t, mid)) low = mid + 1;
else high = mid - 1;
}
cout << high << '\n';
}
}
int32_t main()
{
// freopen("x.inp", "r", stdin);
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
ReadInput();
Solve();
}
# | 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... |