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总 ...
随机推荐
- django orm 以列表作为筛选条件进行查询
在Django的orm中进行查询操作时,可以通过传入列表,列表内的元素为索引值,作为一个筛选条件来进行行查询 from .models import UserInfo user_obj = UserI ...
- Bootstrap框架(二)
day58 巨幕 这是一个轻量.灵活的组件,它能延伸至整个浏览器视口来展示网站上的关键内容. Hello, world! This is a simple hero unit, a simple ju ...
- Volley源码解析
说点题外话,将近三个半月没有写博客了,年初换工作,在新的公司,上班第三天开始干活,花了二十来天弄了一个项目,上线后,接着又迭代了几个版本,不知不觉,试用期过完,才稍微有几天闲时.在年初的时候,就一直在 ...
- php curl 伪造IP来源的实例代码
来源:http://www.jb51.net/article/31694.htm curl 它不但可以模仿用户登录,还可以模仿用户IP地址哦,为伪造IP来源,本实例仅供参考哦 //伪造ip ; $i ...
- iOS开发总结--三方平台开发之分享
1.前言 在公司参与了多个应用三方平台的开发,涉及微信.微博.QQ.Facebook.meetup等,总结一下一般的接入三方平台SDK方法. 2.接入三方SDK 任何应用要接入三方平台,都需要在该平台 ...
- eclispe中使用python库 pyswip 进行prolog编程
from pyswip import Prolog prolog = Prolog() prolog.assertz("father(michael,john)") prolog. ...
- P1494 小Z的袜子
P1494 小Z的袜子 莫队板子题,对询问进行排序+分块,从而得到巧妙的复杂度 对于L,R的询问. 设其中颜色为x,y,z的袜子的个数为a,b,c... 那么答案即为 (a*(a-1)/2+b*(b- ...
- 【STM32H7教程】第14章 STM32H7的电源,复位和时钟系统
完整教程下载地址:http://forum.armfly.com/forum.php?mod=viewthread&tid=86980 第14章 STM32H7的电源,复位和时钟系 ...
- 如何用 纯C++(ndk)开发安卓应用 ?
视频教程请关注 http://edu.csdn.net/lecturer/lecturer_detail?lecturer_id=440 如何安装安卓的开发环境以及怎么设置ndk的环境变量等在前边的文 ...
- windows下安装并使用redis
一.安装前首先了解一下phpinfo里面的一些信息,能否正确安装非常有帮助. (下图是我的本机环境) compiler :编译器 Architecture :CPU架构 Configuration F ...