Big Christmas Tree

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

分析:直接求出每个节点到树根的最短距离距离,之后乘上自身节点的权值求和即可;

ps:注意特判v = 0;这时cnt = 1 > v;

Djistra + priority_queue

//Accepted    2804K    141MS
#include <cstdio>
#include <cstring>
#include <utility>
#include <queue>
#include <vector>
using namespace std;
#define rep0(i,l,r) for(int i = (l);i < (r);i++)
#define rep1(i,l,r) for(int i = (l);i <= (r);i++)
#define rep_0(i,r,l) for(int i = (r);i > (l);i--)
#define rep_1(i,r,l) for(int i = (r);i >= (l);i--)
#define MS0(a) memset(a,0,sizeof(a))
#define MS1(a) memset(a,-1,sizeof(a))
#define inf 1LL<<40
template<typename T>
void read1(T &m)
{
T x=,f=;char ch=getchar();
while(ch<''||ch>''){if(ch=='-')f=-;ch=getchar();}
while(ch>=''&&ch<=''){x=x*+ch-'';ch=getchar();}
m = x*f;
}
template<typename T>
void read2(T &a,T &b){read1(a);read1(b);}
template<typename T>
void read3(T &a,T &b,T &c){read1(a);read1(b);read1(c);}
template<typename T>
void out(T a)
{
if(a>) out(a/);
putchar(a%+'');
}
const int N = ;
typedef __int64 ll;
typedef pair<__int64,int> lli;//距离,编号
#define A first
#define B second
priority_queue<lli , vector<lli> , greater<lli> > q;
int v,val[N];
int head[N<<],tot;
struct edge{
int to,w,Next;
}e[N<<];
void ins(int a,int b,int w = )
{
e[++tot].Next = head[a];
e[tot].to = b;
e[tot].w = w;
head[a] = tot;
}
ll d[N];bool vis[N];
ll Djistra()
{
ll ans = ,cnt = ;
d[] = ;
q.push(lli{d[],});
while(!q.empty()){
lli t = q.top();
q.pop();
int u = t.B;
if(vis[u]) continue;
cnt++;vis[u] = true;
ans += d[u]*val[u];
for(int id = head[u];id;id = e[id].Next){
int v = e[id].to,cost = e[id].w;
if(d[v] > d[u] + cost){
d[v] = d[u] + cost;
q.push(lli{d[v],v}); }
}
}
if(cnt < v) return -;
return ans;
}
int main()
{
int T,e,kase = ;
read1(T);
while(T--){
read2(v,e);
rep1(i,,v)
read1(val[i]),d[i] = inf,vis[i] = false;
MS0(head);tot = ;
rep0(i,,e){
int a,b,w;
read3(a,b,w);
ins(a,b,w);ins(b,a,w);
}
if(v <= )out();
else{
while(!q.empty()) q.pop();
ll ret = Djistra();
if(ret == -) printf("No Answer");
else out(ret);
}
puts("");
}
return ;
}

Djistra + heap

ps:heap中的最后的位置赋值要放在最后,不好直接在break中,因为可能是出边界了,而没有赋到需要的值;

