[C++] 纯文本查看 复制代码 #include <iostream>
#include <cstring>
#include <string>
#include <queue>
using namespace std;
const int mmax = 110;
struct node {
int en;
int next;
}E[mmax * mmax];
int num;
int p[mmax];
void init() {
memset(p, -1, sizeof p);
num = 0;
}
void add(int st, int en) {
E[num].en = en;
E[num].next = p[st];
p[st] = num++;
}
int w[mmax];
int value[mmax];
bool vis[mmax];
int cnt[mmax];
bool spfa(int st, int en) {
queue<int> q;
memset(vis, 0, sizeof vis);
memset(value, 0, sizeof value);
memset(cnt, 0, sizeof cnt);
value[st] = 100 + w[st];
q.push(st);
++cnt[st];
while (!q.empty()) {
int u = q.front();
q.pop();
if (cnt[u] == en + 1) {
continue;
}
++cnt[u];
if (cnt[u] == en + 1) {
value[u] = 10000000;
}
vis[u] = 0;
if (u == en) {
return 1;
}
for (int i = p[u]; i + 1; i = E[i].next) {
int v = E[i].en;
if (value[v] < value[u] + w[v]) {
value[v] = value[u] + w[v];
if (!vis[v]) {
q.push(v);
vis[v] = 1;
}
}
}
}
return 0;
}
int main() {
freopen("game.in", "r", stdin);
freopen("game.out", "w", stdout);
int n;
cin >> n;
init();
for (int i = 1; i <= n; ++i) {
scanf("%d", &w[i]);
int k;
scanf("%d", &k);
while (k--) {
int d;
scanf("%d", &d);
add(i, d);
}
}
if(spfa(1, n)) {
puts("Yes");
} else {
puts("No");
}
return 0;
}
|