POJ 1463 Strategic game( 樹形DP )
來源:程序員人生 發布時間:2014-09-29 13:26:30 閱讀次數:3200次
題意:一顆 N 個節點的樹,在某一個節點上放置一個兵則可以守住與它相鄰的邊。最少放置多少個兵才可以守住所有的邊。
#include <cstdio>
#include <deque>
using namespace std;
#define ABANDON 0
#define GET 1
deque< int > graph[2010];
int DP[2010][2];
void DFS( int start, int parent ){
DP[start][ABANDON] = 0;
DP[start][GET] = 1;
int target;
if( graph[start].size() == 1 && parent != -1 )
return;
for( int i = 0; i < graph[start].size(); ++i ){
target = graph[start][i];
if( target == parent )
continue;
DFS( target, start );
DP[start][ABANDON] += DP[target][GET];
DP[start][GET] += min( DP[target][GET], DP[target][ABANDON] );
}
}
int main(){
int nodes;
int start, roads, target;
while( scanf( "%d", &nodes ) != EOF ){
for( int i = 0; i <= nodes; ++i )
graph[i].clear();
for( int i = 0; i < nodes; ++i ){
scanf( "%d:(%d)", &start, &roads );
while( roads-- ){
scanf( "%d", &target );
graph[start].push_back( target );
graph[target].push_back( start );
}
}
DFS( 0, -1 );
printf( "%d
", min( DP[0][ABANDON], DP[0][GET] ) );
}
return 0;
}</span>
生活不易,碼農辛苦
如果您覺得本網站對您的學習有所幫助,可以手機掃描二維碼進行捐贈