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. struts1吊牌&lt;logic:iterate&gt;

    <logic:iterate>主要用于处理网页上的输出集合,集合是其中一般下列之一: 1. java对象的数组 2. ArrayList.Vector.HashMap等 具体使用方法请參考 ...

  2. dom 规划(html和xml)

    html dom与xml dom关联: 什么是 DOM? DOM 是 W3C(万维网联盟)的标准. DOM 定义了訪问 HTML 和 XML 文档的标准: "W3C 文档对象模型 (DOM) ...

  3. ASP.NET MVC (Razor)开发

    ASP.NET MVC (Razor)开发 过去我们使用过一些周报工具来完成项目组或部门的周报填写与考核工作,但多少有些不理想,要么功能太过简单,要么功能特别繁杂,不接地气,使用不便. 后来我们就考虑 ...

  4. JavaScript面向对象旅程(下)

    JavaScript面向对象旅程 剪不断,理还乱,是离愁. 前面已经提到过新语言开发的两个步骤,分别是:一.定义基本的数据类型,完善结构化编程语言的设计:二.为函数类型绑定this的概念,好在对象的方 ...

  5. 正则获取URL参数

    一 获取指定URL参数 function getUrlParams(name) { var reg = new RegExp("(^|&)" + name + " ...

  6. [LeetCode] 036. Valid Sudoku (Easy) (C++)

    指数:[LeetCode] Leetcode 解决问题的指数 (C++/Java/Python/Sql) Github: https://github.com/illuz/leetcode 036. ...

  7. org.springframework.dao.InvalidDataAccessApiUsageException: detached entity passed to persist: sys.entity.Role; nested exception is org.hibernate.PersistentObjectException: 的解决方案

    1.错误信息 org.springframework.dao.InvalidDataAccessApiUsageException: detached entity passed to persist ...

  8. asp.net学习之SqlDataSource

    原文:asp.net学习之SqlDataSource 通过 SqlDataSource 控件,可以使用 Web 服务器控件访问位于关系数据库中的数据.其中可以包括 Microsoft SQL Serv ...

  9. do{}while(0)宏的作用的定义

    看到开放源代码,宏定义经常这样用 #define some() do { do_somt_thing(); } while (0) 为什么这样用? 能够试一下.假如一个普通宏定义 #define so ...

  10. Java串口通信详细解释

    前言 说到开源.恐怕非常少有人不挑大指称赞. 学生通过开源码学到了知识,程序猿通过开源类库获得了别人的成功经验及可以按时完毕手头的project,商家通过开源软件赚到了钱……,总之是皆大欢喜. 然而开 ...