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 ...
随机推荐
- hdoj - 5202 Rikka with string (BestCoder Round #37 ($))
http://acm.hdu.edu.cn/showproblem.php?pid=5202 字符串处理的题,要细心. 给定一个只包含小写字母和问号的字符串,让我们还原出本来的字符串,把问号替换成任意 ...
- CakeDC(cakephp company)Git workflow--适合于较大团队大型项目开发
CakeDC Git workflow是一个项目开发和版本发布的工作流,在这个工作流程中开发和版本发布周期是基于几个关键阶段(key phases): Development: 所有活跃的开发活动都由 ...
- (转载) jQuery 页面加载初始化的方法有3种
jQuery 页面加载初始化的方法有3种 ,页面在加载的时候都会执行脚本,应该没什么区别,主要看习惯吧,本人觉得第二种方法最好,比较简洁. 第一种: $(document).ready(functio ...
- iOS NSNotificationCenter(消息机制)
转自:http://blog.csdn.net/liliangchw/article/details/8276803 对象之间进行通信最基本的方式就是消息传递,在Cocoa中提供Notificatio ...
- toB的产品经理和toc产品经理区别
腾讯产品经理现身说法 曾经在UC做过2年to c的app,现在在腾讯做to b的产品. 做to c产品的时候,我很瞧不起做to b产品的同学,认为他们不过是做支撑的. 后来,我参与了一个to b平台级 ...
- 【JavaScript学习笔记】点击消失
<!DOCTYPE html> <html> <head> <script type="text/javascript" src=&quo ...
- (转)每天一个Linux命令(6):mv
mv命令是move的缩写,可以用来移动文件或者将文件改名(move (rename) files),是Linux系统下常用的命令,经常用来备份文件或者目录. 1.命令格式: mv [选项] 源文件或目 ...
- php的webservice的soapheader认证问题
参数通过类传输:class authentication_header { private $username; private $password; public ...
- 删除github.com上repository(仓库)的方法
第一步:打开http://github.com,看到右侧仓库列表.第二步:假设要删除“HiTop”这个参考,点击对应仓库进入详细页面之后,在右侧会看到“Settings”入口. 第三步:进入设置页面之 ...
- shell script入门
从程序员的角度来看, Shell本身是一种用C语言编写的程序,从用户的角度来看,Shell是用户与Linux操作系统沟通的桥梁.用户既可以输入命令执行,又可以利用 Shell脚本编程,完成更加复杂的操 ...