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. this指针在不同情况下的指代

     说不同情况了吧,首先要分有几种情况使用this,然后再说分别指代什么 1)如果是一般标签下函数调用,this指代全局对象,也就是window对象或者document对象 2)如果在嵌套函数中被嵌套的 ...

  2. JavaBean在JSP中显示时间

    创建DateTimeBean的类,将其放置于org.caiduping.bean的包中,实现时间,星期的封装. package org.caiduping.bean; import java.text ...

  3. html保留字符详解

    本文由 www.169it.com 搜集整理 1. 注释 HTML中的注释和其它语言注释作用相似,都是为了方便阅读和调试代码.当浏览器遇到注释时会自动忽略注释内容.HTML的注释格式多行和单行注释都用 ...

  4. 开源而又强大的迷你型web服务器推荐

    appweb显然是不二之选,看了下最新版,已经到了4了 下载下来,http://appwebserver.org/software/appweb-4.4.4-0-src.tgz,十几M,直接吓傻,离我 ...

  5. Swift协议(Protocol)

    协议是为方法.属性等定义一套规范,没有具体的实现. 协议能够被类.结构体等具体实现(或遵守). protocol SomeProtocol { // protocoldefinition goes h ...

  6. OC3_歌词解析

    // // LrcManager.h // OC3_歌词解析 // // Created by zhangxueming on 15/6/15. // Copyright (c) 2015年 zhan ...

  7. echarts标准饼图解读(一)——提示框(tooltip)配置

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  8. Adapter模式

    Adapter模式主要用于将一个类的接口转换为另外一个接口,通常情况下再不改变原有体系的条件下应对新的需求变化,通过引入新的适配器类来完成对既存体系的扩展和改造.实现方式主要包括: 1.类的Adapt ...

  9. htm5实现视差动画

    requestAnimationFrame.js window.requestAnimFrame = (function() { return window.requestAnimationFrame ...

  10. ubuntu 阿里云安全配置

    1. 添加新用户, 加入 root 组, 赋予 sudo 权限 2. 禁用 root 3.