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\)的距离都是原图最短的,如果有多条最短路,取字典序最小的那条. 然后询问生成树上恰好包含\( ...
随机推荐
- iOS开发简记(8):数据持久化
数据持久化,也就是把数据保存到磁盘,以后可以再读取出来使用(也可以再次更改或删除).很多场景需要数据持久化,比如为了减轻服务器的访问与存储压力,客户端需要在本地做一些数据持久化的工作. iOS的数据持 ...
- .net core实践系列之短信服务-目录
前言 经过两周多的业余时间,终于把该系列的文章写完了.第一次写系列,可能部分关键点并没有覆盖到,如果有疑问的朋友可以随时反馈给我.另外也感谢在我发布文章时给予我方案建议与反馈源码BUG的朋友们.下面是 ...
- ASP.NET MVC5+EF6+EasyUI 后台管理系统(91)-EF 连接 MySql
前言 虽然系统目前只支持MSSQL版本,但是很多朋友公司技术规定必须使用MySql,下面我们就来使用EF连接MySQL吧! (1)安装MYSQL环境 1.下载安装MYSQL数据,这里我们安装phpSt ...
- Python学习第三篇——逻辑判定
request_foods=["tomato","beaf","milk"] for elements in request_foods: ...
- Feel Good POJ - 2796 (前缀和+单调栈)(详解)
Bill is developing a new mathematical theory for human emotions. His recent investigations are dedic ...
- 快速失败/报错机制 - fail-fast
一.快速报错机制(fail-fast) 这是<Java编程思想>中关于快速报错机制的描述 Java容器有一种保护机制,能够防止多个进程同时修改同一个容器的内容.如果在你迭代遍历容器的过程中 ...
- phantomjs 了解
转自:http://www.cnblogs.com/lei0213/ PhantomJS是一个无界面的,可脚本编程的WebKit浏览器引擎.它原生支持多种web 标准:DOM 操作,CSS选择器,JS ...
- python 3.6.1 安装scrapy踩坑之旅
系统环境:win10 64位系统安装 python基础环境配置不做过多的介绍 window环境安装scrapy需要依赖pywin32,下载对应python版本的exe文件执行安装,下载的pywin32 ...
- 初步了解HTTP
HTTP简介: HTTP:HyperText Transfer Protocol 超文本传输协议,是因特网上使用最为广泛的一种网络传输议,是用于从万维网(www :world wide web)服 ...
- Java 获取当前日期的四种方法
//1 通过Date类来获取当前时间,通过SimpleDateFormat来设置时间格式 SimpleDateFormat dateFormat = new SimpleDateFormat(&quo ...