这么小的数据范围写什么强行边转点跑最小环,暴力dfs就好了。
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#define ll long long
#define rep(i,x,y) for(ll i=(x);i<=(y);i++)
#define repd(i,x,y) for(ll i=(x);i>=(y);i--)
using namespace std;
const ll N=1e4+5;
const ll Inf=1e18;
ll n,m,ans=Inf,v[N],vis[N],sum[N];
ll cnt,to[N],nxt[N],head1[N],head2[N];
inline ll read() {
ll x=0;char ch=getchar();bool f=0;
while(ch>'9'||ch<'0'){if(ch=='-')f=1;ch=getchar();}
while(ch>='0'&&ch<='9'){x=(x<<1)+(x<<3)+ch-'0';ch=getchar();}
return f?-x:x;
}
void ins1(ll x,ll y) {
to[++cnt]=y;nxt[cnt]=head1[x];head1[x]=cnt;
}
void ins2(ll x,ll y) {
to[++cnt]=y;nxt[cnt]=head2[x];head2[x]=cnt;
}
bool check1(ll x,ll y) {
ll p=head1[x];
while(p) {
if(to[p]==y) return false;
p=nxt[p];
}
return true;
}
bool check2(ll x,ll y) {
ll p=head2[x];
while(p) {
if(to[p]==y) return false;
p=nxt[p];
}
return true;
}
void dfs(ll x,ll y,ll z) {
if(vis[x]) {
ans=min(ans,z-sum[x]);return ;
}vis[x]=1;sum[x]=z;
if(check1(x,y)) {
ll p=head1[x];
while(p) {
if(to[p]!=y) dfs(to[p],x,z+v[x]);
p=nxt[p];
}
}
if(check2(x,y)) {
ll p=head2[x];
while(p) {
if(to[p]!=y) dfs(to[p],x,z+v[x]);
p=nxt[p];
}
}
vis[x]=0;
}
int main() {
n=read();
rep(i,1,n) {
ll id=read(),z=read(),m1=read(),m2=read();
v[id]=z;
rep(i,1,m1) {
ll p=read();ins1(id,p);
}
rep(i,1,m2) {
ll p=read();ins2(id,p);
}
}
dfs(1,-1,0);
printf("%lld",ans);
return 0;
}