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,问你每 ...
随机推荐
- 使用nodejs+express+socketio+mysql搭建聊天室
使用nodejs+express+socketio+mysql搭建聊天室 nodejs相关的资料已经很多了,我也是学习中吧,于是把socket的教程看了下,学着做了个聊天室,然后加入简单的操作mysq ...
- angular源码分析:图解angular的启动流程
今天做了一些图来说明angular,由于angular实在太复杂了,不知道用什么图表示比较好,所以就胡乱画了一些,希望有人能看得懂. 一.源码文件编译合并顺序图 二.angular.module函数功 ...
- SharePoint 2013 日历视图兼容性问题
在IE11上访问SharePoint 2013 calendar视图,发现加入兼容性视图以后访问,正常,如下图: 不加入兼容性视图IE11访问,出现兼容性问题,如下图: 因为有些环境有问题,有些环境没 ...
- 内网渗透测试思路-FREEBUF
(在拿到webshell的时候,想办法获取系统信息拿到系统权限) 一.通过常规web渗透,已经拿到webshell.那么接下来作重要的就是探测系统信息,提权,针对windows想办法开启远程桌面连接, ...
- Sharepoint学习笔记—习题系列--70-576习题解析 -(Q13-Q15)
Question 13 You are designing a SharePoint 2010 site. You need to design the site to meet all the fo ...
- Android Handler机制(三)----Looper源码解析
一.Looper Looper对象,顾名思义,直译过来就是循环的意思,从MessageQueue中不断取出message. Class used to run a message loop for a ...
- 【代码笔记】iOS-按钮带下划线的写法
一,效果图. 二,工程图. 三,代码. RootViewController.h RootViewController.m - (void)viewDidLoad { [super viewDidLo ...
- Java中并发问题整理
1. java中有几种方法可以实现一个线程? 使用Runnable,Callable,Thread或者线程池 2. 如何停止一个正在运行的线程? 可以使用正在运行的线程,支持线程中断,通常是定义一个v ...
- jquery.validate 验证机制
jquery.validate 验证机制 金刚 juqery juqery.validate 在开发系统时,使用了jquery.validate.js 这个验证插件,来校验数据合法性 重点 验证是以i ...
- yii2 GridView 下拉搜索实现案例教程
作者:白狼 出处:http://www.manks.top/article/yii2_gridview_dropdown_search本文版权归作者,欢迎转载,但未经作者同意必须保留此段声明,且在文章 ...