Big Christmas Tree
Time Limit: 3000MS   Memory Limit: 131072K
Total Submissions: 19029   Accepted: 4058

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.

Sample Input

2
2 1
1 1
1 2 15
7 7
200 10 20 30 40 50 60
1 2 1
2 3 3
2 4 2
3 5 4
3 7 2
3 6 3
1 5 9

Sample Output

15
1210 学习了一下SPFA算法,咋一看去以为这个题目是最小生成树,但是题目条件边的权值由子节点的点值和决定,这个就很纠结了,。。。在建树过程中,还要一边求某边的子节点,不是很蛋疼吗。。。后来大牛们都说是SPFA最短路求。。。推了一下,发现真是,当求出了源点1到各节点的最短路,再乘以节点值,再累加,就奇迹般的达到了上述效果 最短路就是用SPFA实现的,为了运用SPFA算法,首先还得学会使用邻接表。
邻接表和邻接矩阵的思想相反,邻接表存贮边信息,运用链表的思想将读入的所有边都存在一个链表中、
邻接表的实现是这样的,
for 1 to m
scanf a,b,c
u[i]=a,v[i]=b,w[i]=c;
next[i]=first[u[i]];
first[u[i]]=i;
稍微递推一下,发现,这链表是自尾向前链接的,主要就是它的next放在first前面,first里面保存u[i]节点是第几条边,初始化first数组全部为-1,这也就不难理解,如果u[i]是第一次读入,那next[i]值为-1,表示下面没有跟u[i]再相邻的未访问边了,而first数组的存在使得如果再次读入u[i]为初始节点,则马上更新。。到时候访问的时候实际上是从最新读入的边访问到最底层的边,有点类似于后进先出的栈的味道
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <queue>
#define maxn 50005
#define inf 0x7fffffffffffffff
using namespace std; int v[maxn*];
int u[maxn*];
int w[maxn*];
int pre[maxn*];
int next[maxn*];
long long d[maxn];
int vn[maxn];
bool isqueue[maxn];
void init(int n)
{
for (int i=;i<=n;i++)
{
d[i]=inf;
isqueue[i]=;
}
memset(pre,-,sizeof pre); //初始化邻接表的数组全为-1
}
int cnt;
void addedge(int be,int de,int ve) //建立邻接表
{
u[cnt]=be;
v[cnt]=de;
w[cnt]=ve;
next[cnt]=pre[be];
pre[be]=cnt++;
}
int main()
{
int t;
scanf("%d",&t);
while (t--)
{
int n,m;
scanf("%d %d",&n,&m);
init(n);
int i,j,k;
for (i=;i<=n;i++)
{
scanf("%d",&vn[i]);
}
cnt=;
for (j=;j<=m;j++)
{
int a,b,c;
scanf("%d %d %d",&a,&b,&c);
addedge(a,b,c);
addedge(b,a,c);
}
d[]=;
queue<int> q;
q.push();
isqueue[]=;
while (!q.empty())//SPFA通过队列来实现
{
int vs=q.front();
q.pop();
isqueue[vs]=;
for (int qq=pre[vs];qq>=;qq=next[qq]) //访问与该节点相连的各条边,直至-1,即没有了边
{ if (d[v[qq]]>d[vs]+w[qq])
{
d[v[qq]]=d[vs]+w[qq];//将新点存入队列。
if (!isqueue[v[qq]])
{
q.push(v[qq]);
isqueue[v[qq]]=;
} }
}
}//循环结束即队列完全清空,这也是SPFA的精髓,一旦有点改动,就放入队列,再进行一遍边松弛,如果一直到队列清空,都没有点改动,说明此时已达到最短
d[]=;
bool flag=true;
for (k=;k<=n;k++)
{
if (d[k]==inf) {flag=false;break;}
d[]+=d[k]*vn[k]; //第一个点的点权值无需考虑,对求出的最短路和点值进行相乘累加即是最终答案
}
if (!flag) puts("No Answer");
else
printf("%lld\n",d[]);
}
return ;
}

