今天再做了一道树形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).
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 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.
- 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
本文介绍了一种使用树形动态规划解决士兵布防问题的方法。问题要求在保证所有路径受监视的前提下,找到部署最少数量士兵的方案。文章通过具体实例详细解析了算法实现过程,并附带完整的代码。
 Strategic gameP1463&spm=1001.2101.3001.5002&articleId=8196516&d=1&t=3&u=9de25d23cee045e5b3e1b4478c25f19d)
688

被折叠的 条评论
为什么被折叠?



