poj 3013 Big Christmas Tree (最短路径Dijsktra) -- 第一次用优先队列写Dijsktra
http://poj.org/problem?id=3013
| Time Limit: 3000MS | Memory Limit: 131072K | |
| Total Submissions: 19009 | Accepted: 4048 |
Description
Christmas is coming to KCM city. Suby the loyal civilian in KCM city is preparing a big neat Christmas tree. The simple structure of the tree is shown in right picture.
The tree can be represented as a collection of numbered nodes and some edges. The nodes are numbered 1 through n. The root is always numbered 1. Every node in the tree has its weight. The weights can be different from each other. Also the shape of every available edge between two nodes is different, so the unit price of each edge is different. Because of a technical difficulty, price of an edge will be (sum of weights of all descendant nodes) × (unit price of the edge).
Suby wants to minimize the cost of whole tree among all possible choices. Also he wants to use all nodes because he wants a large tree. So he decided to ask you for helping solve this task by find the minimum cost.
Input
The input consists of T test cases. The number of test cases T is given in the first line of the input file. Each test case consists of several lines. Two numbers v, e (0 ≤ v, e ≤ 50000) are given in the first line of each test case. On the next line, v positive integers wi indicating the weights of v nodes are given in one line. On the following e lines, each line contain three positive integers a, b, c indicating the edge which is able to connect two nodes a and b, and unit price c.
All numbers in input are less than 216.
Output
For each test case, output an integer indicating the minimum possible cost for the tree in one line. If there is no way to build a Christmas tree, print “No Answer” in one line.
Sample Input
2
2 1
1 1
1 2 15
7 7
200 10 20 30 40 50 60
1 2 1
2 3 3
2 4 2
3 5 4
3 7 2
3 6 3
1 5 9
Sample Output
15
1210
Source
/**
Judge Status:Accepted Memory:2880K
Time:610MS Language:G++
Code Length:2062B Author:cj
*/ #include<iostream>
#include<queue>
#include<stdio.h>
#include<string.h>
#include<stdlib.h> #define N 50005
#define INF 1000000000000
using namespace std; struct Edge //保存边的结构体
{
int to; //边连接的另外个点
int next; //下一个搜索的节点
int w; //节点的权值
}edge[N<<]; struct Nod
{
int u; //进入队列中的点
__int64 dis; //到该点的距离
}now,temp; bool operator< (Nod a,Nod b) //优先队列重载'<'运算符
{
return a.dis>b.dis; //小到大
} int weight[N],head[N],visit[N];
__int64 dis[N]; //第一点到各点的最小距离 void init(int n) //初始化
{
int i;
for(i=;i<=n;i++)
{
visit[i] = ;
dis[i] = INF;
head[i] = -;
}
} void Dijkstra(int s)
{
int i,v;
dis[s] = ;
priority_queue<Nod> p_q; //优先队列 你懂的
temp.dis = ;
temp.u = s;
p_q.push(temp);
while(!p_q.empty())
{
temp = p_q.top(); //每次去的都是距离起点最小的点(优先队列的性质)
p_q.pop();
if(visit[temp.u]) continue;
visit[temp.u] = ;
for(i=head[temp.u];i!=-;i=edge[i].next) //每次遍历跟这个点有连接的所有点
{
v = edge[i].to;
if(!visit[v]&&dis[v]>dis[temp.u]+edge[i].w)
{
dis[v] = dis[temp.u]+edge[i].w; //距离更新
now.u = v;
now.dis = dis[v];
p_q.push(now); //压入队列
}
}
}
} int main()
{
int t;
scanf("%d",&t);
while(t--)
{
int m,n;
scanf("%d%d",&n,&m);
int i;
for(i=;i<n;i++) scanf("%d",weight+i);
int id = ;
init(n);
int a,b,c;
for(i=;i<m;i++)
{
scanf("%d%d%d",&a,&b,&c);
a--,b--;
edge[id].to = b;
edge[id].w = c;
edge[id].next = head[a];
head[a] = id++; //将边 a --> b保存 edge[id].to = a;
edge[id].w = c;
edge[id].next = head[b];
head[b] = id++; //将边 b --> a保存
}
Dijkstra();
__int64 res = ;
for(i=;i<n;i++)
{
if(dis[i]==INF) break;
res += dis[i]*weight[i];
}
if(i<n) puts("No Answer");
else printf("%I64d\n",res);
}
return ;
}
poj 3013 Big Christmas Tree (最短路径Dijsktra) -- 第一次用优先队列写Dijsktra的更多相关文章
- POJ 3013 Big Christmas Tree(最短Dijkstra+优先级队列优化,SPFA)
POJ 3013 Big Christmas Tree(最短路Dijkstra+优先队列优化,SPFA) ACM 题目地址:POJ 3013 题意: 圣诞树是由n个节点和e个边构成的,点编号1-n. ...
- poj 3013 Big Christmas Tree
Big Christmas Tree Time Limit: 3000MS Memory Limit: 131072K Total Submissions: 20974 Accepted: 4 ...
- poj 3013 Big Christmas Tree Djistra
Big Christmas Tree 题意:图中每个节点和边都有权值,图中找出一颗树,树根为1使得 Σ(树中的节点到树根的距离)*(以该节点为子树的所有节点的权值之和) 结果最小: 分析:直接求出每个 ...
- poj 3013 Big Christmas Tree (dij+优先级队列优化 求最短)
模板 意甲冠军:给你一个图,1始终根,每一方都有单价值,每个点都有权重新. 每个边缘的价格值 = sum(后继结点重)*单价方值. 最低价格要求树值,它构成了一棵树n-1条边的最小价值. 算法: 1. ...
- SPFA/Dijkstra POJ 3013 Big Christmas Tree
题目传送门 题意:找一棵树使得造价最少,造价为每个点的子节点造价和*边的造价和 分析:最短路跑出1根节点到每个点的最短边权值,然后每个点的权值*最短边距和就是答案,注意INF开足够大,n<=1特 ...
- POJ Big Christmas Tree(最短的基础)
Big Christmas Tree 题目分析: 叫你构造一颗圣诞树,使得 (sum of weights of all descendant nodes) × (unit price of the ...
- POJ3013 Big Christmas Tree[转换 最短路]
Big Christmas Tree Time Limit: 3000MS Memory Limit: 131072K Total Submissions: 23387 Accepted: 5 ...
- POJ 3013 SPFA算法,邻接表的使用
Big Christmas Tree Time Limit: 3000MS Memory Limit: 131072K Total Submissions: 19029 Accepted: 4 ...
- Big Christmas Tree(poj-3013)最短路
Big Christmas Tree Time Limit: 3000MS Memory Limit: 131072K Total Submissions: 25823 Accepted: 5 ...
随机推荐
- CF 19D - Points 线段树套平衡树
题目在这: 给出三种操作: 1.增加点(x,y) 2.删除点(x,y) 3.询问在点(x,y)右上方的点,如果有相同,输出最左边的,如果还有相同,输出最低的那个点 分析: 线段树套平衡树. 我们先离散 ...
- HTTP - 持久连接
Web 客户端经常会打开到同一个站点的连接.比如,一个 Web 页面上的大部分内嵌图片通常都是来自同一个 Web 站点,而且相当一部分指向其他对象的超链接通常都指向同一个站点.因此,初始化了对某服务器 ...
- IIS服务器应用程序不可用的解决办法
转载:http://www.cnblogs.com/caicainiao/archive/2010/11/29/1891085.html 这个问题见了好几次,在.net下 Microsoft visu ...
- 【分享】.Net有哪些大型项目、大型网站的案例?
.Net开发的部分知名网站案例:http://www.godaddy.com 全球最大域名注册商http://www.ips.com 环迅支付,国内最早的在线支付平台http://www.icbc ...
- 第四十三篇、利用NSProxy解决NSTimer内存泄漏问题
问题描述: 用NSTimer来实现每隔一定时间执行制定的任务,例如最常见的广告轮播图.如果我们在 timerWithTimeInterval:1 target:self 中指定target为当前控制器 ...
- C# WinForm打开IE浏览器并访问网址
C# WinForm 打开浏览器并访问网址代码: System.Diagnostics.Process.Start("iexplore.exe", "http://kel ...
- (转)操作型数据库的春天:MongoDB 1.5亿美元融资背后的故事
大部分融资都要耗时数月,但非关系式数据库MongoDB仅用3周时间就完成了1.5亿美元的融资.为什么这个进程会这么快,MongoDB CEO Max Schireson在接受采访时说,这是因为投资者看 ...
- 最全 Adobe 系列产品 CS6版本 序列号/注册码
最全 Adobe 系列产品 CS6版本 序列号/注册码: 1.Adobe Photoshop CS6 Extended 序列号/注册码 2.Adobe After Effects CS6 序列号/注册 ...
- JAVA_SE复习(OOP2)
面向对象编程(二) 一.static 关键字 静态属性 1.不能覆盖静态方法.要被覆盖的方法必须是非静态的.在继承链中具有相同方法名的两个静态方法是两个互相独立的类方法.调用子类的静态方法只是将父类的 ...
- 《APUE》第七章笔记
这一章主要是要解决这么几个问题: 当执行程序时,main函数是如何被调用的? main函数的原型是: int main(int argc, char *argv[]); 其中argc是命令个数,arg ...