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

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

    于是用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. .NET Core 中正确使用 HttpClient 的姿势

    为了更方便在服务端调用 HTTP 请求,微软在 .NET Framework 4.x 的时候引入了 HttpClient.但 HttpClient 有很多严重问题,一直饱受诟病,比如 InfoQ 的这 ...

  2. Pytest+Allure定制报告

    前言: 最近在研究接口自动化的框架,好的测试报告在整个测试框架起到至关重要的部分.终于被我发现一个超好用的报告框架,不仅报告美观,而且方便CI集成. 就是它,就是它:Allure Test Repor ...

  3. 实现多个标签页之间通信的几种方法(sharedworker)

      效果图.gif prologue 之前在网上看到一个面试题:如何实现浏览器中多个标签页之间的通信.我目前想到的方法有三种:使用websocket协议.通过localstorage.以及使用html ...

  4. Python_迭代器_35

    迭代器 # l = [1,2,3]# 索引# 循环 for# for i in l:# i## for k in dic:# pass #可以被for循环的# list# dic# str# set# ...

  5. H5 表单标签

    33-表单标签3 列表数据 注意点: 1.下拉列表不能输入内容, 但是可以直接在列表中选择内容 2.可以通过给option标签添加一个selected属性来指定列表的默认值 3.可以通过给option ...

  6. echarts图片保存

    一.js: function updateChart(versionList,rateList) { option = { title: { text: '拖动频次' }, tooltip : { t ...

  7. tomcat one connection one thread one request one thread

    java - What is the difference between thread per connection vs thread per request? - Stack Overflow ...

  8. windows 内建环境变量

    PS C:\Windows> ls env: Name Value ---- ----- _NT_SYMBOL_PATH srv*C:\Users\vv\Documents\symbols AL ...

  9. springboot 如何操作redis

    1.首先应该引入 依赖 <dependency> <groupId>org.springframework.boot</groupId> <artifactI ...

  10. Handler主线程子线程之间的互相通信

    Handler主线程子线程之间的互相通信 package com.wyl.dansnote; import android.app.Activity; import android.os.Bundle ...