树形动规_(战略游戏)
戰略游戲(SGOI)
stragedi.pas/c/cpp
【問題描述】
?? Bob 喜歡玩電腦游戲,特別是戰略游戲。但是他經常無法找到快速玩過游戲的辦法。現在他有個問題。他要
建立一個古城堡,城堡中的路形成一棵樹。他要在這棵樹的結點上放置最少數目的士兵,使得這些士兵能了望到
所有的路。注意,某個士兵在一個結點上時,與該結點相連的所有邊將都可以被了望到。
? 請你編一程序,給定一樹,幫 Bob 計算出他需要放置最少的士兵。
【輸入格式】
? 輸入文件中數據表示一棵樹,描述如下:
? 第一行 N,表示樹中結點的數目。
? 第二行至第 N+1 行,每行描述每個結點信息,依次為:該結點標號 i,k(后面有 k 條邊與結點 I 相連),
接下來 k 個數,分別是每條邊的另一個結點標號 r1,r2,...,rk。
? 對于一個 n(0 < n <= 1500)個結點的樹,結點標號在 0 到 n-1之間,在輸入文件中每條邊只出現一次。?
【輸出格式】
? 輸出文件僅包含一個數,為所求的最少的士兵數目。?
【輸入樣例1】
4
0 1 1
1 2 2 3
2 0
3 0
【輸出樣例1】
1
【輸入樣例2】
5
3 3 1 4 2
1 1 0
2 0
0 0
4 0
【輸出樣例2】
2
【時間限制】
1s
【空間限制】
64M
//-------------------------------------------------------------------------------------
分析:比較簡單的樹規,f[i,0..1]表示第i個節點放或不放士兵,以i為根的子樹所需的最少人數.
設s為i的一個兒子,則f[i,0]必須從f[s,1]轉移而來,f[i,1]可以從f[s,0]或f[s,1]轉移來.
code:
type edge=recordv,n:longint; end; const maxn=1501; var e:array[0..maxn] of edge;r,h:array[0..maxn] of longint;f:array[0..maxn,0..1] of longint;n,i,j,q,u,v,root,cnt:longint;procedure add(u,v:longint);begininc(cnt);e[cnt].v:=v;e[cnt].n:=h[u];h[u]:=cnt;end;function min(a,b:longint):longint;beginif a>b then exit(b); exit(a);end;procedure DP(u:longint);var v,p:longint;beginf[u,0]:=0;f[u,1]:=1;p:=h[u];while p<>0 dobeginv:=e[p].v;DP(v);inc(f[u,0],f[v,1]);inc(f[u,1],min(f[v,0],f[v,1]));p:=e[p].n;end;end;beginassign(input,'stragedi.in'); reset(input);assign(output,'stragedi.out'); rewrite(output);readln(n);for i:=1 to n dobeginread(u);read(q);for j:=1 to q dobeginread(v);add(u,v);inc(r[v]);end;readln;end;for i:=0 to n-1 doif r[i]=0 thenbeginroot:=i;break;end;DP(root);writeln(min(f[root,0],f[root,1]));close(input);close(output); end.轉載于:https://www.cnblogs.com/exponent/archive/2011/08/06/2129521.html
總結
以上是生活随笔為你收集整理的树形动规_(战略游戏)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 怎么打败腾讯[纯讨论]
- 下一篇: hasOwnProperty和isPro