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

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

    于是用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. 性能调优2:CPU

    关系型数据库严重依赖底层的硬件资源,CPU是服务器的大脑,当CPU开销很高时,内存和硬盘系统都会产生不必需要的压力.CPU的性能问题,直观来看,就是任务管理器中看到的CPU利用率始终处于100%,而侦 ...

  2. LeetCode 657. Robot Return to Origin

    There is a robot starting at position (0, 0), the origin, on a 2D plane. Given a sequence of its mov ...

  3. C#里面滥用String造成的性能问题

    前两天给我们的json写一个解析函数, 之前用的正宗的json parser, 支持完整的json特性. 但是实际上我们用到特性, 只有key-value的映射, value的类型只有数字和字符串两种 ...

  4. 二十四、小程序中改变checkbox和radio的样式

    来源:https://blog.csdn.net/qq_39364032/article/details/79742415 在微信小程序里面,有时候为了配合整个项目的风格,checkbox和radio ...

  5. [LeetCode] Rank Scores -- 数据库知识(mysql)

    Write a SQL query to rank scores. If there is a tie between two scores, both should have the same ra ...

  6. p2394 精度题

    题意:输出n/23即可 解法一: 利用高精度的long double直接输出,但由于n的长度不确定,我们要加个限制%12Lf #include <cstdio> int main(){ l ...

  7. c++学习之字符串拼接

    在这里,强调这样一个问题: 可以看出,c++中,定义了string类,string 类方便我们进行字符串的一些操作,而不是像C语言中采用字符数组的方式或者指针的方式,通过上面的一些描述,可以发现: s ...

  8. python学习之第八篇——字典嵌套之字典中嵌套字典

    cities = { 'shanghai':{'country':'china','population':10000,'fact':'good'}, 'lendon':{'country':'eng ...

  9. 软工网络15团队作业7——Alpha冲刺之事后诸葛亮

    Deadline: 2018-5-16 22:00PM,以博客提交至班级博客时间为准 事后诸葛亮分析 Alpha冲刺,很多同学经历了"Learning by doing"的学一门新 ...

  10. Linux awk使用方法~~整理

    目录 awk行处理方式 awk命令格式 命令行格式 脚本格式 命令行格式——基本格式 awk内置变量 awk内置函数 测试数据 awk变量和函数使用实例 逻辑判断式 扩展格式 BEGIN 和 END ...