POJ1849Two[DP|树的直径](扩展HDU4003待办)
Two
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 Sample Output 6 Source |
DP也可以,f[i]和g[i]分别处理两个人子树i进去回来和只进不回,f[i]=sum{w of i's son}*2,g[i]两种情况,两人进入i的同一个或不同一个孩子,好麻烦啊
其实答案就是sum{w}*2-w直径,无论从哪里开始都可以
证明:
虽然s是起点(很多人的起点就忽略了这个),
s在直径上好说,
假设s不再直径上,我们选择直径某点为root,从s到root到话费也是w*2,和从root开始一个到s又回来是一样的,然后就和上面一样了
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
const int N=1e5+;
int n,s,u,v,w,sum=,mx=;
struct edge{
int v,ne,w;
}e[N*];
int h[N],cnt=;
inline void ins(int u,int v,int w){
cnt++;
e[cnt].ne=h[u];e[cnt].v=v;e[cnt].w=w;h[u]=cnt;
cnt++;
e[cnt].ne=h[v];e[cnt].v=u;e[cnt].w=w;h[v]=cnt;
}
int f[N][];
int dp(int u,int fa){
int &t0=f[u][],&t1=f[u][];
if(t0!=-) return t0;
t0=;
for(int i=h[u];i;i=e[i].ne){
int v=e[i].v;
if(v==fa) continue;
int d=dp(v,u)+e[i].w;
if(d>t0) t1=t0,t0=d;
else if(d>t1) t1=d;
}
return t0;
}
int main(int argc, const char * argv[]) {
scanf("%d%d",&n,&s);
for(int i=;i<=n-;i++){
scanf("%d%d%d",&u,&v,&w);
ins(u,v,w);sum+=w*;
}
memset(f,-,sizeof(f));
dp(,-);
for(int i=;i<=n;i++)
mx=max(mx,f[i][]+f[i][]);
cout<<sum-mx;
//printf("\n\n%d %d",sum,mx);
return ;
}
扩展:2-->k
HDU4003(HDU最近挂了)
和选课很像了,这里特殊之处是d[i][0]的含义是一个下去又上来,其他的下去不上来
以后做做吧
POJ1849Two[DP|树的直径](扩展HDU4003待办)的更多相关文章
- VIJOS1476旅游规划[树形DP 树的直径]
描述 W市的交通规划出现了重大问题,市政府下决心在全市的各大交通路口安排交通疏导员来疏导密集的车流.但由于人员不足,W市市长决定只在最需要安排人员的路口安放人员.具体说来,W市的交通网络十分简单,它包 ...
- HDU 2196.Computer 树形dp 树的直径
Computer Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Su ...
- 换根DP+树的直径【洛谷P3761】 [TJOI2017]城市
P3761 [TJOI2017]城市 题目描述 从加里敦大学城市规划专业毕业的小明来到了一个地区城市规划局工作.这个地区一共有ri座城市,<-1条高速公路,保证了任意两运城市之间都可以通过高速公 ...
- hdu 4607 树形dp 树的直径
题目大意:给你n个点,n-1条边,将图连成一棵生成树,问你从任意点为起点,走k(k<=n)个点,至少需要走多少距离(每条边的距离是1): 思路:树形dp求树的直径r: a:若k<=r+1 ...
- computer(树形dp || 树的直径)
Computer Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Su ...
- hdu-2169 Computer(树形dp+树的直径)
题目链接: Computer Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) ...
- 「校内训练 2019-04-23」越野赛车问题 动态dp+树的直径
题目传送门 http://192.168.21.187/problem/1236 http://47.100.137.146/problem/1236 题解 题目中要求的显然是那个状态下的直径嘛. 所 ...
- POJ 3162.Walking Race 树形dp 树的直径
Walking Race Time Limit: 10000MS Memory Limit: 131072K Total Submissions: 4123 Accepted: 1029 Ca ...
- Computer(HDU2196+树形dp+树的直径)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2196 题目: 题意:有n台电脑,每台电脑连接其他电脑,第i行(包括第一行的n)连接u,长度为w,问你每 ...
随机推荐
- MySql基本概念(一)
MySQL基本概念 一. 数据库系统概述: mysql是数据库系统的一种,下面是所有数据库系统中主要的组件. 数据库系统由硬件部分和软件部分构成,硬件主要用于存储数据库中的数据,包括计算机.存储设备. ...
- js判断radiobuttonlist的选中值显示/隐藏其它模块
<script> $(function () { var SelectVal = $("input[name='rblGJS']:checked").val(); if ...
- crm2013关于contentIFrame不能使用
在CRM2011里面,我们可以在页面的控制台里面输入: contentIFrame.Xrm.Page.data.entity.getEntityName(); contentIFrame.Xrm.Pa ...
- Web自动化测试 Selenium 1/3
Selenium 名字的来源 在这里,我还想说一下关于 Selenium 名字的来源,很有意思的 : > : Selenium 的中文名为 “ 硒 ” ,是一种化学元素的名字,它 对 汞 ( M ...
- iOS开发-canOpenURL: failed for URL: "xx" - error:"This app is not allowed to query for scheme xx"
转载自:http://www.jianshu.com/p/e38a609f786e
- Android App 开发技能图谱
操作系统 Windows/MacOSX/Linux 编程语言 Java HTML/JS (Hybrid/Web App) C/C++ (NDK) SQL (DB) Kotlin 开发工具 IDE An ...
- OC中的protocol
一. 简单使用 1. 基本用途 可以用来声明一大堆方法(不能声明成员变量) 只要某个类遵守了这个协议,就相当于拥有这个协议中的所有方法声明 只要父类遵守了某个协议,就相当于子类也遵守了 2. 格式 协 ...
- 大家一起和snailren学java-(13)字符串
“好久没有写这个系列了.其实也有在看,不过觉得一些很基本的都写上来没意思.现在打算的是将整本书看完后,最后整合为一篇blog,筛选出一些平时没有注意到的或者更深入的理解” 在写程序中,字符串Strin ...
- mysql配置文件my.cnf详解
原文地址:mysql配置文件my.cnf详解 作者:gron basedir = path 使用给定目录作为根目录(安装目录). character-sets-dir = path 给出存放着字符集的 ...
- js实现页面跳转的几种方式
第一种: <script language="javascript" type="text/javascript"> wi ...