This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#pragma GCC optimize("Ofast")
#include<bits/stdc++.h>
using namespace std;
#define pb push_back
#define fi first
#define se second
#define sz(a) (int)(a.size())
#define all(a) a.begin(),a.end()
#define lb lower_bound
#define ub upper_bound
#define owo ios_base::sync_with_stdio(0);cin.tie(0);
#define debug(...) fprintf(stderr, __VA_ARGS__),fflush(stderr)
#define time__(d) for(long blockTime = 0; (blockTime == 0 ? (blockTime=clock()) != 0 : false);\
debug("%s time : %.4fs\n", d, (double)(clock() - blockTime) / CLOCKS_PER_SEC))
typedef long long int ll;
typedef long double ld;
typedef pair<ll,ll> PII;
typedef pair<int,int> pii;
typedef vector<vector<int>> vii;
typedef vector<vector<ll>> VII;
ll gcd(ll a,ll b){if(!b)return a;else return gcd(b,a%b);}
const int MAXN = 5e4+50;
vector<int>adj[MAXN];
ll ans[MAXN];
int main()
{
owo
int n,m,k;
cin>>n>>m>>k;
for(int i=0;i<n;i++)ans[i] = 1e18;
for(int i=0;i<m;i++){
int u,v;
cin>>u>>v;
u--;
v--;
adj[u].pb(v);
adj[v].pb(u);
}
string s;
cin>>s;
vector<int>T(k);
for(int i=0;i<k;i++){cin>>T[i];T[i]--;}
vector<ll>rdist(n,1e9),bdist(n,1e9),ndist(n,1e9),ones;
deque<int>q;
if(true){
queue<int>q;
q.push(T[0]);
vector<ll>d(n,n);
d[T[0]] = 0;
while(!q.empty()){
int v = q.front();
q.pop();
ndist[v] = ans[v] = d[v];
if(v != T[0] && s[v] == '1'){
ones.pb(v);
continue;
}
for(int x:adj[v]){
if(d[x] > d[v]+1){
d[x] = d[v]+1;
q.push(x);
}
}
}
}
for(int i=0;i<n;i++){
if(s[i] == '0')continue;
int cnt = 0;
for(int x:adj[i])cnt += s[x]=='1';
if(cnt){
rdist[i] = 0;
q.pb(i);
}
}
while(!q.empty()){
int v = q.front();
q.pop_front();
for(int x:adj[v]){
if(rdist[x] == 1e9){
if(s[x] == '1'){
rdist[x] = rdist[v];
q.push_front(x);
}else{
rdist[x] = rdist[v]+1;
q.push_back(x);
}
}
}
} //min dist to a red node
for(int i=0;i<n;i++){if(s[i] == '1')rdist[i]++;}
for(int i=0;i<n;i++){
if(s[i] == '1'){
for(int x:adj[i]){
if(bdist[x] > 1){
bdist[x] = 1;
q.pb(x);
}
}
}
}
while(!q.empty()){
int v = q.front();
q.pop_front();
for(int x:adj[v]){
if(bdist[x] == 1e9){
bdist[x] = bdist[v]+1;
q.push_back(x);
}
}
} //min dist to a black node
ll sum = 0;
vector<ll>f;
for(int i=1;i<k;i++){
sum += bdist[T[i]];
f.pb(rdist[T[i]] - bdist[T[i]]);
}
//for(int i=0;i<n;i++)cout<<bdist[i]<<" "<<rdist[i]<<'\n';
sort(all(f));
for(int i=0;i<k;i++){
ll c = 2*(k-1) - i;
//cout<<"iteration: "<<i<<'\n';
//cout<<sum<<" "<<c<<'\n';
vector<ll>dist(n,1e18);
priority_queue<PII,vector<PII>,greater<PII>>pq;
for(int v:ones){
//need to have at least more than one game started
for(int x:adj[v]){
ll cost = ndist[v] + sum + 1;
if(s[x] == '1')cost += c;
if(dist[x] > cost){
dist[x] = cost;
pq.push({dist[x],x});
}
}
}
while(!pq.empty()){
int v = pq.top().se;
ll d = pq.top().fi;
pq.pop();
if(d != dist[v])continue;
//cout<<v<<" "<<d<<'\n';
for(int x:adj[v]){
if(s[x] == '0'&& dist[x] > d+1){
dist[x] = d+1;
pq.push({dist[x],x});
}
if(s[x] == '1' && dist[x] > d+c+1){
dist[x] = d+c+1;
pq.push({dist[x],x});
}
}
}
for(int j=0;j<n;j++){
//cout<<dist[j]<<" ";
if(s[j] == '1')ans[j] = min(ans[j],dist[j]-c);
else ans[j] = min(ans[j],dist[j]);
}
//cout<<'\n';
if(i!=k-1)sum+=f[i];
}
for(int i=0;i<n;i++)cout<<ans[i]<<'\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... |