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 ...
随机推荐
- python下载图片
import re import urllib.request def getHtml(url): page = urllib.request.urlopen(url) html = page. ...
- Android HTTPS(5)SSL测试工具
Nogotofail: A Network Traffic Security Testing Tool Nogotofail is a tool gives you an easy way to co ...
- BZOJ 2173 整数的lqp拆分
题目链接:http://61.187.179.132/JudgeOnline/problem.php?id=2173 题意:给出输出n.设一种拆分为n=x1+x2+x3,那么这种拆分的价值为F(x1) ...
- What is the difference between DAO and DAL?
What is the difference between DAO and DAL? The Data Access Layer (DAL) is the layer of a system tha ...
- Server-Side UI Automation Provider - WPF Sample
Server-Side UI Automation Provider - WPF Sample 2014-09-14 引用程序集 自动化对等类 WPF Sample 参考 引用程序集 返回 UIAut ...
- jpa+spring配置多数据源
property name="hibernate.connection.url" value="jdbc:mysql://localhost:3306/test?useU ...
- PopupWindwo和AlertDialog的区别
AlertDialog 是非阻塞式对话框:AlertDialog弹出时,后台还可以做事情:而PopupWindow是阻塞式对话框:PopupWindow弹出时, 程序会等 待,在PopupWind ...
- 函数lock_rec_enqueue_waiting
type_mode基础上 加上 LOCK_WAIT 表示等待状态 /****************************************************************** ...
- open_binary_frm
参数uchar* head 是已经分配好内存的64个字节的地址 http://mysql.taobao.org/monthly/2015/08/07/ /** *先从.frm文件读取64字节 *第28 ...
- android开发找不到模拟器(PANIC: Could not open:)解决办法
android开发找不到模拟器(PANIC: Could not open:)解决办法 2013/4/3 17:44:15 0人评论 213次浏览 分类:android开发 在系统环境变量设置名为 ...