题意/Description:

    Christmas is coming to KCM city. Suby the loyal civilian in KCM city is preparing a big neat Christmas tree. The simple structure of the tree is shown in right picture.

    The tree can be represented as a collection of numbered nodes and some edges. The nodes are numbered 1 through n. The root is always numbered 1. Every node in the tree has its weight. The weights can be different from each other. Also the shape of every available edge between two nodes is different, so the unit price of each edge is different. Because of a technical difficulty, price of an edge will be (sum of weights of all descendant nodes) × (unit price of the edge).

    Suby wants to minimize the cost of whole tree among all possible choices. Also he wants to use all nodes because he wants a large tree. So he decided to ask you for helping solve this task by find the minimum cost.

 

读入/Input:

    The input consists of T test cases. The number of test cases T is given in the first line of the input file. Each test case consists of several lines. Two numbers ve (0 ≤ ve ≤ 50000) are given in the first line of each test case. On the next line,v positive integers wi indicating the weights of v nodes are given in one line. On the following e lines, each line contain three positive integers abc indicating the edge which is able to connect two nodes a and b, and unit price c.

    All numbers in input are less than 216.


输出/Output:

For each test case, output an integer indicating the minimum possible cost for the tree in one line. If there is no way to build a Christmas tree, print “No Answer” in one line.

 

题解/solution:

 这题一看就是最短路,因为题目说到求边缘的最小值。题目还讲到一个关于边缘价格的式子

{(边缘的价格)=(所有后代节点的权重之和)×(边缘单价)},这在输出的时候处理一下就行了。因为这题有T组数据,所以注意让数组和变量还原值。

   这题我用SPFA做,本因为简单的。结果出现了神奇的错误:Runtime Error。我一脸懵逼,改了不少地方,刷了10多次,才AC。结果那错了,我也不知道。我在这祝大家好运,没有此错误。

 

代码/Code:

constmaxE=100001;maxV=2000001;typearr=recordx,y,w,next:int64;end;varn,m,nm:longint;a:array [0..maxV] of arr;ls:array [0..maxE] of longint;list,b,d,v:array [0..maxE] of int64;ans,max:int64;procedure spfa;
vari,j,k,h,t:longint;
beginfillchar(d,sizeof(d),63);max:=d[1]; h:=0; t:=1;v[1]:=1; list[1]:=1; d[1]:=0;repeath:=h+1;j:=ls[list[h]];while j<>0 dobeginwith a[j] dobeginif d[x]+w<d[y] thenbegind[y]:=d[x]+w;if v[y]=0 thenbegint:=t+1;list[t]:=y;v[y]:=1;end;end;j:=next;end;end;v[list[h]]:=0;until h=t;
end;procedure init1;
beginfillchar(ls,sizeof(ls),0);fillchar(list,sizeof(list),0);fillchar(b,sizeof(b),0);fillchar(a,sizeof(a),0);fillchar(v,sizeof(v),0);
end;procedure init2;
vari,t,k:longint;
beginreadln(n,m);for i:=1 to n doread(b[i]);for i:=1 to m dobegint:=i*2; k:=t-1;with a[k] dobeginreadln(x,y,w);next:=ls[x];ls[x]:=k;end;with a[t] dobeginx:=a[k].y; y:=a[k].x; w:=a[k].w;next:=ls[x];ls[x]:=t;end;end;m:=m*2;
end;procedure print;
vari:longint;
beginans:=0;for i:=1 to n doif d[i]=max then begin writeln('No Answer'); exit; endelse ans:=d[i]*b[i]+ans;writeln(ans);
end;procedure init_main;
vari:longint;
beginreadln(nm);for i:=1 to nm dobegininit1;init2;spfa;print;end;
end;begininit_main;
end.

转载于:https://www.cnblogs.com/zyx-crying/p/9319720.html

