Two
Time Limit: 1000MS   Memory Limit: 30000K
Total Submissions: 1092   Accepted: 527

Description

The city consists of intersections and streets that connect them.

Heavy snow covered the city so the mayor Milan gave to the winter-service a list of streets that have to be cleaned of snow. These streets are chosen such that the number of streets is as small as possible but still every two intersections to be connected i.e. between every two intersections there will be exactly one path. The winter service consists of two snow plovers and two drivers, Mirko and Slavko, and their starting position is on one of the intersections.

The snow plover burns one liter of fuel per meter (even if it is driving through a street that has already been cleared of snow) and it has to clean all streets from the list in such order so the total fuel spent is minimal. When all the streets are cleared of snow, the snow plovers are parked on the last intersection they visited. Mirko and Slavko don’t have to finish their plowing on the same intersection.

Write a program that calculates the total amount of fuel that the snow plovers will spend.

Input

The first line of the input contains two integers: N and S, 1 <= N <= 100000, 1 <= S <= N. N is the total number of intersections; S is ordinal number of the snow plovers starting intersection. Intersections are marked with numbers 1...N.

Each of the next N-1 lines contains three integers: A, B and C, meaning that intersections A and B are directly connected by a street and that street's length is C meters, 1 <= C <= 1000.

Output

Write to the output the minimal amount of fuel needed to clean all streets.

Sample Input

5 2
1 2 1
2 3 2
3 4 2
4 5 1

Sample Output

6

Source

 
 
 
 #include<iostream>
#include<stdio.h>
#include<cstring>
#include<cstdlib>
#include<vector>
#include<queue>
using namespace std; vector<int>Q[];
vector<int>val[];
int num[];
bool use[];
int dp[][]; void add(int x,int y,int c)
{
Q[x].push_back(y);
val[x].push_back(c);
num[x]++;
}
int min(int x,int y)
{
return x>y ? y:x;
}
void dfs(int k)
{
int i,j,s,t,cur;
use[k]=true;
for(i=;i<num[k];i++)
{
t=Q[k][i];
if(use[t]==true)continue;
dfs(t);
for(j=;j>=;j--)
{
dp[k][j]=dp[k][j]+*val[k][i]+dp[t][];
for(s=;s<=j;s++)
{
dp[k][j]=min(dp[k][j],dp[k][j-s]+dp[t][s]+s*val[k][i]);
}
}
}
}
int main()
{
int n,s,i;
int x,y,c;
while(scanf("%d%d",&n,&s)>)
{
for(i=;i<=n;i++)
{
Q[i].clear();
val[i].clear();
num[i]=;
use[i]=false;
}
memset(dp,,sizeof(dp));
for(i=;i<n;i++)
{
scanf("%d%d%d",&x,&y,&c);
add(x,y,c);
add(y,x,c);
}
dfs(s);
printf("%d\n",dp[s][]);
}
return ;
}

