Destroy

Time Limit: 2000ms
Memory Limit: 65536KB

This problem will be judged on ZJU. Original ID: 3684
64-bit integer IO format: %lld      Java class name: Main

DJT country and CG country are always on wars.
This time DJT's King built a new information system over the whole country. He wants to know the message from the frontier immediately. There are numerous cities in DJT. In every city, there is one server of this system, and information is sending to the center continuously along a special data road. However, the data road is so expensive that there is one and only one road from one city to another city. Besides, the place of the center is a secret.

CG, of course, won't let DJT be happy for too long. CG is now planning to destroy DJT's new system. Due to some great undercover agents, CG has controlled some information about DJT's new system. The information CG has got:

  1. The center of DJT's new system would settle down in a city that for all other cities, the maximum distance should be the least.(you can make sure that only one city has the possibility to be the center)
  2. If no frontier city could send message back to the center, the system can be regard as destroyed. (a frontier city is a city that has only one road connecting to it)
  3. The length of each road.
  4. The power we need to destroy each road. (if we have a weapon of power max, we can destroy all the roads which it need the power less or equal to max)

Now, CG gives you a task: calculate the minimum power to destroy the system.

Input

There are multiple cases. For each case, one integer n (0 <= n <= 10000) indicating the number of cities in DJY country, cities are numbered from 1 to n, the next n-1 lines, one line contains four numbers describing one road, the two cities connected by the road, the length, and the power needed to destroy. The lengths are less than or equal to 10000. The powers are less than or equal to 100000000. All integers are nonnegative.

Output

For each case, output one number indicating the least power we need.

Sample Input

9
1 4 1 3
2 3 1 7
2 5 1 2
4 5 1 5
5 6 1 4
5 8 1 4
6 9 1 4
7 8 1 6

Sample Output

4
 

Source

Author

HE, Ningxu
 
解题:树的直径+树形dp(或者二分)
 #include <bits/stdc++.h>
using namespace std;
typedef long long LL;
const int INF = 0x3f3f3f3f;
const int maxn = ;
struct arc{
int to,len,power,next;
arc(int x = ,int y = ,int z = ,int nxt =-){
to = x;
len = y;
power = z;
next = nxt;
}
}e[maxn<<];
int head[maxn],d[maxn],p[maxn],tot,n,S,T;
int dp[maxn];
void add(int u,int v,int len,int power){
e[tot] = arc(v,len,power,head[u]);
head[u] = tot++;
}
queue<int>q;
int bfs(int u){
memset(d,-,sizeof d);
memset(p,-,sizeof p);
d[u] = ;
while(!q.empty()) q.pop();
q.push(u);
int ret = u;
while(!q.empty()){
u = q.front();
q.pop();
if(d[u] > d[ret]) ret = u;
for(int i = head[u]; ~i; i = e[i].next){
if(d[e[i].to] == -){
d[e[i].to] = d[u] + e[i].len;
p[e[i].to] = u;
q.push(e[i].to);
}
}
}
return ret;
}
void dfs(int u,int fa){
int tmp = ;
dp[u] = 0x3f3f3f3f;
bool flag = false;
for(int i = head[u]; ~i; i = e[i].next){
if(e[i].to == fa) continue;
dfs(e[i].to,u);
tmp = max(tmp,min(e[i].power,dp[e[i].to]));
flag = true;
}
if(flag) dp[u] = min(dp[u],tmp);
}
int main(){
int u,v,L,P;
while(~scanf("%d",&n)){
memset(head,-,sizeof head);
int root = tot = ,mx = INF;
for(int i = ; i < n; ++i){
scanf("%d%d%d%d",&u,&v,&L,&P);
add(u,v,L,P);
add(v,u,L,P);
}
u = T = bfs(S = bfs());
while(~u){
int tmp = max(d[T] - d[u],d[u]);
if(tmp < mx){
mx = tmp;
root = u;
}
u = p[u];
}
dfs(root,-);
printf("%d\n",dp[root]);
}
return ;
}

