이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
using namespace std;
template<class T, class U>
void ckmin(T &a, U b)
{
if (a > b) a = b;
}
template<class T, class U>
void ckmax(T &a, U b)
{
if (a < b) a = b;
}
#define MP make_pair
#define PB push_back
#define LB lower_bound
#define UB upper_bound
#define fi first
#define se second
#define SZ(x) ((int) (x).size())
#define ALL(x) (x).begin(), (x).end()
#define FOR(i, a, b) for (auto i = (a); i < (b); i++)
#define FORD(i, a, b) for (auto i = (a) - 1; i >= (b); i--)
const int MAXN = 250013;
const int MAXC = 2813;
const int INF = 1e9 + 7;
const int DELTA = 125;
typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef vector<pii> vpi;
typedef vector<pll> vpl;
int N, M, K, B;
vi edge[MAXN];
int ban[MAXN], md[MAXN], id[MAXN], dist[MAXN], cc[MAXN];
bool hasfriend[MAXN];
set<pii> q;
int32_t main()
{
cout << fixed << setprecision(12);
cerr << fixed << setprecision(4);
ios_base::sync_with_stdio(false); cin.tie(0);
cin >> N >> M;
FOR(i, 0, M)
{
int u, v;
cin >> u >> v; u--; v--;
edge[u].PB(v);
edge[v].PB(u);
}
FOR(i, 0, N)
{
edge[i].PB(i);
cc[i] = -1;
}
cin >> K;
FOR(i, 0, K)
{
int c; cin >> c;
vi s(c);
FOR(j, 0, c)
{
cin >> s[j]; s[j]--;
id[s[j]] = B; B++;
ban[s[j]] = j;
cc[s[j]] = i;
}
md[i] = c;
}
FOR(i, 0, N)
{
for (int u : edge[i])
{
if (cc[u] == -1) hasfriend[i] = true;
}
}
fill(dist, dist + N, INF);
dist[0] = 0;
q.insert({0, 0});
while(!q.empty())
{
int d = q.begin() -> fi, u = q.begin() -> se; q.erase(q.begin());
if (u == N - 1) break;
// cerr << d << ' ' << u << endl;
// bool ok = false;
// for (int v : edge[u])
// {
// if (!seen[v][0]) ok = true;
// //make sure there's one univisited neighbor
// }
// if (!ok) continue;
for (int v : edge[u])
{
if (cc[v] != -1)
{
int mx = 7;
if (cc[u] != -1)
{
mx = 2;
if (hasfriend[u] && v == u) mx = 3;
}
FOR(j, 1, mx)
{
// cerr << u << ' ' << v << ' ' << (d + j) << endl;
int m = md[cc[v]];
if ((d + j) % m == ban[v]) continue;
if (cc[u] == cc[v] && (d + j) % m == ban[u] && (d + j - 1) % m == ban[v]) continue;
if (dist[v] > d + j)
{
dist[v] = d + j;
}
if (d + j - dist[v] >= DELTA) continue;
q.insert({d + j, v});
}
}
else
{
if (dist[v] > d + 1)
{
dist[v] = d + 1;
q.insert({d + 1, v});
}
}
}
}
if (dist[N - 1] >= INF)
{
cout << "impossible\n";
return 0;
}
cout << dist[N - 1] << '\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... |