#include <bits/stdc++.h>
using namespace std;
#define int long long
const int INF = 1e13;
int32_t main(){
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
int n,m,k;
cin >> n >> m >> k;
vector<vector<int>> adj(n+1);
for(int i=1;i<=m;i++){
int u,v;cin>>u>>v;
adj[u].emplace_back(v);
adj[v].emplace_back(u);
}
vector<bool> isStop(n+1);
bool works = false;
for(int i=1;i<=n;i++){
char a;cin>>a;
if(a=='1'){works=isStop[i]=true;}
}
vector<int> starts(k+1);
for(int i=1;i<=k;i++)cin>>starts[i];
vector<int> minmoves(n+1,INF);
minmoves[0]=0;
if(works){
// calculate minmoves
vector<int> nearest_single(n+1,-1);
priority_queue<pair<int,int>> pq;
for(int i=1;i<=n;i++)if(isStop[i])for(int&x:adj[i])pq.emplace(-1,x);
vector<bool> visited(n+1);
while(!pq.empty()){
auto [dist,idx] = pq.top();pq.pop();dist=-dist;
if(visited[idx])continue;
visited[idx]=true;
nearest_single[idx]=dist;
for(int&i:adj[idx])if(!visited[i])pq.emplace(-dist-1,i);
}
vector<int> nearest_double(n+1,-1);
vector<bool> flagged(n+1);
for(int i=1;i<=n;i++)if(isStop[i]){
for(int&x:adj[i]){
if(isStop[x]){
flagged[i]=true;
for(int&y:adj[i])pq.emplace(0,y);
break;
}
}
}
visited = vector<bool>(n+1);
while(!pq.empty()){
auto [dist,idx] = pq.top();pq.pop();dist=-dist;
if(visited[idx])continue;
visited[idx]=true;
nearest_double[idx]=dist;
if(isStop[idx] and !flagged[idx])nearest_double[idx]++;
for(int&i:adj[idx])if(!visited[i]){
if(!isStop[i])pq.emplace(-dist-1,i);
else pq.emplace(-dist,i);
}
}
vector<int> slopeedit(n+1);
int base = 0;
auto process = [&](int x){
base+=nearest_single[x];
slopeedit[1]+=2;
if(nearest_double[1]!=-1 and nearest_double[x]-nearest_single[x]+2<=n)slopeedit[nearest_double[x]-nearest_single[x]+2]--;
};
for(int i=2;i<=k;i++){
process(starts[i]);
}
int currslope = 0;
for(int i=1;i<=n;i++){
base+=currslope;
minmoves[i]=base;
currslope+=slopeedit[i];
}
for(int i=n;i;i--)minmoves[i]-=minmoves[i-1];
}
vector<int> currbest(n+1,INF);
vector<int> ans(n+1,INF);
vector<bool> visited(n+1);
priority_queue<tuple<int,int,int>> pq;
pq.emplace(0,0,starts[1]);
while(!pq.empty()){
auto [stops,dist,idx] = pq.top();pq.pop();dist=-dist;stops=-stops;
if(visited[idx])continue;
if(isStop[idx])currbest[idx] = dist-minmoves[stops];
else currbest[idx] = dist;
visited[idx] = true;
for(int&i:adj[idx]){
if(isStop[i]){
if(dist+1+minmoves[stops+1]<currbest[i]){
currbest[i] = dist+1+minmoves[stops+1];
pq.emplace(-stops-1,-dist-1-minmoves[stops+1],i);
visited[i]=false;
}
ans[i]=min(ans[i],dist+1);
} else {
if(dist+1<currbest[i]){
currbest[i] = dist+1;
pq.emplace(-stops,-dist-1,i);
visited[i]=false;
}
}
}
}
for(int i=1;i<=n;i++)cout<<min(currbest[i],ans[i])<<'\n';
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
604 KB |
Output is correct |
2 |
Correct |
13 ms |
3676 KB |
Output is correct |
3 |
Correct |
23 ms |
5968 KB |
Output is correct |
4 |
Correct |
18 ms |
5468 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
604 KB |
Output is correct |
2 |
Correct |
16 ms |
3932 KB |
Output is correct |
3 |
Correct |
20 ms |
5468 KB |
Output is correct |
4 |
Correct |
21 ms |
5468 KB |
Output is correct |
5 |
Correct |
30 ms |
6224 KB |
Output is correct |
6 |
Correct |
26 ms |
5724 KB |
Output is correct |
7 |
Correct |
21 ms |
4936 KB |
Output is correct |
8 |
Correct |
20 ms |
5260 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
600 KB |
Output is correct |
2 |
Correct |
17 ms |
3932 KB |
Output is correct |
3 |
Correct |
26 ms |
5468 KB |
Output is correct |
4 |
Correct |
21 ms |
5572 KB |
Output is correct |
5 |
Correct |
22 ms |
6084 KB |
Output is correct |
6 |
Correct |
26 ms |
5468 KB |
Output is correct |
7 |
Correct |
26 ms |
5720 KB |
Output is correct |
8 |
Correct |
24 ms |
6080 KB |
Output is correct |
9 |
Correct |
18 ms |
3304 KB |
Output is correct |
10 |
Correct |
17 ms |
3340 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
600 KB |
Output is correct |
2 |
Correct |
2 ms |
604 KB |
Output is correct |
3 |
Correct |
1 ms |
724 KB |
Output is correct |
4 |
Correct |
2 ms |
604 KB |
Output is correct |
5 |
Correct |
2 ms |
596 KB |
Output is correct |
6 |
Correct |
1 ms |
604 KB |
Output is correct |
7 |
Correct |
2 ms |
604 KB |
Output is correct |
8 |
Correct |
3 ms |
860 KB |
Output is correct |
9 |
Incorrect |
2 ms |
604 KB |
Output isn't correct |
10 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
25 ms |
5724 KB |
Output is correct |
2 |
Correct |
34 ms |
6164 KB |
Output is correct |
3 |
Correct |
22 ms |
5464 KB |
Output is correct |
4 |
Incorrect |
58 ms |
6872 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
25 ms |
5724 KB |
Output is correct |
2 |
Correct |
34 ms |
6164 KB |
Output is correct |
3 |
Correct |
22 ms |
5464 KB |
Output is correct |
4 |
Incorrect |
58 ms |
6872 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
600 KB |
Output is correct |
2 |
Correct |
2 ms |
604 KB |
Output is correct |
3 |
Correct |
1 ms |
724 KB |
Output is correct |
4 |
Correct |
2 ms |
604 KB |
Output is correct |
5 |
Correct |
2 ms |
596 KB |
Output is correct |
6 |
Correct |
1 ms |
604 KB |
Output is correct |
7 |
Correct |
2 ms |
604 KB |
Output is correct |
8 |
Correct |
3 ms |
860 KB |
Output is correct |
9 |
Incorrect |
2 ms |
604 KB |
Output isn't correct |
10 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
604 KB |
Output is correct |
2 |
Correct |
13 ms |
3676 KB |
Output is correct |
3 |
Correct |
23 ms |
5968 KB |
Output is correct |
4 |
Correct |
18 ms |
5468 KB |
Output is correct |
5 |
Correct |
2 ms |
604 KB |
Output is correct |
6 |
Correct |
16 ms |
3932 KB |
Output is correct |
7 |
Correct |
20 ms |
5468 KB |
Output is correct |
8 |
Correct |
21 ms |
5468 KB |
Output is correct |
9 |
Correct |
30 ms |
6224 KB |
Output is correct |
10 |
Correct |
26 ms |
5724 KB |
Output is correct |
11 |
Correct |
21 ms |
4936 KB |
Output is correct |
12 |
Correct |
20 ms |
5260 KB |
Output is correct |
13 |
Correct |
2 ms |
600 KB |
Output is correct |
14 |
Correct |
17 ms |
3932 KB |
Output is correct |
15 |
Correct |
26 ms |
5468 KB |
Output is correct |
16 |
Correct |
21 ms |
5572 KB |
Output is correct |
17 |
Correct |
22 ms |
6084 KB |
Output is correct |
18 |
Correct |
26 ms |
5468 KB |
Output is correct |
19 |
Correct |
26 ms |
5720 KB |
Output is correct |
20 |
Correct |
24 ms |
6080 KB |
Output is correct |
21 |
Correct |
18 ms |
3304 KB |
Output is correct |
22 |
Correct |
17 ms |
3340 KB |
Output is correct |
23 |
Correct |
1 ms |
600 KB |
Output is correct |
24 |
Correct |
2 ms |
604 KB |
Output is correct |
25 |
Correct |
1 ms |
724 KB |
Output is correct |
26 |
Correct |
2 ms |
604 KB |
Output is correct |
27 |
Correct |
2 ms |
596 KB |
Output is correct |
28 |
Correct |
1 ms |
604 KB |
Output is correct |
29 |
Correct |
2 ms |
604 KB |
Output is correct |
30 |
Correct |
3 ms |
860 KB |
Output is correct |
31 |
Incorrect |
2 ms |
604 KB |
Output isn't correct |
32 |
Halted |
0 ms |
0 KB |
- |