rt。
写了IDA*结果WA了(如果数据没有问题那就求调qwq)。
#include <bits/stdc++.h>
using namespace std;
const int N = 1e6 + 5;
struct edge {
int w, next, to;
} e[N], e1[N];
struct node {
int pos, w;
node(int _, int __) :
pos(_), w(__) {}
};
int n, m, k, s, t, v, cnt, cnt1, maxdep, dis[N], head[N], head1[N], eh[N];
bool vis[N];
void add(int u, int v, int w) {
e[++ cnt].to = v;
e[cnt].w = w;
e[cnt].next = head[u];
head[u] = cnt;
}
void add1(int u, int v, int w) {
e1[++ cnt1].to = v;
e1[cnt1].w = w;
e1[cnt1].next = head1[u];
head1[u] = cnt1;
}
void bfs() {
queue<node> q;
q.push(node(t, 0));
memset(dis, 0x3f, sizeof dis);
vis[t] = true;
while (!q.empty()) {
node u = q.front(); q.pop();
dis[u.pos] = u.w;
for (int i = head1[u.pos]; i; i = e1[i].next) {
int v = e1[i].to;
if (!vis[v])
vis[v]= true, q.push(node(v, u.w + 1));
}
}
}
void IDA_star(int pos, int depth) {
if (depth + dis[pos] > maxdep) return;
if (eh[pos]) {
for (int temp = 0; temp <= eh[pos]; temp ++) {
eh[pos] -= temp;
for (int i = head[pos]; i; i = e[i].next) {
int v = e[i].to;
eh[v] += temp, IDA_star(pos, depth + 1), eh[v] -= temp;
}
eh[pos] += temp;
}
return;
}
if (pos == t) {
cout << maxdep << '\n';
exit(0);
}
for (int i = head[pos]; i; i = e[i].next)
IDA_star(e[i].to, depth + 1);
}
int main() {
cin >> n >> s >> t;
for (int i = 0; i < n; i ++) {
cin >> eh[i] >> k, eh[i] ^= 1;
for (int j = 0; j < k; j ++)
cin >> v, add(i, v, 1), add1(v, i, 1);
}
bfs();
if (dis[s] != 0x3f3f3f3f) {
for (maxdep = 0; maxdep <= 25; maxdep ++)
IDA_star(s, 0);
}
return 0;
}
/*
6 0 3
1 1 1
0 1 2
0 1 3
1 1 4
1 1 5
1 0
0 0 2 0 0 0
*/