Hướng dẫn giải của GUMBI
Chỉ dùng lời giải này khi không có ý tưởng, và đừng copy-paste code từ lời giải này. Hãy tôn trọng người ra đề và người viết lời giải.
Nộp một lời giải chính thức trước khi tự giải là một hành động có thể bị ban.
Nộp một lời giải chính thức trước khi tự giải là một hành động có thể bị ban.
Lưu ý: Các code mẫu dưới đây chỉ mang tính tham khảo và có thể không AC được bài tập này
Code mẫu của flashmt
const maxn=1048575; var n,k,num,s,f:longint; p:array[0..20] of longint; q,d:array[1..maxn] of longint; a:array[1..20] of longint; procedure rf; var i,x,j,y:longint; begin read(n,k); p[0]:=1; for i:=1 to n do p[i]:=p[i-1] shl 1; f:=p[k-1]; for i:=1 to n do begin read(x); for j:=1 to x do begin read(y); a[i]:=a[i] or p[y-1]; end; a[i]:=p[n]-1-a[i]; end; for i:=1 to n do begin read(x); if x=1 then s:=s or p[i-1]; end; end; procedure pr; var i,j,x:longint; begin num:=1; i:=1; d[s]:=1; q[1]:=s; repeat for j:=1 to n do begin x:=(q[i] and a[j]) or p[j-1]; if d[x]=0 then begin d[x]:=d[q[i]]+1; if x=f then exit; inc(num); q[num]:=x; end; end; inc(i); until (d[f]<>0) or (i>num); end; procedure wf; begin if d[f]=0 then writeln(-1) else writeln(d[f]-1); end; begin rf; pr; wf; end.
Code mẫu của happyboy99x
#include<cstdio> #include<queue> using namespace std; int n, k, a[30], start; bool check[1<<20]; void enter() { scanf("%d%d",&n,&k); k = 1 << (k-1); for(int i = 0; i < n; ++i) { int s; scanf("%d", &s); a[i] = -1; for(int j = 0; j < s; ++j) { int t; scanf("%d",&t); a[i] &= ~(1 << (t-1)); } } start = 0; for(int i = 0; i < n; ++i) { int t; scanf("%d",&t); if(t) start |= 1 << i; } } void solve() { queue<int> qu; qu.push(start); check[start] = 1; for(int step = 0; !qu.empty(); ++step) for(int Z = qu.size()-1; Z >= 0; --Z) { int state = qu.front(); qu.pop(); if(state == k) { printf("%d\n", step); return; } for(int i = 0; i < n; ++i) { int newState = (state & a[i]) | (1 << i); if(!check[newState]) { check[newState] = 1; qu.push(newState); } } } printf("-1\n"); } int main() { #ifndef ONLINE_JUDGE freopen("input.txt", "r", stdin); #endif enter(); solve(); }
Code mẫu của ladpro98
program lem2; uses math; const maxn=20; fi=''; var press,t:array[1..maxn] of longint; d,q:array[0..1 shl maxn] of longint; check:array[0..1 shl maxn] of boolean; start,n,target,k:longint; procedure input; var inp:text; i,s,j,p:longint; begin assign(inp,fi);reset(inp); readln(inp,n,k); target:=1 shl (k-1); for i:=1 to n do begin read(inp,s); press[i]:=1 shl n-1; for j:=1 to s do begin read(inp,p); dec(press[i],1 shl (p-1)); end; readln(inp); end; for i:=1 to n do read(inp,t[i]); for i:=n downto 1 do start:=2*start+t[i]; close(inp); end; function getbit(i,j:longint):longint; begin exit(i shr (j-1) and 1); end; procedure bfs; var l,r,u,v,i:longint; begin l:=1;r:=1; q[1]:=start; check[start]:=true; d[start]:=0; while l<=r do begin u:=q[l];inc(l); for i:=1 to n do begin v:=u and press[i]; if getbit(v,i)=0 then v:=v+1 shl (i-1); if not check[v] then begin check[v]:=true; inc(r); q[r]:=v; d[v]:=d[u]+1; if v=target then begin write(d[v]); halt; end; end; end; end; end; begin input; bfs; write(-1); end.
Code mẫu của RR
{$R-,Q-} const FINP=''; FOUT=''; MAXN=20; oo=1000000; var first,last,k,n:longint; a:array[1..MAXN] of longint; queue,d:array[0..1048577] of longint; procedure inp; inline; var f:text; i,j,jj,x:longint; begin assign(f,FINP); reset(f); read(f,n,k); for i:=1 to n do begin read(f,j); for jj:=1 to j do begin read(f,x); if (a[i] shr (x-1)) and 1=0 then a[i]:=a[i]+1 shl (x-1); end; a[i]:=not a[i]; end; j:=0; for i:=1 to n do begin read(f,x); if x=1 then j:=j+1 shl (i-1); end; for i:=1 shl n downto 0 do d[i]:=oo; first:=1; last:=1; queue[1]:=j; d[j]:=0; close(f); end; procedure solve; inline; var u,i,uu:longint; begin while first<=last do begin u:=queue[first]; inc(first); for i:=1 to n do begin uu:=(u or (1 shl (i-1))) and a[i]; if d[uu]=oo then begin d[uu]:=d[u]+1; inc(last); queue[last]:=uu; if uu=1 shl (k-1) then exit; end; end; end; end; procedure ans; inline; var f:text; begin assign(f,FOUT); rewrite(f); if d[1 shl (k-1)]<oo then writeln(f,d[1 shl (k-1)]) else writeln(f,-1); close(f); end; begin inp; solve; ans; end.
Code mẫu của hieult
#include <set> #include <map> #include <list> #include <cmath> #include <queue> #include <stack> #include <cstdio> #include <string> #include <vector> #include <cstdlib> #include <cstring> #include <sstream> #include <iomanip> #include <iostream> #include <algorithm> #include <ctime> #include <deque> #include <bitset> #include <cctype> #include <utility> using namespace std; typedef long long ll; typedef long double ld; typedef unsigned int ui; typedef unsigned long long ull; #define Rep(i,n) for(int i = 0; i < (n); ++i) #define Repd(i,n) for(int i = (n)-1; i >= 0; --i) #define For(i,a,b) for(int i = (a); i <= (b); ++i) #define Ford(i,a,b) for(int i = (a); i >= (b); --i) #define Fit(i,v) for(__typeof((v).begin()) i = (v).begin(); i != (v).end(); ++i) #define Fitd(i,v) for(__typeof((v).rbegin()) i = (v).rbegin(); i != (v).rend(); ++i) #define mp make_pair #define pb push_back #define fi first #define se second #define sz(a) ((int)(a).size()) #define all(a) (a).begin(), (a).end() #define ms(a,x) memset(a, x, sizeof(a)) template<class F, class T> T convert(F a, int p = -1) { stringstream ss; if (p >= 0) ss << fixed << setprecision(p); ss << a; T r; ss >> r; return r; } template<class T> T gcd(T a, T b) { T r; while (b != 0) { r = a % b; a = b; b = r; } return a; } template<class T> T lcm(T a, T b) { return a / gcd(a, b) * b; } template<class T> T sqr(T x) { return x * x; } template<class T> T cube(T x) { return x * x * x; } template<class T> int getbit(T s, int i) { return (s >> i) & 1; } template<class T> T onbit(T s, int i) { return s | (T(1) << i); } template<class T> T offbit(T s, int i) { return s & (~(T(1) << i)); } template<class T> int cntbit(T s) { return s == 0 ? 0 : cntbit(s >> 1) + (s & 1); } const int bfsz = 1 << 16; char bf[bfsz + 5]; int rsz = 0; int ptr = 0; char gc() { if (rsz <= 0) { ptr = 0; rsz = (int) fread(bf, 1, bfsz, stdin); if (rsz <= 0) return EOF; } --rsz; return bf[ptr++]; } void ga(char &c) { c = EOF; while (!isalpha(c)) c = gc(); } int gs(char s[]) { int l = 0; char c = gc(); while (isspace(c)) c = gc(); while (c != EOF && !isspace(c)) { s[l++] = c; c = gc(); } s[l] = '\0'; return l; } template<class T> bool gi(T &v) { v = 0; char c = gc(); while (c != EOF && c != '-' && !isdigit(c)) c = gc(); if (c == EOF) return false; bool neg = c == '-'; if (neg) c = gc(); while (isdigit(c)) { v = v * 10 + c - '0'; c = gc(); } if (neg) v = -v; return true; } typedef pair<int, int> II; const ld PI = acos(-1.0); const ld eps = 1e-12; const int dr[] = { -1, 0, +1, 0}; const int dc[] = { 0, +1, 0, -1}; const int inf = (int) 1e9 + 5; const ll linf = (ll) 1e18 + 5; const int mod = (ll) (1e9 + eps); using namespace std; #define maxn 21 int n, k, d[21], f[1 << 21], que[1 << 21]; int main(){ // freopen("in.txt", "r", stdin); cin >> n >> k; k--; int num, x, u, A = 0; ms(d, 0); ms(f, 0x3f); Rep(i, n){ cin >> num; Rep(run, num){ cin >> x; x--; d[i] += (1 << x); } } Rep(i, n) { cin >> x; if(x) A += (1 << i); } f[A] = 0; int size = 0; que[size++] = A; Rep(i, size){ u = que[i]; if(u == (1 << k)) break; Rep(j, n) { A = onbit((u & d[j]) ^ u, j); if(f[A] > f[u] + 1){ f[A] = f[u] + 1; que[size++] = A; } } } if(f[1 << k] < inf) cout << f[1 << k]; else cout << -1; return 0; }
Code mẫu của ll931110
{$MODE DELPHI} Program LEM2; Const input = ''; output = ''; maxn = 20; maxd = 2200000; Var n,k: integer; start,finish: integer; d,queue: array[0..maxd] of integer; s: array[1..maxn] of integer; Procedure init; Var f: text; a,i,j,t: integer; Begin Assign(f, input); Reset(f); Readln(f, n, k); For i:= 1 to n do Begin Read(f, a); s[i]:= 0; For j:= 1 to a do Begin Read(f, t); s[i]:= s[i] + 1 shl (t - 1); End; End; finish:= 1 shl (k - 1); start:= 0; For i:= 1 to n do Begin Read(f, t); If t = 1 then start:= start + 1 shl (i - 1); End; Close(f); End; Procedure BFS; Var front,rear: integer; u,v,i: integer; Begin If start = finish then Begin d[start]:= 0; exit; End; Fillchar(d, sizeof(d), 0); d[start]:= 0; front:= 1; rear:= 1; queue[1]:= start; Repeat u:= queue[front]; inc(front); For i:= 1 to n do Begin v:= (u or (1 shl (i - 1))) and (not s[i]); If (d[v] = 0) and (v <> start) then Begin d[v]:= d[u] + 1; If v = finish then exit; inc(rear); queue[rear]:= v; End; End; Until front > rear; End; Procedure printresult; Var f: text; Begin Assign(f, output); Rewrite(f); If (d[finish] = 0) and (finish <> start) then writeln(f, -1) else writeln(f, d[finish]); Close(f); End; Begin init; BFS; printresult; End.
Code mẫu của skyvn97
#include<cstdio> #include<cstring> #include<queue> #define FOR(i,a,b) for (int i=(a),_b=(b);i<=_b;i=i+1) #define REP(i,n) for (int i=0,_n=(n);i<_n;i=i+1) #define MASK(i) (1<<(i)) using namespace std; int dis[MASK(20)+7]; int stt[22]; int n,goal,sta; int nextInt(void) { int x; scanf("%d",&x); return (x); } void init(void) { n=nextInt(); goal=MASK(nextInt()-1); REP(i,n) REP(zz,nextInt()) stt[i]|=MASK(nextInt()-1); REP(i,n) if (nextInt()==1) sta|=MASK(i); } void bfs(void) { memset(dis,-1,sizeof dis); queue<int> q; dis[sta]=0; q.push(sta); while (!q.empty()) { int u=q.front();q.pop(); if (u==goal) { printf("%d\n",dis[u]); return; } REP(i,n) { int v=(u|MASK(i))&~stt[i]; if (dis[v]<0) { dis[v]=dis[u]+1; q.push(v); } } } printf("-1\n"); } int main(void) { init(); bfs(); return 0; }
Bình luận