/* Author : Mychecksdead */
#include<bits/stdc++.h>
using namespace std;
#define ll long long int
#define MOD (1000000000+7)
#define MOD1 (998244353)
#define pb push_back
#define all(x) x.begin(), x.end()
#define en cout << '\n'
#define ff first
#define ss second
#define pii pair<int,int>
#define vi vector<int>
const int N = 1e6+100, M = 50000+10, K = 52, MX = 30;
int n, m, k, a[N], is[N];
vector<int> mn(M);
vi g[N];
ll DP[N];
array<ll, 3> res[N][2]; // 2 dif results for players for 2...k
// vector<int> DIST[M];
// bitset<M> vis[M];
// bool put(int v, int p, int d){
// if(DIST[v].empty()){
// mn[v] = p;
// }
// if(p < mn[v]) return false;
// while(DIST[v].size() <= p - mn[v]) DIST[v].pb(INT_MAX);
// DIST[v][p - mn[v]] = d;
// return true;
// }
void solve(){
cin >> n >> m >> k;
for(int i = 1; i <= m; ++i){
int u, v; cin >> u >> v;
g[u].pb(v);
g[v].pb(u);
}
string s; cin >> s;
for(int i = 1; i <= n; ++i){
if(s[i-1] == '0') is[i] = 1;
else is[i] = 0;
}
for(int i = 1; i <= k; ++i){
cin >> a[i];
}
vector<int> dist(n + 1, N);
vector<pii> dist2(n + 1, pii{N, N});
queue<int> q;
priority_queue<array<int, 3>> qq;
for(int i = 1; i <= n; ++i){
if(is[i] == 0){
dist[i] = 0;
q.push(i);
}
}
while(!q.empty()){
int v = q.front(); q.pop();
for(int u: g[v]){
if(dist[u] == N){
dist[u] = dist[v] + 1;
q.push(u);
}
}
}
for(int i = 1; i <= n; ++i){
if(is[i] == 0){
for(int u: g[i]){
if(is[u] == 0){
dist2[i] = {0, 0};
qq.push({0, 0, i});
break;
}
}
}
}
vector<bool> VIS(n + 1);
while(!qq.empty()){
int v = qq.top()[2]; qq.pop();
if(VIS[v]) continue;
VIS[v] = 1;
for(int u: g[v]){
int new_dist = dist2[v].ff + (is[v] == 0 ? 0 : 1);
if(dist2[u].ff > new_dist){
dist2[u] = {new_dist, dist2[v].ss};
if(is[v] == 0){
dist2[u].ss++;
}
qq.push({-dist2[u].ff, -(int)(is[u]), u});
}
}
}
for(int i = 0; i <= n; ++i){
DP[i] = 0;
}
int cnt_1 = 0, cnt_2 = 0;
vector<vi> slope_1(n + 1);
for(int i = 2; i <= k; ++i){
// first one
res[i][0] = {1, dist[a[i]], 2}; // at first, dist[a[i]], then just +2 +2 +2 +2...
res[i][1] = {dist2[a[i]].ss, dist2[a[i]].ff + dist2[a[i]].ss, 1}; // we will wait for dist2[a[i]].ss phases to take it, then +1 +1 +1 +1...
if(res[i][1][0] == 0){
// all to 1
cnt_1++;
DP[1] += 1;
// cerr << " one\n";
}else{
if(res[i][0][1] == 0){
res[i][0][1] = 2;
}
int k1 = res[i][0][1], m1 = 2;
int k2 = res[i][1][1], m2 = 1;
int y = res[i][1][0];
int x = k2 - k1 - y + 2;// first time tp-1 crosses tp-2
// so: k1, k1+2, k1+4.....k1+2*tm-2, k2 + (x-y), k2+(x-y)+1 ....
// cerr << x << ' ' << k1 << ' ' << k2 << ' ' << y << '\n';
if(x <= 1){
cnt_1++;
DP[1] += k2;
}else{
cnt_2++;
DP[1] += k1;
if(x <= n + 1)
slope_1[x].pb(k2 + (x - y) - (k1 + 2 * x - 2)); // switch...
}
}
}
for(int i = 2; i <= n; ++i){
DP[i] = DP[i - 1] + cnt_2 * 2 + cnt_1;
for(int val: slope_1[i]){
cnt_2--;
cnt_1++;
DP[i] += val;
}
}
// for(int i = 1; i <= n; ++i){
// cerr << DP[i] << '\n';
// }
priority_queue<array<ll, 3>> Q; // -dist, phase, node
Q.push({0, 0, a[1]});
vector<int> vis(n + 1);
vector<ll> DIST(n + 1, (ll)(1e18));
DIST[a[1]] = 0;
while(!Q.empty()){
int v = Q.top()[2], phase = -Q.top()[1]; Q.pop();
// cerr << v << ' ' << phase << '\n';
if(vis[v] == 10) continue;
vis[v]++;
for(int u: g[v]){
ll w = (is[v] == 0 && v != a[1] ? DP[phase + 1] - DP[phase] : 0ll) + 1;
if(vis[u] < 10 && DIST[u] > DIST[v] + w){
DIST[u] = DIST[v] + w;
Q.push({-DIST[u], -(is[v] == 0 && v != a[1] ? phase + 1 : phase), u});
}
}
}
// for(int i = 1; i <= n; ++i){
// cerr << i << ": " << DP[i] << '\n';
// }
// en;
for(int i = 1; i <= n; ++i){
cout << DIST[i] << '\n';
}
}
int main(){
cin.tie(0); ios::sync_with_stdio(0);
int tt = 1, aa;
// freopen("in.txt", "r", stdin);
// freopen("out.txt", "w", stdout);
while(tt--){
solve();
en;
}
cerr<<"time taken : "<<(float)clock()/CLOCKS_PER_SEC<<" seconds\n";
return 0;
}
# | 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... |