//4116K    125MS    G++    2869B
#include <cstdio>
#include <cstring>
#include <utility>
#include <queue>
#include <vector>
using namespace std;
#define rep0(i,l,r) for(int i = (l);i < (r);i++)
#define rep1(i,l,r) for(int i = (l);i <= (r);i++)
#define rep_0(i,r,l) for(int i = (r);i > (l);i--)
#define rep_1(i,r,l) for(int i = (r);i >= (l);i--)
#define MS0(a) memset(a,0,sizeof(a))
#define MS1(a) memset(a,-1,sizeof(a))
#define inf 1LL<<40
template<typename T>
void read1(T &m)
{
T x=,f=;char ch=getchar();
while(ch<''||ch>''){if(ch=='-')f=-;ch=getchar();}
while(ch>=''&&ch<=''){x=x*+ch-'';ch=getchar();}
m = x*f;
}
template<typename T>
void read2(T &a,T &b){read1(a);read1(b);}
template<typename T>
void read3(T &a,T &b,T &c){read1(a);read1(b);read1(c);}
template<typename T>
void out(T a)
{
if(a>) out(a/);
putchar(a%+'');
}
const int N = ;
typedef __int64 ll;
typedef pair<__int64,int> lli;
#define A first
#define B second
int v,val[N];
int head[N<<],tot;
struct edge{
int to,w,Next;
}e[N<<];
void ins(int a,int b,int w = )
{
e[++tot].Next = head[a];
e[tot].to = b;
e[tot].w = w;
head[a] = tot;
}
ll d[N];bool vis[N];
lli heap[N],q[N];int _cnt;
bool comp(lli x,lli y)
{
return x.A < y.A;//最小堆
}
inline void push(const lli x)
{
heap[++_cnt] = x;
int son,fa;
for(son = _cnt,fa = son>>;fa >= ;son = fa,fa = son>>){
if(comp(x,heap[fa]))
heap[son] = heap[fa];
else break;
}
heap[son] = x;
}
inline lli top()
{
return heap[];
}
inline void pop()
{
lli tmp = heap[] = heap[_cnt--];
int son ,fa;
for(fa = ,son = fa<<;son <= _cnt;fa = son,son = fa<<){
if(son < _cnt && comp(heap[son|],heap[son]))
son++;
if(comp(tmp,heap[son]))break;
else heap[fa] = heap[son];
}
heap[fa] = tmp;
}
ll Djistra()
{
ll ans = ,cnt = ;
d[] = ;
push(lli{d[],});
while(_cnt){
lli t = top();
pop();
int u = t.B;
if(vis[u]) continue;
cnt++;vis[u] = true;
ans += d[u]*val[u];
for(int id = head[u];id;id = e[id].Next){
int v = e[id].to,cost = e[id].w;
if(d[v] > d[u] + cost){
d[v] = d[u] + cost;
push(lli{d[v],v});
}
}
}
if(cnt < v) return -;
return ans;
}
int main()
{
int T,e,kase = ;
read1(T);
while(T--){
read2(v,e);
rep1(i,,v)
read1(val[i]),d[i] = inf,vis[i] = false;
MS0(head);tot = ;
rep0(i,,e){
int a,b,w;
read3(a,b,w);
ins(a,b,w);ins(b,a,w);
}
if(v <= )out();
else{
_cnt = ;
ll ret = Djistra();
if(ret == -) printf("No Answer");
else out(ret);
}
puts("");
}
return ;
}

poj 3013 Big Christmas Tree Djistra的更多相关文章

  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 (dij+优先级队列优化 求最短)

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

  5. SPFA/Dijkstra POJ 3013 Big Christmas Tree

    题目传送门 题意:找一棵树使得造价最少,造价为每个点的子节点造价和*边的造价和 分析:最短路跑出1根节点到每个点的最短边权值,然后每个点的权值*最短边距和就是答案,注意INF开足够大,n<=1特 ...

  6. POJ Big Christmas Tree(最短的基础)

    Big Christmas Tree 题目分析: 叫你构造一颗圣诞树,使得 (sum of weights of all descendant nodes) × (unit price of the ...

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

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

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

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

  9. 【POJ 2486】 Apple Tree(树型dp)

    [POJ 2486] Apple Tree(树型dp) Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 8981   Acce ...

随机推荐

  1. ExtJs Tree加载选项卡,选项卡加载页面不用iframe

    点击树节点,自动加载选项卡对应的页面, 效果图: JS Code: 一.创建TreeStore var store = Ext.create('Ext.data.TreeStore', { root: ...

  2. Microsoft Office Visio Professional 2007密钥

    Microsoft Office Visio Professional 2007 产品密钥: W2JJW-4KYDP-2YMKW-FX36H-QYVD8

  3. Events

    Events The idea behind Events is the ability to send data, as parameters, to interested Listeners an ...

  4. CSS Selector (part 1)

    Selector概述 示例: strong { color: red; } 解释: 这是一个完整 css 规则(标签选择器).strong 叫做选择器,它选择该规则将被应用到 DOM 的那个元素上去. ...

  5. C#如何加载程序运行目录外的程序集

    我们的应用程序部署的时候,目录结构一般不会只有运行程序的目录这一个,我们可能在运行目录下建子目录,也可能使用System32目录,也可能使用其它第三方的程序集..Net程序集 首先会在GAC中搜索相应 ...

  6. Quartus II 11.0 开始使用

    一.Altera Quartus II 11.0套件介绍 所谓巧妇难为无米之炊,再强的软硬件功底,再多的思维创造力,没有软件的平台,也只是徒劳.因此,一切创造的平台——Quartus II 软件安装, ...

  7. CENTOS install summary

    1 centos6.5 before version : first :ultraISO write into u disk,second: copy source iso file into u d ...

  8. c# 远程监控(2) 摄像头调研及模拟

    经过N多调研,最终选择了OpenCV(Emgu CV) ** 至于DirectShow, OpenCV等等其他大家可以百度,在这里我就不再赘述 环境:vs2010 vs2012 vs2013均可 Op ...

  9. Android之 Fragment

    什么是Fragment: Android是在Android 3.0 (API level 11)开始引入Fragment的. 可以把Fragment想成Activity中的模块,这个模块有自己的布局, ...

  10. WebClient.UploadValues Post中文乱码的解决方法

    //using (System.Net.WebClient wc = new System.Net.WebClient()) //{ // wc.Encoding = Encoding.GetEnco ...