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 <set>
#include <algorithm>
using namespace std;
const int max_n = 1e5;
const int INF = 1e9;
vector<int> power_dist(1+max_n, INF);
struct city
{
int i;
};
bool operator < (city A, city B)
{
if(power_dist[A.i] == power_dist[B.i]) return A.i < B.i;
return power_dist[A.i] < power_dist[B.i];
}
vector<int> merge_cc[1+max_n], merge_dist[1+max_n];
vector<int> col_set[1+max_n];
vector<int> node_col(1+max_n);
void merge(int a, int b)
{
if(node_col[a] == node_col[b]) return;
if(col_set[ node_col[a] ].size() < col_set[ node_col[b] ].size()) swap(a, b);
int colA = node_col[a];
int colB = node_col[b];
for(int z: col_set[colB])
{
node_col[z] = colA;
col_set[colA].push_back(z);
merge_cc[z].push_back(colA);
merge_dist[z].push_back(min(power_dist[a], power_dist[b]));
}
col_set[colB].clear();
// cerr << "merging " << a << ", " << b << " at " << min(power_dist[a], power_dist[b]) << '\n';
}
int main()
{
int n, m;
cin >> n >> m;
vector<int> edge[1+n], dist[1+n];
for(int j = 1; j <= m; j++)
{
int a, b, w;
cin >> a >> b >> w;
edge[a].push_back(b);
dist[a].push_back(w);
edge[b].push_back(a);
dist[b].push_back(w);
}
int k;
cin >> k;
for(int p = 1; p <= k; p++)
{
int g;
cin >> g;
power_dist[g] = 0;
}
set<city> tbv;
for(int i = 1; i <= n; i++) tbv.insert(city{i});
while(!tbv.empty())
{
int u = tbv.begin()->i;
tbv.erase(tbv.begin());
for(int e = 0; e < edge[u].size(); e++)
{
int v = edge[u][e];
int w = dist[u][e];
if(power_dist[v] > power_dist[u] + w)
{
tbv.erase(city{v});
power_dist[v] = power_dist[u] + w;
tbv.insert(city{v});
}
}
}
// for(int u = 1; u <= n; u++) cerr << u << ": " << power_dist[u] << '\n';
vector<int> power_sort(n);
for(int i = 0; i < n; i++) power_sort[i] = i+1;
sort(power_sort.begin(), power_sort.end(), [] (int x, int y)
{
return power_dist[x] > power_dist[y];
});
for(int i = 1; i <= n; i++)
{
node_col[i] = i;
col_set[i].push_back(i);
merge_cc[i].push_back(i);
merge_dist[i].push_back(INF);
}
for(int p: power_sort)
{
for(int q: edge[p])
{
// cerr << "p = " << p << ", q = " << q << '\n';
if(power_dist[q] >= power_dist[p])
merge(p, q);
}
}
int Q;
cin >> Q;
for(int q = 1; q <= Q; q++)
{
int s, t;
cin >> s >> t;
int ans = 0;
// cerr << "s = " << s << ", t = " << t << '\n';
for(int e1 = 0; e1 < merge_cc[s].size(); e1++)
{
for(int e2 = 0; e2 < merge_cc[t].size(); e2++)
{
// cerr << s << ' ' << e1 << ' ' << merge_cc[s][e1] << ' ' << merge_dist[s][e1] << ", ";
// cerr << t << ' ' << e2 << ' ' << merge_cc[t][e2] << ' ' << merge_dist[t][e2] << "\n";
if(merge_cc[s][e1] == merge_cc[t][e2])
{
ans = max(ans, min(merge_dist[s][e1], merge_dist[t][e2]));
}
}
}
cout << ans << '\n';
}
}
Compilation message (stderr)
plan.cpp: In function 'int main()':
plan.cpp:87:26: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
87 | for(int e = 0; e < edge[u].size(); e++)
| ~~^~~~~~~~~~~~~~~~
plan.cpp:148:28: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
148 | for(int e1 = 0; e1 < merge_cc[s].size(); e1++)
| ~~~^~~~~~~~~~~~~~~~~~~~
plan.cpp:150:32: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
150 | for(int e2 = 0; e2 < merge_cc[t].size(); e2++)
| ~~~^~~~~~~~~~~~~~~~~~~~
# | 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... |