算是我的第一个树形DP 的题:

题目意思:N个城市形成树状结构。现在建立一些消防站在某些城市;每个城市有两个树形cost(在这个城市建立消防站的花费),limit ;

  我们要是每个城镇都是安全的:就是每个距离这个城镇最近的消防站不能超过这个城镇的limit值;

解法:这个题目乍一看卧槽怎么玩!玩不了啊!先给出dp[i][j]( I 依靠J,并且 I 这课子树都已经被覆盖的最优解(不一定都被J覆盖));

  假设一个节点的亲儿子树都被解决完毕,我们对于这些 亲儿子树 和这个 节点所组成的树的解如何得出?

  初始化dp[i][j]=cost[j];

  无疑使枚举这个节点I 依靠的节点,然后得出最小值;

  而这些 亲子树的合并无疑有俩情况

  1:这些亲儿子树依靠这个节点J  值 dp[k][j]-cost[j];

  2:这些亲儿子树不依靠这个节点I 值

      {

        int best=0x7fffffff;

        for(int w=1;w<=n;w++)

          best=min(dp[k][w],best);

        >>>best;

      }

  所以我们要开一个数字组维护每个节点的最值;

我输得不清楚那么百度下“国家集训队2006论文集 陈启峰”

#include <cstdio>
#include <algorithm>
#include <cmath>
#include <cstdlib>
#include <cstring>
using namespace std;
const int maxn=;
const int INF=0x7fffffff;
struct Edge
{
int to,dis,pre;
Edge(int to=,int dis=,int pre=):to(to),dis(dis),pre(pre){}
};
Edge edge[maxn*];
int head[maxn],pos;
int dp[maxn][maxn];
int dis[maxn][maxn];
int limit[maxn],cost [maxn];
int best[maxn];
int n,start; void inint()
{
for(int i=;i<maxn;i++)
{
best[i]=INF;
for(int j=;j<maxn;j++)
dp[i][j]=INF;
}
memset(head,-,sizeof(head));
pos=;
}
void add_edge(int s,int to,int dis)
{
edge[pos]=Edge(to,dis,head[s]);
head[s]=pos++;
}
void DIS(int pa,int s)
{
for(int i=head[s];~i;i=edge[i].pre)
{
Edge &tmp=edge[i];
if(tmp.to==pa)continue;
dis[start][tmp.to]=dis[start][s]+tmp.dis;
DIS(s,tmp.to);
}
}
void dfs(int pa,int s)
{
for(int i=head[s];~i;i=edge[i].pre)
{
Edge &tmp=edge[i];
if(tmp.to==pa)continue;
dfs(s,tmp.to);
}
for(int i=;i<=n;i++)
if(dis[s][i]<=limit[s])
{
dp[s][i]=cost[i];
for(int j=head[s];~j;j=edge[j].pre)
{
Edge &tmp=edge[j];
if(tmp.to==pa)continue;
dp[s][i]+=min(dp[tmp.to][i]-cost[i],best[tmp.to]);//加等
}
best[s]=min(best[s],dp[s][i]);
}
}
int main()
{
int t;
int a,b,c;
scanf("%d",&t);
while(t--)
{
inint();
scanf("%d",&n);
for(int i=;i<=n;i++)scanf("%d",&cost[i]);
for(int i=;i<=n;i++)scanf("%d",&limit[i]); for(int i=;i<=n;i++)
{
scanf("%d%d%d",&a,&b,&c);
add_edge(a,b,c);
add_edge(b,a,c);
}
for(int i=;i<=n;i++)start=i,DIS(-,i);
dfs(-,);
printf("%d\n",best[]);
}
return ;
}

