http://poj.org/problem?id=3013

Big Christmas Tree
Time Limit: 3000MS   Memory Limit: 131072K
Total Submissions: 19009   Accepted: 4048

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

Source

POJ Monthly--2006.09.29, Kim, Chan Min (kcm1700@POJ)
 
【题解】:
  变向的最短路劲问题:
      题目意思其实就是求各点到1节点的 最短路径*节点权值 之和,根节点1的权值没有用的
【code】:
 /**
Judge Status:Accepted Memory:2880K
Time:610MS Language:G++
Code Length:2062B Author:cj
*/ #include<iostream>
#include<queue>
#include<stdio.h>
#include<string.h>
#include<stdlib.h> #define N 50005
#define INF 1000000000000
using namespace std; struct Edge //保存边的结构体
{
int to; //边连接的另外个点
int next; //下一个搜索的节点
int w; //节点的权值
}edge[N<<]; struct Nod
{
int u; //进入队列中的点
__int64 dis; //到该点的距离
}now,temp; bool operator< (Nod a,Nod b) //优先队列重载'<'运算符
{
return a.dis>b.dis; //小到大
} int weight[N],head[N],visit[N];
__int64 dis[N]; //第一点到各点的最小距离 void init(int n) //初始化
{
int i;
for(i=;i<=n;i++)
{
visit[i] = ;
dis[i] = INF;
head[i] = -;
}
} void Dijkstra(int s)
{
int i,v;
dis[s] = ;
priority_queue<Nod> p_q; //优先队列 你懂的
temp.dis = ;
temp.u = s;
p_q.push(temp);
while(!p_q.empty())
{
temp = p_q.top(); //每次去的都是距离起点最小的点(优先队列的性质)
p_q.pop();
if(visit[temp.u]) continue;
visit[temp.u] = ;
for(i=head[temp.u];i!=-;i=edge[i].next) //每次遍历跟这个点有连接的所有点
{
v = edge[i].to;
if(!visit[v]&&dis[v]>dis[temp.u]+edge[i].w)
{
dis[v] = dis[temp.u]+edge[i].w; //距离更新
now.u = v;
now.dis = dis[v];
p_q.push(now); //压入队列
}
}
}
} int main()
{
int t;
scanf("%d",&t);
while(t--)
{
int m,n;
scanf("%d%d",&n,&m);
int i;
for(i=;i<n;i++) scanf("%d",weight+i);
int id = ;
init(n);
int a,b,c;
for(i=;i<m;i++)
{
scanf("%d%d%d",&a,&b,&c);
a--,b--;
edge[id].to = b;
edge[id].w = c;
edge[id].next = head[a];
head[a] = id++; //将边 a --> b保存 edge[id].to = a;
edge[id].w = c;
edge[id].next = head[b];
head[b] = id++; //将边 b --> a保存
}
Dijkstra();
__int64 res = ;
for(i=;i<n;i++)
{
if(dis[i]==INF) break;
res += dis[i]*weight[i];
}
if(i<n) puts("No Answer");
else printf("%I64d\n",res);
}
return ;
}

poj 3013 Big Christmas Tree (最短路径Dijsktra) -- 第一次用优先队列写Dijsktra的更多相关文章

  1. POJ 3013 Big Christmas Tree(最短Dijkstra+优先级队列优化,SPFA)

    POJ 3013 Big Christmas Tree(最短路Dijkstra+优先队列优化,SPFA) ACM 题目地址:POJ 3013 题意:  圣诞树是由n个节点和e个边构成的,点编号1-n. ...

  2. poj 3013 Big Christmas Tree

    Big Christmas Tree Time Limit: 3000MS   Memory Limit: 131072K Total Submissions: 20974   Accepted: 4 ...

  3. poj 3013 Big Christmas Tree Djistra

    Big Christmas Tree 题意:图中每个节点和边都有权值,图中找出一颗树,树根为1使得 Σ(树中的节点到树根的距离)*(以该节点为子树的所有节点的权值之和) 结果最小: 分析:直接求出每个 ...

  4. poj 3013 Big Christmas Tree (dij+优先级队列优化 求最短)

    模板 意甲冠军:给你一个图,1始终根,每一方都有单价值,每个点都有权重新. 每个边缘的价格值 = sum(后继结点重)*单价方值. 最低价格要求树值,它构成了一棵树n-1条边的最小价值. 算法: 1. ...

  5. SPFA/Dijkstra POJ 3013 Big Christmas Tree

    题目传送门 题意:找一棵树使得造价最少,造价为每个点的子节点造价和*边的造价和 分析:最短路跑出1根节点到每个点的最短边权值,然后每个点的权值*最短边距和就是答案,注意INF开足够大,n<=1特 ...

  6. POJ Big Christmas Tree(最短的基础)

    Big Christmas Tree 题目分析: 叫你构造一颗圣诞树,使得 (sum of weights of all descendant nodes) × (unit price of the ...

  7. POJ3013 Big Christmas Tree[转换 最短路]

    Big Christmas Tree Time Limit: 3000MS   Memory Limit: 131072K Total Submissions: 23387   Accepted: 5 ...

  8. POJ 3013 SPFA算法,邻接表的使用

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

  9. Big Christmas Tree(poj-3013)最短路

    Big Christmas Tree Time Limit: 3000MS   Memory Limit: 131072K Total Submissions: 25823   Accepted: 5 ...

随机推荐

  1. C# 截图类

    注意修改命名空间using System.Drawing; using System.Windows.Forms;   namespace WindowsFormsApplication1 {     ...

  2. Jersey(1.19.1) - Client API, Security with Http(s)URLConnection

    With Http(s)URLConnection The support for security, specifically HTTP authentication and/or cookie m ...

  3. sql常识-LEFT JOIN

    SQL LEFT JOIN 关键字 LEFT JOIN 关键字会从左表 (table_name1) 那里返回所有的行,即使在右表 (table_name2) 中没有匹配的行. LEFT JOIN 关键 ...

  4. JavaScript之表格修改

    讲到表格,我们不免都了解它的属性及用途. colspan跨列(纵向的)和rowspan跨行(横向的). 表格中<tr></tr>标签标示行标签:<td></t ...

  5. centos6.5下磁盘分区及挂载

    1..查看磁盘空间 2.磁盘分区 3.格式化分区 4.挂载/卸载

  6. SecureCRT连接虚拟机中的Linux系统(Ubuntu)

    最近在学习Linux,看了网上很多SecureCRT连接本地虚拟机当中的Linux系统,很多都是需要设置Linux的配置文件,有点繁琐,所以自己就摸索了一下,把相关操作贴出来分享一下. SecureC ...

  7. Android系统中提供的原子操作

    代码的实现位于文件system/core/include/cutils中 http://androidxref.com/4.4.3_r1.1/xref/system/core/include/cuti ...

  8. From MSI to WiX, Part 4 - Features and Components by Alex Shevchuk

    Following content is directly reprinted from : http://blogs.technet.com/b/alexshev/archive/2008/08/2 ...

  9. 如何禁止KnockoutJs在VS2012的智能格式化

    http://blogs.msdn.com/b/webdev/archive/2013/03/04/disabling-knockout-intellisense.aspx 我升级了一下VS2012, ...

  10. linux环境下配置github远程仓库

    1.设置git用户和邮箱 git config --global user.name "fujinzhou" git config --global user.email &quo ...