Pentru această operație este nevoie să te autentifici.
Cod sursă (job #466215)
Utilizator |
|
IP | ascuns |
---|---|---|---|
Problemă | Evacuare (lot liceu) | Compilator | cpp | 1,00 kb |
Rundă | lasm_03_04_2019_10_12 | Status | evaluat |
Dată | 4 apr. 2019 15:20:16 | Scor | 100 |
#include <bits/stdc++.h>
using namespace std;
int dirr[500001], dij[500001];
vector<int> v[500001];
bool viz[500001];
queue<int> q;
int n, m, x;
int main()
{
ifstream cin("evacuare.in");
ofstream cout("evacuare.out");
cin >> n >> m >> x;
for (int i = 1; i <= m; i++)
{
int x, y;
cin >> x >> y;
v[x].push_back(y);
v[y].push_back(x);
}
for (int i = 1; i <= n; i++)
{
cin >> dirr[i];
dij[i] = 50000000;
}
int start = x, next;
int temp_dij;
dij[start] = 0;
q.push(start);
viz[start] = 1;
while (!q.empty())
{
start = q.front();
q.pop();
viz[start] = 0;
for (int i = 0; i < v[start].size(); i++)
{
next = v[start][i];
if (dirr[start] == next)
temp_dij = dij[start];
else
temp_dij = dij[start] + 1;
if (temp_dij < dij[next])
{
dij[next] = temp_dij;
if (viz[next] == 0)
{
q.push(next);
viz[next] = 1;
}
}
}
}
for (int i = 1; i <= n; i++)
cout << dij[i] << '\n';
return 0;
}