POJ-3013: Big Christmas Tree

题目链接:POJ:3013


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

题意:哈理工1419是本题的中文题意。

分析: 第二个样例
=40∗4+60∗2+50∗3+(40+60+50+20)∗3+30∗2+(10+20+30+40+50+60)∗1=40*4+60*2+50*3+(40+60+50+20)*3+30*2+(10+20+30+40+50+60)*1
=40∗(4+3+1)+60∗(2+3+1)+50∗(3+3+1)+20∗(3+1)+30∗(2+1)∗10∗1=40*(4+3+1)+60*(2+3+1)+50*(3+3+1)+20*(3+1)+30*(2+1)*10*1
得到:最小的答案=每个点*该点到根结点的最短路

注意: INF开到0x3f3f3f3f3f3f3f3f


  • dij
#include<queue>
#include<stdio.h>
#include<vector>
#include<string.h>
#define INF 0x3f3f3f3f3f3f3f3f
using namespace std;
typedef long long LL;
const int maxn=50005;
int vv[maxn];
int pos[maxn*2],vis[maxn],num;
LL dis[maxn],ans;
struct node1
{int en,val,next;
}a[maxn*2];
struct node
{int id,val;node(int idd=0,int vall=0){id=idd;val=vall;}friend bool operator <(node x,node y){return x.val>y.val;}
};
void add(int st,int en,int val)
{a[num].en=en;a[num].val=val;a[num].next=pos[st];pos[st]=num++;
}
void dij()
{dis[1]=0;priority_queue<node>q;q.push(node(1,dis[1]));while(!q.empty()){node now=q.top();q.pop();if(vis[now.id]) continue;vis[now.id]=1;for(int i=pos[now.id];i!=-1;i=a[i].next){if(!vis[a[i].en]&&dis[a[i].en]>dis[now.id]+a[i].val){dis[a[i].en]=dis[now.id]+a[i].val;q.push(node(a[i].en,dis[a[i].en]));}}}
}
void init()
{ans=0;num=0;memset(vis,0,sizeof(vis));memset(dis,0x3f,sizeof(dis));memset(pos,-1,sizeof(pos));
}
int main()
{int t;int v,e;int x,y,z;scanf("%d",&t);while(t--){init();scanf("%d%d",&v,&e);for(int i=1;i<=v;i++)scanf("%d",&vv[i]);while(e--){scanf("%d%d%d",&x,&y,&z);add(x,y,z);add(y,x,z);}dij();int flag=1;for(int i=1;i<=v;i++){if(dis[i]==INF){flag=0;break;}ans+=dis[i]*vv[i];}if(flag)printf("%lld\n",ans);elseprintf("No Answer\n");}return 0;
}
  • spfa
#include<stdio.h>
#include<string.h>
#include<vector>
#include<queue>
#define INF 0x3f3f3f3f3f3f3f3f
using namespace std;
typedef long long LL;
const int maxn=50005;
int v,e;
int vv[maxn];
int cnt[maxn],vis[maxn];
struct node
{int en,val,next;
}a[maxn*2];
int pos[maxn],num;
LL dis[maxn],ans;
void add(int st,int en,int val)
{a[num].en=en;a[num].val=val;a[num].next=pos[st];pos[st]=num++;
}
void init()
{num=0;ans=0;memset(pos,-1,sizeof(pos));
}
void spfa()
{queue<int>q;for(int i=1;i<=v;i++)dis[i]=INF,vis[i]=0,cnt[i]=0;dis[1]=0;cnt[1]++;q.push(1);while(!q.empty()){int u=q.front();q.pop();vis[u]=0;for(int i=pos[u];i!=-1;i=a[i].next){int w=a[i].en;int val=a[i].val;if(dis[w]>dis[u]+val){dis[w]=dis[u]+val;if(!vis[w]){vis[w]=1;cnt[w]++;if(cnt[w]>=v)return;q.push(w);}}}}
}
int main()
{int t;int x,y,z;scanf("%d",&t);while(t--){init();scanf("%d%d",&v,&e);for(int i=1;i<=v;i++)scanf("%d",&vv[i]);while(e--){scanf("%d%d%d",&x,&y,&z);add(x,y,z);add(y,x,z);}spfa();int flag=1;for(int i=1;i<=v;i++){if(dis[i]==INF){flag=0;break;}ans+=dis[i]*vv[i];}if(flag)printf("%lld\n",ans);elseprintf("No Answer\n");}return 0;
}

