POJ3013-Big Christmas Tree-最短路
题意:给出一个图,每个节点都有权值,每条边也有费用。要求建立一颗树,使总花费最小。树上每连一条边的花费定义为孩子节点权值和×此边费用。
做法:分析可知,最终的答案为所有节点的权值×到根节点的距离。可以知道当距离最短时,花费最小。
于是用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-最短路的更多相关文章
- POJ3013 Big Christmas Tree[转换 最短路]
Big Christmas Tree Time Limit: 3000MS Memory Limit: 131072K Total Submissions: 23387 Accepted: 5 ...
- POJ3013 Big Christmas Tree(最短路径树)
题目大概说给一张点和边都有权的图,现在要求其一棵以1结点为根的生成树使树的边权和最小,树边权 = 对应的图边权 * 树边末端点为根的子树所有结点对于图顶点的点权和. 要求∑(边权*子树点权和),等价于 ...
- POJ3013 Big Christmas Tree
题目:http://poj.org/problem?id=3013 求每个点到1的最短路.不是最小生成树. 总是WA.看讨论里说INF至少2e10,于是真的A了! 算一下,dis最大可能3276800 ...
- POJ 3013 Big Christmas Tree(最短Dijkstra+优先级队列优化,SPFA)
POJ 3013 Big Christmas Tree(最短路Dijkstra+优先队列优化,SPFA) ACM 题目地址:POJ 3013 题意: 圣诞树是由n个节点和e个边构成的,点编号1-n. ...
- Big Christmas Tree(poj-3013)最短路
Big Christmas Tree Time Limit: 3000MS Memory Limit: 131072K Total Submissions: 25823 Accepted: 5 ...
- poj 3013 Big Christmas Tree
Big Christmas Tree Time Limit: 3000MS Memory Limit: 131072K Total Submissions: 20974 Accepted: 4 ...
- poj 3013 Big Christmas Tree (最短路径Dijsktra) -- 第一次用优先队列写Dijsktra
http://poj.org/problem?id=3013 Big Christmas Tree Time Limit: 3000MS Memory Limit: 131072K Total S ...
- POJ Big Christmas Tree(最短的基础)
Big Christmas Tree 题目分析: 叫你构造一颗圣诞树,使得 (sum of weights of all descendant nodes) × (unit price of the ...
- poj 3013 Big Christmas Tree Djistra
Big Christmas Tree 题意:图中每个节点和边都有权值,图中找出一颗树,树根为1使得 Σ(树中的节点到树根的距离)*(以该节点为子树的所有节点的权值之和) 结果最小: 分析:直接求出每个 ...
- HDU 4871 Shortest-path tree 最短路 + 树分治
题意: 输入一个带权的无向连通图 定义以顶点\(u\)为根的最短路生成树为: 树上任何点\(v\)到\(u\)的距离都是原图最短的,如果有多条最短路,取字典序最小的那条. 然后询问生成树上恰好包含\( ...
随机推荐
- prometeus, grafana部署以及监控mysql
什么是普罗米修斯? Prometheus是一个最初在SoundCloud上构建的开源系统监视和警报工具包 .自2012年成立以来,许多公司和组织都采用了Prometheus,该项目拥有一个非常活跃的开 ...
- C#使用ILGenerator动态生成函数
游戏服务器里面总是有一大堆的配置文件需要读取, 而且这些配置文件的读取: * 要不然做成弱类型的, 就是一堆字符串或者数字, 不能看出来错误(需要重新检测一次) * 要不然做成强类型的, 每种类型都需 ...
- Mysql数据库中索引的概念总结
1.索引的目的是什么 1.快速访问数据表中的特定信息,提高检索速度 2.创建唯一性索引,保证数据库表中每一行数据的唯一性. 3.加速表和表之间的连接 4.使用分组和排序子句进行数据检索时,可以显著减少 ...
- Integer的NPE问题
- 【转】Word之表格、图片的题注(抬头)自动编号
问:word中的表格怎么自动插入题注(即表头的编号自动编号)? 答: 1首先搞清楚自动编号的意思.自动插入题注的意思是,在你在word中新建或者复制一个word表格的时候,表头的编号就自动生成了,而不 ...
- 移动端和PC端页面常用的弹出层
我们在页面的时候,很多时候用到了弹出层,消息提醒,确认框等等,统一样式的弹出框可以使页面更加优美.在此,我整理一下我们项目的移动端和PC端页面常用的弹出层. 一.移动端 我们需在页面引入弹出框的样式和 ...
- asp.net core Api配置swagger
这个很简单的一篇文章用来记录以下使用swagger的过程,以后有用. 1.nuget 下载install-package Swashbuckle.AspNetCore 2.startup里面confi ...
- [转帖]system()、exec()、fork()三个与进程有关的函数的比较
system().exec().fork()三个与进程有关的函数的比较 https://www.cnblogs.com/qingergege/p/6601807.html 启动新进程(system函数 ...
- Mission Impossible 6
题目:Mission Impossible 6 题目链接:http://hihocoder.com/problemset/problem/1228 题目大意: 大概就是让我们写一个代码模拟文本编辑器的 ...
- spring AOP源码分析(二)
现在,我们将对代理对象的生成过程进行分析. 在springAOP源码分析(一)的例子中,将会生成哪些对象呢? 可以看到将会生成六个对象,对应的beanName分别是: userDao:目标对象 log ...