POJ 3013 SPFA算法,邻接表的使用的更多相关文章

  1. POJ 1511 Invitation Cards (spfa的邻接表)

    Invitation Cards Time Limit : 16000/8000ms (Java/Other)   Memory Limit : 524288/262144K (Java/Other) ...

  2. hdu 1874 畅通工程(spfa 邻接矩阵 邻接表)

    题目链接 畅通工程,可以用dijkstra算法实现. 听说spfa很好用,来水一发 邻接矩阵实现: #include <stdio.h> #include <algorithm> ...

  3. POJ 1511 - Invitation Cards 邻接表 Dijkstra堆优化

    昨天的题太水了,堆优化跑的不爽,今天换了一个题,1000000个点,1000000条边= = 试一试邻接表 写的过程中遇到了一些问题,由于习惯于把数据结构封装在 struct 里,结果 int [10 ...

  4. poj1273--Drainage Ditches(最大流Edmond-Karp算法 邻接表实现)

    最大流模板题 大部分Edmond-Karp算法代码都是邻接矩阵实现,试着改成了邻接表. #include <iostream> #include <cstdio> #inclu ...

  5. nyoj 239 月老的难题【匈牙利算法+邻接表】

    月老的难题 时间限制:1000 ms  |  内存限制:65535 KB 难度:4   描述 月老准备给n个女孩与n个男孩牵红线,成就一对对美好的姻缘. 现在,由于一些原因,部分男孩与女孩可能结成幸福 ...

  6. hdu 2444 The Accomodation of Students(二分匹配 匈牙利算法 邻接表实现)

    The Accomodation of Students Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K ( ...

  7. ural 1109,NYOJ 239,匈牙利算法邻接表

    NYOJ 239:http://acm.nyist.net/JudgeOnline/problem.php?pid=239 ural 1109 :http://acm.timus.ru/problem ...

  8. ZSTU OJ 3999 零基础学算法---邻接表

    题目:Click here 题意:我就喜欢中文题! 分析:这个题虽然是中文题,但是还是有一点费解的.其实就是给你一棵树,是用图的形式给你的,只知道a,b之间有一条边,并不知道谁是父,谁是子.思路就是先 ...

  9. poj 3013 SPFA

    首先看题看的很懵.. 然后这题直接没想用Djstra做 TLE了.看discuss,Dijstra要用堆优化,也可以用SPFA做. 这里在网上找了这两种做法的区别,点多稠密图用Dij,以为它是操作点的 ...

随机推荐

  1. 007.CI4框架CodeIgniter, 加载自己的helper辅助类,调用自己helper中定义各种全局函数

    01. 我们在Helpers文件中创建一个Tx_helper.php的文件,里面就下一个函数 <?php //输出 function ShowMessage($AMsg) { echo &quo ...

  2. C# 控制台应用程序从外部传参运行和调试

    参考:/*十有三博客*/ 新建一个用于演示的控制台应用程序项目,然后在Program.cs的入口Main方法里编写如下代码 foreach (var arg in args) { Console.Wr ...

  3. pyhton输出表格数据出现省略号?(教你很快解决)

    //2019.07.18 pandas是python提供的非常好用的数据分析模块,但是在使用pandas进行数据分析时,有时候需要查看打印的结果,当dataframe行数或者列数比较多的时候,打印结果 ...

  4. 安卓fragment transaction add方法报错

    这个问题百度了很多能用的很少! 原来看的B站的视频教程比较老了参数不匹配!我记一下安卓studio3.1的方法 切换fragment 前都先要 FragmentManager manager=getS ...

  5. php.laravel.middleware

    关于中间件,在php-laravel中的定义就是对请求的一个过滤,相当于JSP技术中的filter的存在.需要知道编写了一个中间件可以配置在三个地方(就目前5.7版本而言)让其发挥作用,具体需要看/a ...

  6. 050-PHP除法运算

    <?php $n=10/3; //除法运算 echo $n; //输出变量n的值 ?>

  7. 160-PHP 文本替换函数str_replace(一)

    <?php $str='Hello world!'; //定义源字符串 $search='o'; //定义将被替换的字符 $replace='O'; //定义替换的字符串 $res=str_re ...

  8. 114-PHP判断类变量是否相同

    <?php class ren{ //定义人类 } class mao{ //定义人类 } $ren=new ren(); //实例化人类的对象 $ren_a=new ren(); //实例化人 ...

  9. HDU - 4405 Aeroplane chess(期望dp)

    题意:沿着x轴从0走到大于等于N的某处,每一步的步数由骰子(1,2,3,4,5,6)决定,若恰好走到x轴上某飞行路线的起点,则不计入扔骰子数.问从0走到大于等于N的某处的期望的扔骰子次数. 分析: 1 ...

  10. 每天一点点之vue框架开发 - vue-router路由进阶(路由别名、跳转、默认路由、二级路由、路由守卫)

    路由别名   在main.js中的路由中添加name来创建别名 const routes = [ {path:'/footer',name:footerLink,component:Footer} ] ...