이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#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... |