2435: [Noi2011]道路修建

Time Limit: 10 Sec  Memory Limit: 128 MB
Submit: 2188  Solved: 639
[Submit][Status]

Description

在 W 星球上有 n 个国家。为了各自国家的经济发展,他们决定在各个国家
之间建设双向道路使得国家之间连通。但是每个国家的国王都很吝啬,他们只愿
意修建恰好 n – 1条双向道路。 每条道路的修建都要付出一定的费用, 这个费用等于道路长度乘以道路两端的国家个数之差的绝对值。例如,在下图中,虚线所示道路两端分别有 2 个、4个国家,如果该道路长度为 1,则费用为1×|2 – 4|=2。图中圆圈里的数字表示国家的编号。


由于国家的数量十分庞大,道路的建造方案有很多种,同时每种方案的修建
费用难以用人工计算,国王们决定找人设计一个软件,对于给定的建造方案,计
算出所需要的费用。请你帮助国王们设计一个这样的软件。

Input

输入的第一行包含一个整数n,表示 W 星球上的国家的数量,国家从 1到n
编号。接下来 n – 1行描述道路建设情况,其中第 i 行包含三个整数ai、bi和ci,表
示第i 条双向道路修建在 ai与bi两个国家之间,长度为ci。

Output

输出一个整数,表示修建所有道路所需要的总费用。

Sample Input

6
1 2 1
1 3 1
1 4 2
6 3 1
5 2 1

Sample Output

20

HINT

n = 1,000,000 1≤ai, bi≤n

0 ≤ci≤ 10^6

Source

Day2

 

题解:其实就是个搜索,由于题目中给了一棵树,所以直接建树,然后求出每个点有多少子孙(算自己)。。。本身应该不难的,但是对于P党问题来了——BFS嘛,搞死了还是超时(坑爹的int64运算简直慢到哭);DFS呵呵直接爆栈不解释(对于C++党的linux下系统栈无限表示严重鄙视!!TT传送门 ),于是我还是壮烈的TLE了,求神犇帮助(不过程序算法应该没有问题,就是int64害得......)

BFS:

 1 type
 2     point=^node;
 3     node=record
 4                g,w:longint;
 5                next:point;
 6     end;
 7 
 8 var
 9    i,j,k,l,m,n:longint;ll:int64;
    a:array[..] of point;
    c,b,d,e:array[..] of longint;
    p:point;
 procedure add(x,y,z:longint);inline;
           var
              p:point;
           begin
                new(p);
                p^.g:=y;
                p^.w:=z;
                p^.next:=a[x];
                a[x]:=p;
           end;
 procedure bfs1;inline;
           var
              p:point;f,r:longint;
           begin
                b[]:=;d[]:=;
                f:=;r:=;
                while f<r do
                      begin
                           p:=a[d[f]];
                           while p<>nil do
                                 begin
                                      if b[p^.g]= then
                                         begin
                                              b[d[f]]:=;
                                              b[p^.g]:=;
                                              c[p^.g]:=d[f];
                                              d[r]:=p^.g;
                                              inc(r);
                                         end;
                                      p:=p^.next;
                                 end;
                           inc(f);
                      end;
           end;
 begin
      readln(n);
      for i:= to n do a[i]:=nil;
      for i:= to n- do
          begin
               readln(j,k,l);
               add(j,k,l);
               add(k,j,l);
          end;
      fillchar(b,sizeof(b),);ll:=;
      fillchar(c,sizeof(c),);fillchar(d,sizeof(d),);
      bfs1;ll:=;
      for i:=n downto  do
          begin
               e[d[i]]:=;
               p:=a[d[i]];
               while p<>nil do
                     begin
                          if p^.g<>c[d[i]] then
                             begin
                                  ll:=ll+int64(abs(e[p^.g]-(n-e[p^.g])))*int64(p^.w);
                                  e[d[i]]:=e[d[i]]+e[p^.g];
                             end;
                          p:=p^.next;
                     end;
          end;
      writeln(ll);
      readln; end.       

DFS:

 type
point=^node;
node=record
g,w:longint;
next:point;
end; var
i,j,k,l,m,n:longint;ll:int64;
a:array[..] of point;
c,b:array[..] of longint;
procedure add(x,y,z:longint);inline;
var
p:point;
begin
new(p);
p^.g:=y;
p^.w:=z;
p^.next:=a[x];
a[x]:=p;
end;
procedure dfs(x:longint);inline;
var
p:point;
begin
if b[x]= then exit;
p:=a[x];b[x]:=;
while p<>nil do
begin
if b[p^.g]= then
begin
dfs(p^.g);
ll:=ll+int64(int64(abs(int64(c[p^.g])-int64(int64(n)-int64(c[p^.g]))))*int64(p^.w));
c[x]:=c[x]+c[p^.g];
end;
p:=p^.next;
end;
end;
begin
readln(n);
for i:= to n do
begin
c[i]:=;
a[i]:=nil;
end;
for i:= to n- do
begin
readln(j,k,l);
add(j,k,l);
add(k,j,l);
end;
fillchar(b,sizeof(b),);ll:=;
dfs();
writeln(ll);
end.