PKU 3013 Big Christmas Tree 最短路 spfa相关推荐

  1. poj 3013 Big Christmas Tree(最短路变形)

    传送门:POJ 3013 Big Christmas Tree 描述: Big Christmas Tree Time Limit: 3000MS   Memory Limit: 131072K To ...

  2. POJ-3013: Big Christmas Tree(dij,spfa)

    POJ-3013: Big Christmas Tree 题目链接:POJ:3013 Description Christmas is coming to KCM city. Suby the loy ...

  3. 洛谷P1462 通往奥格瑞玛的道路 二分答案+最短路SPFA

    洛谷P1462 通往奥格瑞玛的道路 二分答案+最短路SPFA 二分交费最多的一次的钱数 然后只将符合要求的边加入图中 如果到终点的最短路大于等于血量 或者直接起点不能到达终点 那么说明不符合要求 需要 ...

  4. Russian Dolls on the Christmas Tree

    题目链接:Russian Dolls on the Christmas Tree 显然可以dfs序转区间之后上莫队,不过感觉会被卡. 其实也可以dfs序之后,对询问区间排序,然后线段树. 我懒得dfs ...

  5. 图论刷水题记录(二)(最短路-----SPFA算法)

    继第一篇的后续,又来刷水题了,写的是SPFA算法,这个算法的复杂度比较玄学,感觉能不用就不用了,但是他的好处就是可以判断负圈. 3月26日: 1.POJ 1847 Tram 题意:在一个交通网络上有N ...

  6. 单源最短路 SPFA 算法模板

    简介 在图论中,最短路是十分重要的一部分,在很多问题中都有涉及 而现在所讲的 SPFA 算法是十分优秀的算法,时间复杂度为 O(k∗E)O(k*E) 其中 EE 是图的边数,而 kk 是一个常数,一般 ...

  7. POJ 3259 Wormholes【最短路/SPFA判断负环模板】

    农夫约翰在探索他的许多农场,发现了一些惊人的虫洞.虫洞是很奇特的,因为它是一个单向通道,可让你进入虫洞的前达到目的地!他的N(1≤N≤500)个农场被编号为1..N,之间有M(1≤M≤2500)条路径 ...

  8. POJ - 2449 Remmarguts' Date(第k短路:spfa+A*)

    题目链接:点击查看 题目大意:给出一个有向图,求第k短路 题目分析:偷学了一波A*,本来以为是多难的算法,其实就是bfs+优先队列的升级版,之前看的那些博客写的都太深奥了,以至于看了一半啥都没看懂然后 ...

  9. 【bzoj1726/Usaco2006 Nov】Roadblocks第二短路——SPFA

    题目链接 分析:题目要求一个连通图的从1到n的严格次短路,我们只需要在跑最短路的时候顺便判一下次短路是否能够被更新即可. dis[x][0]表示1到x的最短路,而dis[x][1]则表示次短路,需要分 ...

最新文章

  1. hutool 取前12个月_Excel – 创建 12 个月的工资表模板,我只要 20 秒
  2. Linux学习之shell
  3. 16. GD32F103C8T6入门教程-adc 使用教程2-dma+连续扫描方式采集数据
  4. 巴菲特午宴中标者孙宇晨与王小川隔空互怼 还要对赌100个比特币?
  5. C++多态虚函数demo
  6. PHPSTORM 6.0.3 Xdebug 配置9
  7. CuteFtpnbsp;Pro之站点导入导出
  8. 易飞ERP--自定义报表之采购价格分析功能表
  9. 【面试】--【集合容器】
  10. 《DSP using MATLAB》Problem 7.36
  11. RSSI-RSRP-RSRQ
  12. Python献个礼 | 属于80、90后的 “儿童节” 礼物!
  13. Android学习之Pair类 (android.util.Pair )
  14. Cocos2d 面试题
  15. Jzoj P6305 最小值___单调栈优化dp
  16. 计算机专业浅谈:都说这行业35岁失业,是真的嘛?
  17. 绝对限量版PSP[全球只此一台]....
  18. 机械设备配套风机常用选型资料
  19. c语言 编译 flash,DSP-C2000 flash编程,自己定义的烧写函数在主函数中运行无法通过...
  20. ttyUSB 串口编程 Linux下 USB转串口

热门文章

  1. 人工蜂群算法的最小搜索模型_【优化求解】人工蜂群ABC算法
  2. Nginx 多域名多网站绑定及禁止IP访问
  3. 麦肯锡指出布局金融生态圈对中国国内银行意义重大
  4. macbook里软件打不开说是没有权限
  5. 使用@property
  6. ****** 四 ******、软设笔记【数据结构】-排序、插入排序、选择排序
  7. python装饰器使用
  8. 微信公众平台开发(83) 生成带参数二维码
  9. JAVA Timer 定时器
  10. 前景菜谱制作的seo技巧