POJ-3013: Big Christmas Tree(dij,spfa)相关推荐

  1. poj 3013 Big Christmas Tree(最短路变形)

    传送门:POJ 3013 Big Christmas Tree 描述: Big Christmas Tree Time Limit: 3000MS   Memory Limit: 131072K To ...

  2. 【BZOJ1924】【SDOI2010】所驼门王的宝藏(Tarjan,SPFA)

    题目描述 在宽广的非洲荒漠中,生活着一群勤劳勇敢的羊驼家族.被族人恭称为"先知"的Alpaca L. Sotomon是这个家族的领袖,外人也称其为"所驼门王". ...

  3. 榛子树搜索算法(Hazelnut tree search algorithm,HTS)——一种高效的优化算法

    榛子树搜索算法(Hazelnut tree search algorithm,HTS)--一种高效的优化算法 榛子树搜索算法(Hazelnut tree search algorithm,HTS)是一 ...

  4. P5801 [SEERC2019]Game on a Tree(ACM - ICPC 2019)(树的最大匹配)(完美匹配)

    P5801 [SEERC2019]Game on a Tree(ACM - ICPC 2019)(树的最大匹配) 完美匹配:如果一个图的某个匹配中,所有的顶点都是匹配点,那么它就是一个完美匹配. #i ...

  5. Java开发常见面试题详解(并发,JVM)

    预览 并发 问题 详解 请谈谈你对volatile的理解 link CAS你知道吗? link 原子类Atomiclnteger的ABA问题谈谈?原子更新引用知道吗? link 我们知道ArrayLi ...

  6. 关联分析(Apriori,FP-growth)

    关联分析是数据挖掘中的重要组成部分,旨在挖掘数据中的频繁模式.我们可以通过一个案例数据库挖掘著名案例来大致了解挖掘频繁项集并产生关联规则. 关联分析的基本概念 关联分析:在大规模数据集中寻找有趣的关系 ...

  7. POJ 3069 Saruman's Army(萨鲁曼军)

    POJ 3069 Saruman's Army(萨鲁曼军) Time Limit: 1000MS   Memory Limit: 65536K [Description] [题目描述] Saruman ...

  8. Linux教程之删除文件(rm,rmdir)

    删除文件(rm,rmdir) 适用于 ubuntu 20.04 ubuntu 20.04 是 "西柚云" 主要使用的操作系统 西柚云官网 rmdir 删除一个空目录 # 在 /tm ...

  9. Python 中 (,|)和(and,or)之间的区别 逻辑判断

    and or 符号 只是单一的对比 & | 适合批量对比 (&,|)和(and,or)是两组比较相似的运算符,用在"与"/ "或"上,在用法上有 ...

最新文章

  1. DOM中的onbeforeunload函数
  2. 使用个性化Profile代替Session
  3. Python实训day09pm【Python处理Excel实际应用】
  4. 这是一位川大零基础转行 Python 的人生勇士
  5. python作中国地图背景气泡图_exce表格中怎么制作中国地图背景数据气泡图
  6. 2020 RocketMQ安装
  7. 博文视点学院直播:如何用产品思维解决生活中的迷茫
  8. Java语言学习指导与习题解答_Java语言程序设计(第3版)学习指导与习题解析
  9. 图灵机器人api接入测试
  10. NYOJ 17 (最长单调递增子序列) O (n*n) + O(n*lgn)
  11. 纪念第一次2019河南省第十二届ACM大赛之旅
  12. 走近汇编理解与内核编程(楚狂人)
  13. 《惢客创业日记》2021.07.15-17(周四)房东和租客,谁更弱势?
  14. MySQL Workbench建表时 PK NN UQ B UN ZF AI G的含义
  15. 牛客题库 题解 | #[NOIP2017]图书管理员#
  16. 使用Optional类来消除代码中的null检查
  17. 腾讯云自助建站CloudPages教程,不会代码小白轻松搭建网站
  18. 我们计划招收300名数据人,免费攻读R语言数据分析
  19. 思维导图iMindMap可以插入哪些附件?
  20. TCP/IP协议族(第4版)

热门文章

  1. Linux/Ubuntu网络知识
  2. java教程解析_【B0666】[java视频教程]全方位深入解析最新版SpringBoot源码新手都能学懂视频教程...
  3. 欧科云链OKLink:中国开启“逆袭战” 区块链的盛夏来了?
  4. 关于运行ET框架Demo时遇到的问题(Error Code 10061)
  5. QRS心拍定位: 解决识别对象问题
  6. NSGA-II算法的学习笔记
  7. 苹果手机如何微信双开多开,详细教程来了
  8. Selenium实现全自动打字
  9. php微信地图定位导航,网页拉取微信内置地图(openLocation)详细教程 轻松实现一键导航 – 蓝洛水深...
  10. 4.Ubuntu16.04中SNMP配置