pku3659 Cell Phone Network
http://poj.org/problem?id=3659
树状DP,树的最小点覆盖
#include <stdio.h>
#include <vector>
#define N 10010 using namespace std; vector<int> a[N];
int mark[N], dp[N][];
const int inf = ; int min(int x, int y)
{
return x<y? x: y;
} int limit_inf(int x)
{
return x>inf? inf: x;
} int f(int x, vector<int> b)
{
int i, j, k, sum1, min1; //leaf
if(b.size() == )
{
dp[x][] = ;
dp[x][] = inf;
dp[x][] = ;
return ;
} //
sum1 = ;
for(i=; i<b.size(); i++)
{
sum1 += dp[b[i]][];
}
dp[x][] = limit_inf(sum1); //
min1 = inf;
for(i=; i<b.size(); i++)
{
sum1 = dp[b[i]][];
for(j=; j<b.size(); j++)
{
if(j != i)
{
sum1 += min(dp[b[j]][], dp[b[j]][]);
}
}
min1 = min(min1, sum1);
}
dp[x][] = limit_inf(min1); //
sum1 = ;
for(i=; i<b.size(); i++)
{
sum1 += min(dp[b[i]][], min(dp[b[i]][], dp[b[i]][]));
}
sum1 += ;
dp[x][] = limit_inf(sum1);
return ;
} int dfs(int x)
{
int i, j;
vector<int> b;
b.clear();
//printf("(%d: ", x);
for(i=; i<a[x].size(); i++)
{
j = a[x][i];
if(mark[j] == )
{
b.push_back(j);
mark[j] = ;
dfs(j);
}
}
f(x, b);
//printf("[%d,%d,%d]", dp[x][0], dp[x][1], dp[x][2]);
//printf(" ) ");
return ;
} int main()
{
int n, i, x, y;
while(~scanf("%d", &n))
{
for(i=; i<=n; i++)
{
//dp[i][0] = dp[i][1] = dp[1][2] = inf;
mark[i] = ;
a[i].clear();
}
for(i=; i<=n-; i++)
{
scanf("%d%d", &x, &y);
a[x].push_back(y);
a[y].push_back(x);
}
mark[] = ;
dfs();
printf("%d\n", min(dp[][], dp[][]));
if(dp[][] == inf)
{
dp[][] = -;
}
//printf("\n");
//for(i=1; i<=n; i++)
//{
// printf("%5d %5d %5d\n", dp[i][0], dp[i][1], dp[i][2]);
//}
}
return ;
}
pku3659 Cell Phone Network的更多相关文章
- [USACO08JAN]手机网络Cell Phone Network
[USACO08JAN]手机网络Cell Phone Network 题目描述 Farmer John has decided to give each of his cows a cell phon ...
- POJ 3659 Cell Phone Network / HUST 1036 Cell Phone Network(最小支配集,树型动态规划,贪心)-动态规划做法
POJ 3659 Cell Phone Network / HUST 1036 Cell Phone Network(最小支配集,树型动态规划,贪心) Description Farmer John ...
- POJ 3659 Cell Phone Network(树的最小支配集)(贪心)
Cell Phone Network Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 6781 Accepted: 242 ...
- 洛谷P2899 [USACO08JAN]手机网络Cell Phone Network
P2899 [USACO08JAN]手机网络Cell Phone Network 题目描述 Farmer John has decided to give each of his cows a cel ...
- 树的最小支配集 E - Cell Phone Network POJ - 3659 E. Tree with Small Distances
E - Cell Phone Network POJ - 3659 题目大意: 给你一棵树,放置灯塔,每一个节点可以覆盖的范围是这个节点的所有子节点和他的父亲节点,问要使得所有的节点被覆盖的最少灯塔数 ...
- 树形dp compare E - Cell Phone Network POJ - 3659 B - Strategic game POJ - 1463
B - Strategic game POJ - 1463 题目大意:给你一棵树,让你放最少的东西来覆盖所有的边 这个题目之前写过,就是一个简单的树形dp的板题,因为这个每一个节点都需要挺好处 ...
- 洛谷 P2899 [USACO08JAN]手机网络Cell Phone Network(树形动规)
题目描述 Farmer John has decided to give each of his cows a cell phone in hopes to encourage their socia ...
- 洛谷 P2899 [USACO08JAN]手机网络Cell Phone Network
题目描述 Farmer John has decided to give each of his cows a cell phone in hopes to encourage their socia ...
- P2899 [USACO08JAN]手机网络Cell Phone Network
P2899 [USACO08JAN]手机网络Cell Phone Networ题目描述 Farmer John has decided to give each of his cows a cell ...
随机推荐
- 关于RotateAnimation的各构造方法的参数
本文以RotateAnimation的构造方法 讲解. RotateAnimation(float fromDegrees, float toDegrees) 其他构造器的旋转也可参考这副图. Rot ...
- 安装SQL2008的时候 出现System.Configuration.ConfigurationErrorsException: 创建 userSettings/Microsoft.SqlServe
System.Configuration.ConfigurationErrorsException: 创建 userSettings/Microsoft.SqlServer.Configuration ...
- CodeSmith listview属性
private void button1_Click(object sender, EventArgs e)//将数据库中读出来的信息直接显示在listview里 { //连接数据库 SqlConne ...
- 【笨嘴拙舌WINDOWS】计时器精度
WINDOWS的大多数系统并非实时操作系统,所以不能规定计算机在某个精确到纳秒的时间让计算机做某项任务,如果规定了时间WINDOWS也将需要在完成了线程调度后,经行任务执行! 也就是说,如果你的应用程 ...
- UVa 699 (二叉树) The Falling Leaves
题意: 按先序方式输入一棵二叉树,节点是带权的,左孩子在父节点的左一个单位,右孩子在父节点的右一个单位,从左到右输出相同水平位置节点之和. 分析: 做了好几道二叉树的题,代码应该也很好理解了.这里ma ...
- iOS 8 AutoLayOut入门
http://blog.csdn.net/asdfg13697116596/article/details/42562565 iOS 8 AutoLayOut入门自从iOS6带来Auto Layout ...
- ubuntu 11.04 源 更新不了,全显示ign、404
原文地址:http://blog.csdn.net/enjio/article/details/11603373 ubuntu 11.04 源 更新不了 分类: 开发相关2013-09-12 14 ...
- POJ 1861 Network (MST)
题意:求解最小生成树,以及最小瓶颈生成树上的瓶颈边. 思路:只是求最小生成树即可.瓶颈边就是生成树上权值最大的那条边. //#include <bits/stdc++.h> #includ ...
- Android 编程下两种方式注册广播的区别
常驻型广播 常驻型广播,当你的应用程序关闭了,如果有广播信息来,你写的广播接收器同样的能接收到,它的注册方式就是在你应用程序的AndroidManifast.xml 中进行注册,这种注册方式通常又被称 ...
- Java核心技术II读书笔记(二)
ch2 XML 有两种XML文档结构,DTD和Schema,用解释文档构成规则,这些规则指定了每个元素俺的合法子元素和属性. DTD DTD有多种提供方式,可以像下面这样加到XML中: <?xm ...