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. AMD的规范使用

    1.解决命名冲突 2.解决繁琐的文件依赖 3. 可读性.可依赖性 参考这里 // foobar.js // 私有变量 var   test = 123: //  公有方法 function  foot ...

  2. spring controller接口中,用pojo对象接收页面传递的参数,发现spring在对pojo对象赋值时,有一定顺序的问题

    1.我的项目中的实体类都继承了基类entityBase,里面封装了分页的一些属性,pageindex.pagesize.pagerownum等. 2.思路是页面可以灵活的传递分页参数,比如当前页pag ...

  3. MyEclipse日志文件目录

    MyEclipse存放日志的目录为: <Workspace_Root>/.metadata/.log 在该目录下存放了日志的存档和当前日志,通过该日志可以查看MyEclipse的错误信息.

  4. 301 Remove Invalid Parentheses 删除无效的括号

    删除最小数目的无效括号,使输入的字符串有效,返回所有可能的结果.注意: 输入可能包含了除 ( 和 ) 以外的元素.示例 :"()())()" -> ["()()() ...

  5. MVC之模型绑定

    1.前言 MVC全名是Model View Controller,是模型(model)-视图(view)-控制器(controller)的缩写,一种软件设计典范,用一种业务逻辑.数据.界面显示分离的方 ...

  6. P1165 日志分析

    题目描述 M 海运公司最近要对旗下仓库的货物进出情况进行统计.目前他们所拥有的唯一记录就是一个记录集装箱进出情况的日志.该日志记录了两类操作:第一类操作为集装箱入库操作,以及该次入库的集装箱重量:第二 ...

  7. 浏览器的两种模式quirks mode 和strict mode

    关键字: javascript.quirks mode.strict mode 在看js代码时,有时会看到关于quirks mode(怪异模式)和strict mode(严格格式)的东西,一直也没深究 ...

  8. python学习笔记(6)——字典(Dictionary)

    dict= {key1 : value1, key2 : value2 ...} 关键词:字典中元素成对出现- key:value 格式- 两端{ } ,键:值,每对键值间用 ,隔开. 键key-唯一 ...

  9. PHP引用(&)的考察点

    引用的概念 在PHP中引用意味着用不同的名字访问同一个变量内容. 定义方式 使用 & 符号来表示 变量的引用 $a = 'ABC'; //开辟一块内存空间存储数据,$a指向该空间 $b = & ...

  10. Redis 注册为 widows 服务

    redis-server.exe --service-install redis.windows.conf --loglevel verbose