Big Christmas Tree

题目分析:

叫你构造一颗圣诞树,使得 (sum of weights of all descendant nodes) × (unit price of the edge)尽量的小。转换后就是求根节点到每一个节点的距离最短,也就是最短路。

生成树可能会超时。我没试过。然后,求解最短路要用优化的解法不然会超时。最后的答案就是:sum = w[1] * dist[1] + w[2] * dist[2] +
..... w[n] * dist[n].能够自己推推例子就知道了。

本来是一道简单的最短路。结果由于没有清空,浪费了一个早上的时间。,,,T_T

以后,一定要记住啊!!!

血的教训!!

!!

#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
#include <cstdio>
#include <cstring>
using namespace std; typedef long long LL;
const int MAXN = 50000 + 100; struct Edge{
int from,to;
LL c;
Edge(){};
Edge(int _f,int _t,LL _c)
:from(_f),to(_t),c(_c){};
}; vector<Edge> edges;
vector<int> G[MAXN];
LL weight[MAXN],dist[MAXN];
bool vst[MAXN];
int numV,numE; //清空
void init(){
edges.clear();
for(int i = 0;i <= numV;++i){
G[i].clear();
}
}
void addEdge(int x,int y,LL c){
edges.push_back(Edge(x,y,c));
int sz = edges.size();
G[x].push_back(sz - 1);
} //松弛操作
bool relax(int u,int v,LL c){
if((dist[u] == -1) || (dist[u] > dist[v] + c)){
dist[u] = dist[v] + c;
return true;
}
return false;
} //求解最短路
void spfa(LL src){
int i,u,k;
queue<int> Q;
for(i = 0;i <= numV;++i){
vst[i] = 0;
dist[i] = -1;
} Q.push(src);
dist[src] = 0; while(!Q.empty()){
u = Q.front();
Q.pop();
vst[u] = 0;
for(i = 0;i < (int)G[u].size();++i){
k = G[u][i];
Edge& e = edges[k];
if(relax(e.to,u,e.c) && !vst[e.to]){
vst[e.to] = 1;
Q.push(e.to);
}
}
}
} int main()
{
int T;
scanf("%d",&T);
while(T--){
int x,y;
LL c;
scanf("%d%d",&numV,&numE);
init();
for(int i = 1;i <= numV;++i){
cin >> weight[i];
} for(int i = 0;i < numE;++i){
scanf("%d%d%I64d",&x,&y,&c);
addEdge(x,y,c);
addEdge(y,x,c);
} spfa(1);
LL sum = 0;
bool flag = false;
for(int i = 1;i <= numV;++i){
if(dist[i] == -1){
flag = true;
break;
}
sum += dist[i] * weight[i];
}
if(flag)
puts("No Answer");
else
printf("%I64d\n",sum);
}
return 0;
}



版权声明:本文博客原创文章,博客,未经同意,不得转载。