2435: [Noi2011]道路修建的更多相关文章

  1. bzoj 2435: [Noi2011]道路修建 树上 dp

    2435: [Noi2011]道路修建 Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://www.lydsy.com/JudgeOnline/pr ...

  2. BZOJ 2435: [Noi2011]道路修建( dfs )

    NOI的水题...直接一遍DFS即可 ------------------------------------------------------------------------- #includ ...

  3. BZOJ 2435: [Noi2011]道路修建 dfs搜图

    2435: [Noi2011]道路修建 Description 在 W 星球上有 n 个国家.为了各自国家的经济发展,他们决定在各个国家之间建设双向道路使得国家之间连通.但是每个国家的国王都很吝啬,他 ...

  4. 2435: [Noi2011]道路修建(树上操作)

    2435: [Noi2011]道路修建 题目:传送门 题解: 建完边之后以1为根建树,统计深度和各个点的子树大小(包括自己) 询问的时候:答案=长度*abs(n-深度大的点的子树大小*2) ans+= ...

  5. 【BZOJ】2435: [Noi2011]道路修建(树形dp)

    http://www.lydsy.com/JudgeOnline/problem.php?id=2435 我怎么感觉那么水.. 坑的是,dfs会爆...好吧..用bfs.. //upd:我的智商也是醉 ...

  6. 2435: [Noi2011]道路修建 - BZOJ

    Description 在 W 星球上有 n 个国家.为了各自国家的经济发展,他们决定在各个国家之间建设双向道路使得国家之间连通.但是每个国家的国王都很吝啬,他们只愿意修建恰好 n – 1条双向道路. ...

  7. bzoj 2435: [Noi2011]道路修建

    Description 在 W 星球上有 n 个国家.为了各自国家的经济发展,他们决定在各个国家 之间建设双向道路使得国家之间连通.但是每个国家的国王都很吝啬,他们只愿 意修建恰好 n – 1条双向道 ...

  8. bzoj 2435: [Noi2011]道路修建【树形dp】

    dp求size和deep,然后对每条边模拟求代价即可 #include<iostream> #include<cstdio> #include<algorithm> ...

  9. NOI2011道路修建

    2435: [Noi2011]道路修建 Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 1974  Solved: 550[Submit][Status ...

随机推荐

  1. Java学习之旅基础知识篇:面向对象之封装、继承及多态

    Java是一种面向对象设计的高级语言,支持继承.封装和多态三大基本特征,首先我们从面向对象两大概念:类和对象(也称为实例)谈起.来看看最基本的类定义语法: /*命名规则: *类名(首字母大写,多个单词 ...

  2. python bottle 简介

    bottle是一个轻量级的python web框架, 可以适配各种web服务器,包括python自带的wsgiref(默认),gevent, cherrypy,gunicorn等等.bottle是单文 ...

  3. Javascript中的 “&” 和 “|” 你知多少?

    .v-top { position: relative; top: -5px; font-size: 12px } 一.前言: 在文章开始之前,先出几个题目给大家看看: var num1 = 1 &a ...

  4. POJ1753 搜索

    Flip Game Description Flip game is played on a rectangular 4x4 field with two-sided pieces placed on ...

  5. 持久层框架之MyBatis

    1.mybatis框架介绍: MyBatis 本是apache的一个开源项目iBatis, 2010年这个项目由apache software foundation 迁移到了google code,并 ...

  6. css 样式重置

    html, body, div, span, applet, object, iframe, h1, h2, h3, h4, h5, h6, p, blockquote, pre, a, abbr, ...

  7. 使用python制作ArcGIS插件(2)代码编写

    使用python制作ArcGIS插件(2)代码编写 by 李远祥 上一章节已经介绍了如何去搭建AddIn的界面,接下来要实现具体的功能,则到了具体的编程环节.由于使用的是python语言进行编程,则开 ...

  8. ECJTUACM16 Winter vacation training #4 题解&源码

    A......................................................................................... 题目链接→Code ...

  9. Raid类型

    raid0:两块磁盘同时写入raid1:一块磁盘写入,另一块磁盘做备份,利用率50%raid5:两块磁盘同时写入两块磁盘都有内容,第三块磁盘做效应,n/n-1的利用率raid6:四块磁盘两块磁盘做存储 ...

  10. Ansible_自动化运维《Ansible之初识-1》

    1.Ansible简介 1.1 Ansible介绍 Ansible 是一个简单的自动化运维管理工具,基于Python开发,集合了众多运维工具(puppet.cfengine.chef.func.fab ...