POJ 2152 Fire的更多相关文章

  1. POJ 2152 fire / SCU 2977 fire(树型动态规划)

    POJ 2152 fire / SCU 2977 fire(树型动态规划) Description Country Z has N cities, which are numbered from 1 ...

  2. POJ 2152 Fire(树形dp)

    http://poj.org/problem?id=2152 题意: n个节点组成的树,要在树一些点上建立消防站,每个点建站都有个cost[i],每个点如果不在当前的点上建站,也要依赖其他的消防站,并 ...

  3. POJ 2152 Fire(树形DP)

    题意: 思路:令F[i][j]表示 的最小费用.Best[i]表示以i为根节点的子树多有节点都找到负责消防站的最小费用. 好难的题... #include<algorithm> #incl ...

  4. POJ 2152 Fire (树形DP,经典)

    题意:给定一棵n个节点的树,要在某些点上建设消防站,使得所有点都能够通过某个消防站解决消防问题,但是每个点的建站费用不同,能够保证该点安全的消防站的距离上限也不同.给定每个点的建站费用以及最远的消防站 ...

  5. 【POJ 2152】 Fire (树形DP)

    Fire   Description Country Z has N cities, which are numbered from 1 to N. Cities are connected by h ...

  6. Fire (poj 2152 树形dp)

    Fire (poj 2152 树形dp) 给定一棵n个结点的树(1<n<=1000).现在要选择某些点,使得整棵树都被覆盖到.当选择第i个点的时候,可以覆盖和它距离在d[i]之内的结点,同 ...

  7. 【POJ 2152】 Fire

    [题目链接] 点击打开链接 [算法] 同样是树形DP,但是比较难,笔者做这题看了题解 令f[i][j]表示在以i为根的子树中 1.在以i为根的子树中建一些消防站 2.在节点j必须建一个消防站 3.以i ...

  8. POJ 2607 Fire Station(Floyd打表+枚举更新最优)

    题目链接: http://poj.org/problem?id=2607 Description A city is served by a number of fire stations. Some ...

  9. POJ 2607 Fire Station

    Fire Station Time Limit: 5000ms Memory Limit: 65536KB This problem will be judged on PKU. Original I ...

随机推荐

  1. STM32学习之路-SysTick的应用(时间延迟)

    开发板:奋斗V5 好~ 菜B要来搞实验了.. 前面已经说了SysTick的工作原理什么的了,这里就不说了.. 先来做第一个实验: 盗自奋斗的样例,嘿嘿, 用SysTick产生1ms的基准时间,产生中断 ...

  2. git digest

    .gitignore文件示例: .classpath .project .idea/ .settings/ target/ *~ *.iml *.log *.tmp https://zhuanlan. ...

  3. 端口映射工具 redir/socat/xinetd - 运维技术 - 开源中国社区

    端口映射工具 redir/socat/xinetd - 运维技术 - 开源中国社区 端口映射工具 redir/socat/xinetd    10人收藏此文章, 我要收藏 发表于3天前(2013-08 ...

  4. Python基础 - 迭代

    前言 在pythone中经常可以看到iterable这样的描述. 直译为迭代. 这是在C中没有的概念. iterable(可迭代) 支持每次返回自己所包含的一个成员的对象就是可迭代对象. iterab ...

  5. 基于Cocos2dx开发卡牌游戏Demo_放开那三国 2.0

    PS:下载地址在最以下 1.登录 2.副本选择 3.地图 4. 选择敌人 5. 战斗 6. 战斗结算 7. 地图拓展 8. 武将拓展 9. 下载地址: 点击打开链接

  6. inkscape - 百度百科

    http://wapbaike.baidu.com/view/1267893.htm?ssid=0&from=844b&uid=3151E6C0905477A13653132D762B ...

  7. UVA 11090 - Going in Cycle!!(Bellman-Ford)

    UVA 11090 - Going in Cycle!! option=com_onlinejudge&Itemid=8&page=show_problem&category= ...

  8. Maven中Spring-Data-Redis存储对象(redisTemplate) (转)

    Redis是一种nosql数据库,在开发中常用做缓存.Jedis是Redis在java中的redis- client.在此之前,希望已经了解redis的基本使用和Maven的使用.建立Maven Pr ...

  9. httpcomponents-client-ga(4.5)

    http://hc.apache.org/httpcomponents-client-ga/tutorial/html/   Chapter 1. Fundamentals Prev     Next ...

  10. 屏蔽电信流氓广告造成的诡异的问题--Android WebView 长时间不能载入页面

    发如今家里的时候用Android App里的WebView打开站点非常慢,会有十几秒甚至更长时间的卡住. 可是在电脑上打开相同的网页却非常快. 查找这个问题的过程比較曲折,记录下来. 抓取Androi ...