题意:给出一个图,每个节点都有权值,每条边也有费用。要求建立一颗树,使总花费最小。树上每连一条边的花费定义为孩子节点权值和×此边费用。

做法:分析可知,最终的答案为所有节点的权值×到根节点的距离。可以知道当距离最短时,花费最小。

    于是用Dijkstra+优先队列优化就可以搞定了。这题有些卡时间。最后还要注意使用long long,特判n=0和n=1。

 /*--------------------------------------------------------------------------------------*/
// Helica's header
// Second Edition
// 2015.11.7
//
#include <algorithm>
#include <iostream>
#include <cstring>
#include <ctype.h>
#include <cstdlib>
#include <cstdio>
#include <vector>
#include <string>
#include <queue>
#include <stack>
#include <cmath>
#include <set>
#include <map> //debug function for a N*M array
#define debug_map(N,M,G) printf("\n");for(int i=0;i<(N);i++)\
{for(int j=;j<(M);j++){\
printf("%d",G[i][j]);}printf("\n");}
//debug function for int,float,double,etc.
#define debug_var(X) cout<<#X"="<<X<<endl;
/*--------------------------------------------------------------------------------------*/
using namespace std; const int maxn = 5e4+;
const long long INF = 0x3f3f3f3f3f3f3f3f;
int N,M,T,S; struct qnode
{
int v,c;
qnode(int _v=,int _c=):v(_v),c(_c){}
bool operator < (const qnode &r) const
{return c>r.c;}
}; struct Edge
{
int to,next;
int cost;
}edge[*maxn]; int head[maxn],tol,weight[maxn];
long long dis[maxn];
bool vis[maxn];
priority_queue<qnode> que; void Dijkstra(int n,int start)
{
memset(vis,false,sizeof vis);
for(int i=;i<=n;i++) dis[i] = INF;
dis[start] = ;
while(!que.empty()) que.pop(); que.push(qnode(start,));
qnode cur;
while(!que.empty())
{
cur = que.top();
que.pop();
int u = cur.v;
if(vis[u]) continue;
vis[u] = true; for(int i=head[u];~i;i=edge[i].next)
{
int v = edge[i].to;
int cost = edge[i].cost;
//printf("u:%d v:%d cost:%d\n",u,v,cost);
if(!vis[v] && dis[v]>dis[u]+cost)
{
dis[v] = dis[u]+cost;
que.push(qnode(v,dis[v]));
}
}
}
} void add_edge(int u,int v,int cost)
{
edge[tol].to = u;
edge[tol].next = head[v];
edge[tol].cost = cost;
head[v] = tol++; edge[tol].to = v;
edge[tol].next = head[u];
edge[tol].cost = cost;
head[u] = tol++;
} int main()
{
scanf("%d",&T);
while(T--)
{
scanf("%d%d",&N,&M);
for(int i=;i<=N;i++) scanf("%d",&weight[i]);
memset(head,-,sizeof head);
memset(vis,false,sizeof vis);
tol = ;
for(int i=;i<M;i++)
{
int a,b,c;
scanf("%d%d%d",&a,&b,&c);
add_edge(a,b,c);
}
if(N==||N==)
{
printf("0\n");
continue;
}
Dijkstra(N,); long long ans = ;
bool flag = true;
for(int i=;i<=N;i++)
{
if(dis[i] == INF)
{
flag = false;
break;
}
else
ans += weight[i]*dis[i];
} if(!flag) printf("No Answer\n");
else printf("%lld\n",ans);
}
}

POJ3013-Big Christmas Tree-最短路的更多相关文章

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

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

  2. POJ3013 Big Christmas Tree(最短路径树)

    题目大概说给一张点和边都有权的图,现在要求其一棵以1结点为根的生成树使树的边权和最小,树边权 = 对应的图边权 * 树边末端点为根的子树所有结点对于图顶点的点权和. 要求∑(边权*子树点权和),等价于 ...

  3. POJ3013 Big Christmas Tree

    题目:http://poj.org/problem?id=3013 求每个点到1的最短路.不是最小生成树. 总是WA.看讨论里说INF至少2e10,于是真的A了! 算一下,dis最大可能3276800 ...

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

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

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

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

  6. poj 3013 Big Christmas Tree

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

  7. poj 3013 Big Christmas Tree (最短路径Dijsktra) -- 第一次用优先队列写Dijsktra

    http://poj.org/problem?id=3013 Big Christmas Tree Time Limit: 3000MS   Memory Limit: 131072K Total S ...

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

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

  9. poj 3013 Big Christmas Tree Djistra

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

  10. HDU 4871 Shortest-path tree 最短路 + 树分治

    题意: 输入一个带权的无向连通图 定义以顶点\(u\)为根的最短路生成树为: 树上任何点\(v\)到\(u\)的距离都是原图最短的,如果有多条最短路,取字典序最小的那条. 然后询问生成树上恰好包含\( ...

随机推荐

  1. redis-trib.rb命令详解

    redis-trib.rb是官方提供的Redis Cluster的管理工具,无需额外下载,默认位于源码包的src目录下,但因该工具是用ruby开发的,所以需要准备相关的依赖环境. 准备redis-tr ...

  2. 从 0 到 1 实现 React 系列 —— 4.setState优化和ref的实现

    看源码一个痛处是会陷进理不顺主干的困局中,本系列文章在实现一个 (x)react 的同时理顺 React 框架的主干内容(JSX/虚拟DOM/组件/生命周期/diff算法/setState/ref/. ...

  3. 十五、bootstrap-select的使用方法

    参考来源https://www.cnblogs.com/nianyifenzhizuo/p/8119462.html 需要的css和js <link rel="stylesheet&q ...

  4. Linux模拟控制网络时延

    之前以为可以使用Linux自带的工具模拟控制网络时延,所以上网找了一些资料.后来发现,找到的资料目前只支持在一个网卡上模拟发送报文的时延,而不能设置有差别的网络时延,或者说当要模拟的向A发送的时延与要 ...

  5. 05Hadoop 概论

    Hadoop的思想之源:Google Google搜索引擎,Gmail,安卓,AppspotGoogle Maps,Google earth,Google 学术,Google翻译,Google+,下一 ...

  6. asp.net mvc Dateset读取Excel数据

    //处理Excel //读取Excel [NonAction] public static DataSet ExcelToDS(string Path) { //根据情况选择合适的连接字符,参考msd ...

  7. DVWA学习笔记-----环境搭建

    DVWA是一款渗透测试的演练系统,在圈子里是很出名的.如果你需要入门,那么就选它了. 我们通常将演练系统称为靶机,下面请跟着我一起搭建DVWA测试环境.  安装PHP集成环境 我这里用的是phpstu ...

  8. 渗透测试平台bwapp简单介绍及安装

    先来介绍一下bwapp bwapp是一款非常好用的漏洞演示平台,包含有100多个漏洞 SQL, HTML, iFrame, SSI, OS Command, XML, XPath, LDAP, PHP ...

  9. # 【Python3练习题 004】输入某年某月某日,判断这一天是这一年的第几天?

    # [Python练习题 004]输入某年某月某日,判断这一天是这一年的第几天? # 思路:先判断是否为闰年,这关系到 2 月份的天数.# 之后再根据月份值把前几个月的天数累积加起来,最后再加上个“日 ...

  10. react单组件 渲染页面

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