poj 1849 Two 树形dp的更多相关文章

  1. [POJ 1155] TELE (树形dp)

    题目链接:http://poj.org/problem?id=1155 题目大意:电视台要广播电视节目,要经过中转机构,到观众.从电视台到中转商到观众是一个树形结构,经过一条边需要支付成本.现在给你每 ...

  2. Apple Tree POJ - 2486 (树形dp)

    题目链接: D - 树形dp  POJ - 2486 题目大意:一颗树,n个点(1-n),n-1条边,每个点上有一个权值,求从1出发,走V步,最多能遍历到的权值 学习网址:https://blog.c ...

  3. Anniversary party POJ - 2342 (树形DP)

    题目链接:  POJ - 2342 题目大意:给你n个人,然后每个人的重要性,以及两个人之间的附属关系,当上属选择的时候,他的下属不能选择,只要是两个人不互相冲突即可.然后问你以最高领导为起始点的关系 ...

  4. POJ 3107.Godfather 树形dp

    Godfather Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 7536   Accepted: 2659 Descrip ...

  5. POJ 3342 (树形DP)

    题意 :给出一些上下级关系,要求i和i的直接上级不能同时出现,现在选出一些人构成一个集合,问你这个集合里面的最大人数是都少,同时给出这个最大的人数的集合是否唯一. 思路:树形DP,dp[i][0],表 ...

  6. POJ 2342 (树形DP)

    Anniversary party Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 3863   Accepted: 2172 ...

  7. POJ Anniversary party 树形DP

    /* 树形dp: 给一颗树,要求一组节点,节点之间没有父子关系,并且使得所有的节点的权值和最大 对于每一个节点,我们有两种状态 dp[i][0]表示不选择节点i,以节点i为根的子树所能形成的节点集所能 ...

  8. POJ 1947Rebuilding Roads(树形DP + 01背包)

    题目链接 题意:给出一个树形结构,求P个节点的子树最少要去掉几条边 分析:DP[root][j] 表示 以第 root 个为根节点, 包含j 个节点需要去掉几条边.那么对于 root 这个根节点来说, ...

  9. POJ 1947 (树形DP+背包)

    题目链接: http://poj.org/problem?id=1947 题目大意:树中各点都由一条边连接.问要弄出个含有m个点的(子)树,至少需要截去多少条边. 解题思路: 设dp[i][j]为i总 ...

随机推荐

  1. day65 Django模板语言

      常用语法 只需要记两种特殊符号: {{  }}和 {% %} 变量相关的用{{}},逻辑相关的用{%%}. 变量 {{ 变量名 }} 变量名由字母数字和下划线组成. 点(.)在模板语言中有特殊的含 ...

  2. QQ个人信息保护 | 攻的对面叫防

    近来我们愈来愈重视个人信息保护,当接到骚扰电话时,你心里或许在想(我手机号怎么又被别人知道的?别人是怎么知道我手机号的?),现在的时代,手机已或不可缺,QQ几乎每个人都在用.而有些人总想保护个人QQ资 ...

  3. beyondCompare试用期到期解决办法

    找到beyond Compare 4文件夹下面的BCUnrar.dll,将其删掉或者重命名,再重新打开接着使用! 我这是转的,到期之后试试.

  4. SQL 必知必会 总结(一)

    SQL必知必会 总结(一) 第 1 课 了解SQL 1.数据库(database): 保存有组织的数据容器(通常是一个文件或一组文件). 2.数据库管理系统(DBMS): 数据库软件,数据库是通过 D ...

  5. orm 练习题

    一: 多表练习查询 1. 自行创建测试数据: 2. 查询学生总人数: 3. 查询“生物”课程和“物理”课程成绩都及格的学生id和姓名: 4. 查询每个年级的班级数,取出班级数最多的前三个年级: 5. ...

  6. EJB3 阶段总结+一个EJB3案例 (1)

    经过一段时时间的学习,对EJB3的相关知识和jboss8的配置有了大概的了解. 网上对EJB的评论很多,基本都是负面的,都表示EJB太过于沉重,不容易维护.但通过这段时间的学习,私下认为,EJB3在某 ...

  7. 开源微信支付SDK

    应该有一年多没在博客园上写文章了,毕竟是一个记录自己技术成长的平台,没能将写博客长期坚持下来,说起来也是挺惭愧的.对于自己的近况而言,确实平常加班也比较多,时间会比较压缩,所以到后来博客也基本停止了更 ...

  8. hanlp的基本使用--python(自然语言处理)

    hanlp拥有:中文分词.命名实体识别.摘要关键字.依存句法分析.简繁拼音转换.智能推荐. 这里主要介绍一下hanlp的中文分词.命名实体识别.依存句法分析,这里就不介绍具体的hanlp的安装了,百度 ...

  9. Spring Security构建Rest服务-1203-Spring Security OAuth开发APP认证框架之短信验证码登录

    浏览器模式下验证码存储策略 浏览器模式下,生成的短信验证码或者图形验证码是存在session里的,用户接收到验证码后携带过来做校验. APP模式下验证码存储策略 在app场景下里是没有cookie信息 ...

  10. 正则中str.match(pattern)与pattern.exec(str)的区别

    这两个函数除了调用对象以及参数不同之外,<javascript高级程序设计>中对exec描述比较详细,对match只是说返回数组跟exec一样.书中并没有说只说了正则在非全局模式下的情况, ...