(树形DP) Strategic game(P1463)

本文介绍了一种使用树形动态规划解决士兵布防问题的方法。问题要求在保证所有路径受监视的前提下,找到部署最少数量士兵的方案。文章通过具体实例详细解析了算法实现过程,并附带完整的代码。

今天再做了一道树形DP的题,,在不知道DP之前这个题当时抓破了脑袋也没有出来。


题意:有N个具有特定的连通关系的点,如果安排一个兵在一个点上,则在该点还有其邻点上都被监视着

      为了使整个网络内都能够被监视,则最少需要多少个士兵。


思路:就是找出满足相邻两边不能同时有兵士时的最大数量,然后用N减去这样的最大数就可以了


误区:我开始就这样想的,可是做对了才想起这个理论没有考虑成熟就做了,幸好 还对了。


难点:其输入些许麻烦,对输入的处理要小心,不然一不小心就错了。


#include<iostream>
#include<vector>
#include<map>
#include<string>
using namespace std;

#define N 1501
#define inf -1000000000

vector<int> son[N];
int n;
int d[N][2];
bool vist[N];

void dfs(int k)
{
	//cout<<k<<endl;
	
	for (vector<int>::size_type it=0;it<son[k].size();it++) if (!vist[son[k][it]])
	{
		vist[son[k][it]]=true;
		dfs(son[k][it]);
		vist[son[k][it]]=false;
	}

	int i=0,j=0;
	for (vector<int>::size_type it=0;it<son[k].size();it++) if (!vist[son[k][it]])
	{
		i+=max(d[son[k][it]][0],d[son[k][it]][1]);
		j+=d[son[k][it]][0];
	}
	d[k][0]=i;
	d[k][1]=j+1;
}

int main()
{
//	freopen("C:\in.txt","r",stdin);
	int i,j,k;
	
	char  a[10],b[10];
	int cur;
	while (cin>>n)
	{
		memset(vist,false,sizeof(vist));
		for (i=0;i<n;i++)
			son[i].clear();
		memset(d,0,sizeof(d));
		vist[0]=true;
		for (i=0;i<n;i++)
		{
			cin>>a;
			int lena=strlen(a);
			k=0;
			j=0;
			int tt=0;
			while (a[j]!='(')
			{
				if (a[j]>='0'&&a[j]<='9')
				tt=tt*10+a[j]-'0';
				j++;
			}
			j++;
			while (a[j]!=')')
			{
				k=k*10+a[j]-'0';
				j++;
			}
		

			for (j=0;j<k;j++)
			{
				cin>>cur;
				son[tt].push_back(cur);
				son[cur].push_back(tt);
			}
		}
		dfs(0);

		//for (i=0;i<n;i++)
			//cout<<d[i][0]<<' '<<d[i][1]<<endl;
		cout<<n-max(d[0][0],d[0][1])<<endl;
		
	}

	return 0;
}


Strategic game
Time Limit: 2000MS Memory Limit: 10000K
Total Submissions: 4942 Accepted: 2260

Description

Bob enjoys playing computer games, especially strategic games, but sometimes he cannot find the solution fast enough and then he is very sad. Now he has the following problem. He must defend a medieval city, the roads of which form a tree. He has to put the minimum number of soldiers on the nodes so that they can observe all the edges. Can you help him?

Your program should find the minimum number of soldiers that Bob has to put for a given tree.

For example for the tree:

the solution is one soldier ( at the node 1).

Input

The input contains several data sets in text format. Each data set represents a tree with the following description:

  • the number of nodes
  • the description of each node in the following format
    node_identifier:(number_of_roads) node_identifier1 node_identifier2 ... node_identifiernumber_of_roads
    or
    node_identifier:(0)

The node identifiers are integer numbers between 0 and n-1, for n nodes (0 < n <= 1500);the number_of_roads in each line of input will no more than 10. Every edge appears only once in the input data.

Output

The output should be printed on the standard output. For each given input data set, print one integer number in a single line that gives the result (the minimum number of soldiers). An example is given in the following:

Sample Input

4
0:(1) 1
1:(2) 2 3
2:(0)
3:(0)
5
3:(3) 1 4 2
1:(1) 0
2:(0)
0:(0)
4:(0)

Sample Output

1
2

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值