ZOJ 3684 Destroy的更多相关文章

  1. ZOJ 3684 Destroy 树的中心

    中心节点就是树的中心,2遍dfs求到树的直径.而中心一定在直径上,顺着直径找到中心就够了. 然后能够一遍树形DP找到最小值或者二分+推断是否訪问到叶子节点. #include <iostream ...

  2. 2014 Super Training #9 E Destroy --树的直径+树形DP

    原题: ZOJ 3684 http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3684 题意: 给你一棵树,树的根是树的中心(到其 ...

  3. ZOJ 2753 Min Cut (Destroy Trade Net)(无向图全局最小割)

    题目大意 给一个无向图,包含 N 个点和 M 条边,问最少删掉多少条边使得图分为不连通的两个部分,图中有重边 数据范围:2<=N<=500, 0<=M<=N*(N-1)/2 做 ...

  4. zoj 3261 Connections in Galaxy War

    点击打开链接zoj 3261 思路: 带权并查集 分析: 1 题目说的是有n个星球0~n-1,每个星球都有一个战斗值.n个星球之间有一些联系,并且n个星球之间会有互相伤害 2 根本没有思路的题,看了网 ...

  5. zoj 3620 Escape Time II dfs

    题目链接: 题目 Escape Time II Time Limit: 20 Sec Memory Limit: 256 MB 问题描述 There is a fire in LTR ' s home ...

  6. 洛谷 P1197 BZOJ 1015 [JSOI2008]星球大战 (ZOJ 3261 Connections in Galaxy War)

    这两道题长得差不多,都有分裂集合的操作,都是先将所有操作离线,然后从最后一步开始倒着模拟,这样一来,分裂就变成合并,也就是从打击以后最终的零散状态,一步步合并,回到最开始所有星球都被连为一个整体的状态 ...

  7. Backbone.js 中的Model被Destroy后,不能触发success的一个原因

    下面这段代码中, 当调用destroy时,backbone会通过model中的url,向服务端发起一个HTTP DELETE请求, 以删除后台数据库中的user数据. 成功后,会回调触发绑定到dest ...

  8. ZOJ People Counting

    第十三届浙江省大学生程序设计竞赛 I 题, 一道模拟题. ZOJ  3944http://www.icpc.moe/onlinejudge/showProblem.do?problemCode=394 ...

  9. ZOJ 3686 A Simple Tree Problem

    A Simple Tree Problem Time Limit: 3 Seconds      Memory Limit: 65536 KB Given a rooted tree, each no ...

随机推荐

  1. 双栈排序 2008年NOIP全国联赛提高组(二分图染色)

    双栈排序 2008年NOIP全国联赛提高组  时间限制: 1 s  空间限制: 128000 KB  题目等级 : 大师 Master     题目描述 Description Tom最近在研究一个有 ...

  2. 在Chrome与火狐中,输入框input类型为number时,如何去除掉的自带的上下默认箭头

    如何移除input='number'时浏览器自带的上下箭头: CSS样式: /* 去除input[type=number]浏览器默认的icon显示 */ input::-webkit-outer-sp ...

  3. Lightoj 1020 - A Childhood Game (博弈)

    题目链接: 1020 - A Childhood Game 题目描述: Alice和Bob在玩弹珠游戏,两人轮流拿走弹珠,每次只能拿走一个或者两个,当Alice作为先手时谁拿走最后一个就是输家,而Bo ...

  4. vue中引入swiper插件

    这里我们使用npm的方式安装swiper插件. 1.npm install vue-awesome-swiper --save 2.在main.js文件中引入文件 import Vue from 'v ...

  5. 如何快速部署Oracle Database

    Oracle Database在Linux系统上的安装是每一个初学者都必须面临的问题,只有正确的配置好了环境,才能进行后续的深入学习.本文旨在说明如何快速的部署Oracle的单实例环境,对于初学者,还 ...

  6. java邮件发送工具

    最近在web项目中,客户端注册时需要通过邮箱验证,服务器就需要向客户端发送邮件,我把发送邮件的细节进行了简易的封装: 在maven中需要导入: <!--Email--> <depen ...

  7. git tag管理

    操作 实例 创建标签 git tag -a V1.2 -m 'WebSite version 1.2' 查看标签 git tag / git show V1.2 远程推送 git push origi ...

  8. 微信小程序组件解读和分析:四、icon图标

      icon图标组件说明: icon是一种图标格式,用于系统图标.软件图标等,这种图标扩展名为.icon..ico.常见的软件或windows桌面上的那些图标一般都是ICON格式的.在应用上面很多地方 ...

  9. SQLiteOpenHelper学习

    0.视频:http://www.imooc.com/video/3384 1.SQLiteOpenHelper笔记: 2.SQLiteOpenHelper.java代码: import android ...

  10. 求助:可以使用任何编程工具做成一个控件或组件,使得在VB中能调用并得到摄像头的参数及图片。

    请看下网址上的这个问题,看是否有解决的方式http://www.educity.cn/wenda/338634.html