POJ Big Christmas Tree(最短的基础)的更多相关文章

  1. POJ 3013 Big Christmas Tree(最短Dijkstra+优先级队列优化,SPFA)

    POJ 3013 Big Christmas Tree(最短路Dijkstra+优先队列优化,SPFA) ACM 题目地址:POJ 3013 题意:  圣诞树是由n个节点和e个边构成的,点编号1-n. ...

  2. poj 3013 Big Christmas Tree (最短路径Dijsktra) -- 第一次用优先队列写Dijsktra

    http://poj.org/problem?id=3013 Big Christmas Tree Time Limit: 3000MS   Memory Limit: 131072K Total S ...

  3. poj 3013 Big Christmas Tree

    Big Christmas Tree Time Limit: 3000MS   Memory Limit: 131072K Total Submissions: 20974   Accepted: 4 ...

  4. poj 3013 Big Christmas Tree Djistra

    Big Christmas Tree 题意:图中每个节点和边都有权值,图中找出一颗树,树根为1使得 Σ(树中的节点到树根的距离)*(以该节点为子树的所有节点的权值之和) 结果最小: 分析:直接求出每个 ...

  5. POJ3013 Big Christmas Tree[转换 最短路]

    Big Christmas Tree Time Limit: 3000MS   Memory Limit: 131072K Total Submissions: 23387   Accepted: 5 ...

  6. Big Christmas Tree(poj-3013)最短路

    Big Christmas Tree Time Limit: 3000MS   Memory Limit: 131072K Total Submissions: 25823   Accepted: 5 ...

  7. POJ.3321 Apple Tree ( DFS序 线段树 单点更新 区间求和)

    POJ.3321 Apple Tree ( DFS序 线段树 单点更新 区间求和) 题意分析 卡卡屋前有一株苹果树,每年秋天,树上长了许多苹果.卡卡很喜欢苹果.树上有N个节点,卡卡给他们编号1到N,根 ...

  8. POJ - 3321 Apple Tree (线段树 + 建树 + 思维转换)

    id=10486" target="_blank" style="color:blue; text-decoration:none">POJ - ...

  9. poj 3013 Big Christmas Tree (dij+优先级队列优化 求最短)

    模板 意甲冠军:给你一个图,1始终根,每一方都有单价值,每个点都有权重新. 每个边缘的价格值 = sum(后继结点重)*单价方值. 最低价格要求树值,它构成了一棵树n-1条边的最小价值. 算法: 1. ...

随机推荐

  1. pydev-python 链接mysql数据库(mac系统)

    1.首先,实现了命令行可以运行mysql          非常清楚了,直接引用过来,多谢哈.引用:http://www.lihui.info/mac-pydev-mysqldb/           ...

  2. ImageView建立selector在录音中遇到的小问题及解决方案

    随着两张照片做了一个selector,采用ImageView的src要么background采用selector当点击,总不会出现点击效果,这就是为什么?经过一番折腾,后来发现"揭秘&quo ...

  3. Ajax 实现无刷新页面

    注意:如本文所用,在前面的文章库的数目可以在源代码中找到,我将指示在文本,其中链路,为了缩短制品的长度,阅读由此带来的不便.乞求被原谅. 评论文章 Ajax 实现无刷新页面.其原理.代码库.代码. 这 ...

  4. OpenGL模板 Mac Cmake OpenGL(Glut) Template

    自己经常使用的一些功能做一个模板,有灯光效果,你可以用鼠标放大,围绕所述旋转坐标系的原点 watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvcHlhbmcxOT ...

  5. Writing your first Django app, part 1(转)

    Let’s learn by example. Throughout this tutorial, we’ll walk you through the creation of a basic pol ...

  6. Hybrid----U采用IWebView演出PDF和其他文件

    App如果你需要显示pdf.word档,在这个时候有没有其他控制,比UIWebView更适合,这是高度抽象的技术细节,可以非常easy采用 UIWebView打开文件类型列表 watermark/2/ ...

  7. HTML5游戏开发引擎Pixi.js完全入门手册(一)框架简介及框架结构分析,作者思路剖析

    前言: 最近无聊在淘宝弄了个小店,打算做一个兼职.遇到一个客户,要我帮忙拷贝一个html5游戏.. 我这人有一个习惯,拿到自己没见过的东西.都会去研究一番.去网上查了下发现,资料都是英文版.感觉极度不 ...

  8. 【 D3.js 入门系列 --- 9.1 】 生产饼图

    我个人的博客: www.ourd3js.com csdn博客为: blog.csdn.net/lzhlzz 转载请注明出处.谢谢. 这一节用 Layout 做一个饼状图.第9节中说过, Layout ...

  9. 一个数据表对象(NSManagedObject)加入排序

    eg:数据库表对象 @interface Meditation : NSManagedObject @property (nonatomic, retain) NSString * order;//用 ...

  10. typedef和define具体的具体差异

      1) #define这是一个预处理指令,简单的更换当预处理程序.不检查的正确性,仍不能正常关机进入的意思,那里只是已被展开时编译源代码会发现可能的错误和错误. 例如: #define PI 3.1 ...