poj 1849 Two 树形dp
| Time Limit: 1000MS | Memory Limit: 30000K | |
| Total Submissions: 1092 | Accepted: 527 |
Description
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
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
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的更多相关文章
- [POJ 1155] TELE (树形dp)
题目链接:http://poj.org/problem?id=1155 题目大意:电视台要广播电视节目,要经过中转机构,到观众.从电视台到中转商到观众是一个树形结构,经过一条边需要支付成本.现在给你每 ...
- Apple Tree POJ - 2486 (树形dp)
题目链接: D - 树形dp POJ - 2486 题目大意:一颗树,n个点(1-n),n-1条边,每个点上有一个权值,求从1出发,走V步,最多能遍历到的权值 学习网址:https://blog.c ...
- Anniversary party POJ - 2342 (树形DP)
题目链接: POJ - 2342 题目大意:给你n个人,然后每个人的重要性,以及两个人之间的附属关系,当上属选择的时候,他的下属不能选择,只要是两个人不互相冲突即可.然后问你以最高领导为起始点的关系 ...
- POJ 3107.Godfather 树形dp
Godfather Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 7536 Accepted: 2659 Descrip ...
- POJ 3342 (树形DP)
题意 :给出一些上下级关系,要求i和i的直接上级不能同时出现,现在选出一些人构成一个集合,问你这个集合里面的最大人数是都少,同时给出这个最大的人数的集合是否唯一. 思路:树形DP,dp[i][0],表 ...
- POJ 2342 (树形DP)
Anniversary party Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 3863 Accepted: 2172 ...
- POJ Anniversary party 树形DP
/* 树形dp: 给一颗树,要求一组节点,节点之间没有父子关系,并且使得所有的节点的权值和最大 对于每一个节点,我们有两种状态 dp[i][0]表示不选择节点i,以节点i为根的子树所能形成的节点集所能 ...
- POJ 1947Rebuilding Roads(树形DP + 01背包)
题目链接 题意:给出一个树形结构,求P个节点的子树最少要去掉几条边 分析:DP[root][j] 表示 以第 root 个为根节点, 包含j 个节点需要去掉几条边.那么对于 root 这个根节点来说, ...
- POJ 1947 (树形DP+背包)
题目链接: http://poj.org/problem?id=1947 题目大意:树中各点都由一条边连接.问要弄出个含有m个点的(子)树,至少需要截去多少条边. 解题思路: 设dp[i][j]为i总 ...
随机推荐
- openvswitch datapath 内核态流表创建过程(ovs_flow_cmd_new)
datapath流表更新的入口函数都定义在dp_flow_genl_ops中,流表创建的入口函数是ovs_flow_cmd_new函数,通过该函数,我们可以一窥流表相关信息的建立. 1.ovs_flo ...
- Elasticsearch系列(五)----JAVA客户端之TransportClient操作详解
Elasticsearch JAVA操作有三种客户端: 1.TransportClient 2.JestClient 3.RestClient 还有种是2.3中有的NodeClient,在5.5.1中 ...
- spring json的支持
在spring中可以通过配置来实现对json的支持: 以下连接是看到的一篇对这方面内容讲解比较好的文章 http://www.cnblogs.com/fangjian0423/p/springMVC- ...
- my SQL Workbench创建数据库
1.首先连接数据库 2. 点击图中的红圈里的按钮,新建一个Schema,即数据库(个人理解..),修改一下Name 的值,如 mydatabase ,点击apply,再点apply,然后点finis ...
- 抓取出现时间3s后消失的元素
背景:日常自动化脚本编写中,可能需要定位,获取元素的位置,通常会遇到一种元素,只出现几秒,几秒后慢慢消失的,这个时候,如果要抓取这个提示,如果和它比手速,当你手速比较快,可以箭头抓取到,但当这个元素的 ...
- DOM时钟
使用JS使时钟运动 DOM运动,主要操作css3中transform:rotate(): 计时器setInterval(),setTimeout(),如何防止时钟偷停; 时钟的时针.分针.秒针的运动的 ...
- AbstractFactory抽象工厂模式(创建型模式)
1.new 的问题 常见的对象创建方法: //创建一个Road对象 Road road=new Road(); new的问题:实现依赖,不能应对具体实例的变化 怎么理解上面这句话呢? 可以这样理解:我 ...
- ubuntu16.04更改源为阿里源
一.通过系统更换源 第一步:备份原来的源文件cd /etc/apt/ 然后会显示下面的源文件sources.list输入命令sudo cp sources.list sources.list.bak ...
- 【Java并发编程】:并发新特性—塞队列和阻塞栈
阻塞队列 阻塞队列是Java5并发新特性中的内容,阻塞队列的接口是Java.util.concurrent.BlockingQueue,它有多个实现类:ArrayBlockingQueue.Delay ...
- RSNAKE 的 Slowloris DOS攻击工具初试
Slowloris 号称低带宽对服务器进行DDOS攻击 原理就是对WEB服务器发送 不完整的包并且以 单一 \r\n结尾,并不是 完整的HTTP包.造成WEB服务器堵塞达到最大连接数. 官网给出介绍 ...