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;
#define MAX_N 100000
#define MAX_DIST 500000000
struct muchie {
int vec, dist;
};
struct aaa {
int nod, dist;
bool operator < (const aaa &aux) const {
return dist > aux.dist;
}
};
int ans[MAX_N];
struct dsu {
int sef[MAX_N];
vector <int> comp[MAX_N];
vector <int> aux;
void init( int n ) {
int i;
for ( i = 0; i < n; i++ )
sef[i] = i;
}
int findSef( int x ) {
if ( sef[x] != x )
sef[x] = findSef( sef[x] );
return sef[x];
}
void join( int x, int y, int danger ) {
int i, j;
x = findSef( x );
y = findSef( y );
if ( x == y )
return;
i = j = 0;
while ( i < comp[x].size() && j < comp[y].size() ) {
if ( comp[x][i] < comp[y][j] ) {
aux.push_back( comp[x][i] );
i++;
} else {
aux.push_back( comp[y][j] );
j++;
}
if ( aux.size() >= 2 && aux[aux.size() - 1] == aux[aux.size() - 2] ) {
ans[aux[aux.size() - 1]] = danger;
aux.pop_back();
aux.pop_back();
}
}
while ( i < comp[x].size() ) {
aux.push_back( comp[x][i] );
i++;
if ( aux.size() >= 2 && aux[aux.size() - 1] == aux[aux.size() - 2] ) {
ans[aux[aux.size() - 1]] = danger;
aux.pop_back();
aux.pop_back();
}
}
while ( j < comp[y].size() ) {
aux.push_back( comp[y][j] );
j++;
if ( aux.size() >= 2 && aux[aux.size() - 1] == aux[aux.size() - 2] ) {
ans[aux[aux.size() - 1]] = danger;
aux.pop_back();
aux.pop_back();
}
}
sef[y] = x;
comp[x].clear();
comp[y].clear();
comp[x] = aux;
aux.clear();
}
};
struct bbb {
int dist, nod;
bool operator < (const bbb &a) const {
if ( dist == a.dist )
return nod < a.nod;
return dist < a.dist;
}
};
int dist[MAX_N], viz[MAX_N];
vector <muchie> muchii[MAX_N];
priority_queue <aaa> pq;
dsu ds;
bbb v[MAX_N];
int main() {
int n, m, k, q, x, y, d, i, j;
aaa aux;
cin >> n >> m;
for ( i = 0; i < m; i++ ) {
cin >> x >> y >> d;
x--;
y--;
muchii[x].push_back( { y, d } );
muchii[y].push_back( { x, d } );
}
for ( i = 0; i < n; i++ )
dist[i] = MAX_DIST + 1;
cin >> k;
for ( i = 0; i < k; i++ ) {
cin >> x;
x--;
dist[x] = 0;
pq.push( { x, 0 } );
}
cin >> q;
for ( i = 0; i < q; i++ ) {
cin >> x >> y;
x--;
y--;
ds.comp[x].push_back( i );
ds.comp[y].push_back( i );
}
while ( !pq.empty() ) {
aux = pq.top();
pq.pop();
x = aux.nod;
if ( viz[x] == 0 ) {
viz[x] = 1;
for ( i = 0; i < muchii[x].size(); i++ ) {
y = muchii[x][i].vec;
d = muchii[x][i].dist;
if ( dist[x] + d < dist[y] ) {
dist[y] = dist[x] + d;
pq.push( { y, dist[y] } );
}
}
}
}
for ( i = 0; i < n; i++ )
v[i] = { dist[i], i };
sort( v, v + n );
ds.init( n );
for ( i = n - 1; i >= 0; i-- ) {
x = v[i].nod;
for ( j = 0; j < muchii[x].size(); j++ ) {
y = muchii[x][j].vec;
if ( bbb{ dist[x], x } < bbb{ dist[y], y } )
ds.join( x, y, dist[x] );
}
}
for ( i = 0; i < q; i++ )
cout << ans[i] << "\n";
return 0;
}
Compilation message (stderr)
plan.cpp: In member function 'void dsu::join(int, int, int)':
plan.cpp:52:19: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
52 | while ( i < comp[x].size() && j < comp[y].size() ) {
| ~~^~~~~~~~~~~~~~~~
plan.cpp:52:41: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
52 | while ( i < comp[x].size() && j < comp[y].size() ) {
| ~~^~~~~~~~~~~~~~~~
plan.cpp:66:19: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
66 | while ( i < comp[x].size() ) {
| ~~^~~~~~~~~~~~~~~~
plan.cpp:75:19: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
75 | while ( j < comp[y].size() ) {
| ~~^~~~~~~~~~~~~~~~
plan.cpp: In function 'int main()':
plan.cpp:149:28: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<muchie>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
149 | for ( i = 0; i < muchii[x].size(); i++ ) {
| ~~^~~~~~~~~~~~~~~~~~
plan.cpp:167:24: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<muchie>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
167 | for ( j = 0; j < muchii[x].size(); j++ ) {
| ~~^~~~~~~~~~~~~~~~~~
# | 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... |