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. Struts2标签--S:iterator----jsp页面遍历双层list

    双层遍历,第一层list为classes,里面放的是班级class对象,第二层为班级class里的小组groups.     <s:iterator value="classes&qu ...

  2. HDU1173

    采矿 Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submiss ...

  3. easyui datagrid的json格式

    easyui datagrid的json格式: {"columns":[[{"field":"one","title": ...

  4. 使用动态内置的JSON 数据源

    自从ActiveReports 11发布以来,一个重磅功能推出,为Web开发人员又带来一大福利.JSON数据常常会通过WebService来动态生成JSON数据,因此动态链接JSON 数据内置参数会更 ...

  5. Ninject之旅之十二:Ninject在Windows Form程序上的应用(附程序下载)

    摘要: 下面的几篇文章介绍如何使用Ninject创建不同类型的应用系统.包括: Windows Form应用系统 ASP.NET MVC应用系统 ASP.NET Web Form应用系统 尽管对于不同 ...

  6. 《无所不能的JavaScript编程系列:arguments 参数对象》

    前言:无所不能的JavaScript JavaScript起源于Netscape公司的LiveScript语言,这是一种基于对象和事件驱动的客户端脚本语言,最初的设计是为了检验HTML表单输入的正确性 ...

  7. SUI Mobile

    <header class="bar bar-nav"> <h1 class='title'>只有图标的表单</h1> </header& ...

  8. 特性Attribute 的使用

    [IdentityAuthorize]           public ActionResult Index()        {             return View("~/V ...

  9. (二) 从Angular1到Angular2需要的预备知识

    1. TypeScript语法与ES6新特性 写惯了jQ的话突然从ES5跳到ES6,又是个变形的ES6(TypeScript),学习成本确实不低.不过笔者也是从ng1直接上手ng2,对与很多新特性的积 ...

  10. Kickstart Practice Round 2017 Google

    Problem B. Vote A and B are the only two candidates competing in a certain election. We know from po ...