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 ...
随机推荐
- poj - 1258 Agri-Net (最小生成树)
http://poj.org/problem?id=1258 FJ为了竞选市长,承诺为这个地区的所有农场联网,为了减少花费,希望所需光纤越少越好,给定每两个农场的花费,求出最小花费. 最小生成树. # ...
- JavaScript高级程序设计之数据类型
首先讲讲关于js文件放置的问题,如果把<script>放在head标签处,浏览器会先加载完该处的所有不使用defer属性的js文件再呈现页面的内容(浏览器在遇到body标签时才呈现内容), ...
- shader复杂与深入:Normal Map(法线贴图)1
转自:http://www.zwqxin.com/archives/shaderglsl/review-normal-map-bump-map.htmlNormal Map法线贴图,想必每个学习计算机 ...
- tc srm 632 500 (规律)
We have a sequence of N positive integers: a[0] through a[N-1]. You do not know these integers. All ...
- ubuntu13.04云主机部署gitlab6.6
GitLab 是何物? GitLab是 GitHub 的山寨版,GitLab几乎包含了GitHub的所有功能,还包含比较有特色的功能:Code Review,Wiki,Merge Requests,最 ...
- 51nod1627 瞬间移动
打表可以看出来是组合数...妈呀为什么弄成n+m-4,n-1,m-3就错啊... //打表可以看出来是组合数...妈呀为什么弄成n+m-4,n-1,m-3就错啊... #include<cstd ...
- 深入理解OpenERP的工作流(Workflow)
一.工作流定义: <?xml version="1.0"?> <terp><data> <record model="w ...
- MVC+Ef项目(2) 如何更改项目的生成顺序;数据库访问层Repository仓储层的实现
我们现在先来看看数据库的生成顺序 居然是 Idal层排在第一,而 web层在第二,model层反而在第三 了 我们需要把 coomon 公用层放在第一,Model层放在第二,接下来是 Idal ...
- 解决Eclipse快捷键被其他软件占用
做为一个java攻城狮,eclipse是我最常用的攻城设备,eclipse快捷键 极大的提高了我的开发效率!!!! 前段时间升级了一下我的战斗装备——给电脑的系统盘换成了一个固态硬盘,因此需要重装系统 ...
- 【英语】Bingo口语笔记(54) - how to date a foreigner