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 ...
随机推荐
- 关于在c#中引用外部dll文件,在页面中找不到命名空间
最近在项目中碰到这样的问题,经过搜索,发现是vs2010的版本不对,VS默认的版本是.NET Framework 4 Client Profile,需要将他更改为.NET Framework 4 版本 ...
- Unix/Linux 关机命令
关机命令 AIX shutdown now Solaris init 5 Redhat shutdown now Centos shutdown now
- LEFT JOIN、Right、Full后ON和WHERE的区别
今天在工作的时候碰到了一个问题,A表B表left join后在on后面关于A表的条件过滤语句没起到我想要的过滤作用,还是对左连接等理解的不够呀. SELECT * FROM student; SELE ...
- Hybris 预备知识学习列表
需要学习的: Spring,Spring对于hybris非常重要,整个hybris是基于spring的架构之上. 包括例如spring mvc,spring security, 设计模式 软件构建工具 ...
- UVa 10020 (最小区间覆盖) Minimal coverage
题意: 数轴上有n个闭区间[ai, bi],选择尽量少的区间覆盖一条指定线段[0, m] 算法: [start, end]为已经覆盖到的区间 这是一道贪心 把各个区间先按照左端点从小到大排序,更新st ...
- HDU 1231 最大连续子序列
和前面两道题一样 不过这题要求输出子序列首尾的元素的值,而且如果所有元素都小于0的话,规定子序列的和为0,并输出整个序列的首尾元素. //#define LOCAL #include <iost ...
- 51nod1394 差和问题
我只会用线段树写...不喜欢树状数组..其实跑的也不算慢?然后各种*的时候忘了longlong一直WA...药丸! 而且我不怎么会用map离散化...那么就sort+unique #include&l ...
- BZOJ 1878 HH的项链
不能分块(显然复杂度会炸啊.....) 离线+BIT.每个颜色在每个询问中只出现一次. #include<iostream> #include<cstdio> #include ...
- ORACLE解锁record is locked by another user
在操作ORACLE数据库的时候,由于执行完,没有COMMIT,直接把PL/SQL关闭掉,后来导致那张表被锁住,当编辑时就会出现这个信息,record is locked by another user ...
- centos 6.5 minimal 安装及vm-tools安装
安装vm-->注册vm-->新建一个虚拟机(选择等会安装系统)-->加载iso-->配置桥接-->启动 这里可能碰到一个cpu的虚拟化配置,需要在bios里的配置设置为e ...