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 <bits/stdc++.h>
#define ll long long
#define pb push_back
#define mp make_pair
using namespace std;
const int maxn = 100100;
ll n, m, k, a, b;
ll dista[maxn], distb[maxn];
vector<int>g[maxn];
set<int>edge[maxn];
vector<int>unsolved[maxn];
bool visiteda[maxn], visitedb[maxn];
void bfs() {
for(int i=1;i<=n;i++) dista[i] = 1e9;
queue<int>q;
visiteda[k] = true;
dista[k] = 0;
q.push(k);
while(!q.empty()) {
int curr = q.front();
q.pop();
for(int i:g[curr]) {
if(!visiteda[i]) {
visiteda[i] = true;
dista[i] = dista[curr] + 1;
q.push(i);
}
}
}
}
void bfsb() {
for(int i=1;i<=n;i++) {
distb[i] = 1e9;
}
queue<int>q;
visitedb[k] = true;
distb[k] = 0;
q.push(k);
while(!q.empty()) {
int curr = q.front(); q.pop();
for(int i:g[curr]) {
vector<int>temp;
for(int j:unsolved[i]) {
if(visitedb[j]) continue;
temp.pb(j);
if(edge[j].find(curr) != edge[j].end()) {
continue;
}
visitedb[j] = true;
distb[j] = distb[curr] + 1;
q.push(j);
}
unsolved[i] = temp;
}
}
}
int main() {
cin>>n>>m>>k>>a>>b;
int x, y;
for(int i=0;i<m;i++) {
cin>>x>>y;
g[x].pb(y); g[y].pb(x);
edge[x].insert(y); edge[y].insert(x);
unsolved[x].pb(y); unsolved[y].pb(x);
}
bfs();
bfsb();
for(int i=1;i<=n;i++) {
if(dista[i] % 2 == 0) {
cout<<min(dista[i]*a, (dista[i]/2)*b)<<"\n";
}
else {
ll cb = INT_MAX;
if(visitedb[i]) cb = distb[i] * b;
cout<<min(min(dista[i] * a, ((dista[i]-1)/2*b)+a),cb)<<"\n";
}
}
}
# | 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... |
# | 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... |