POJ 3013 Big Christmas Tree (spfa)
来源:互联网 发布:发货打印软件 编辑:程序博客网 时间:2024/05/20 04:15
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 throughn. 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 numbersv,e (0 ≤ v, e ≤ 50000) are given in the first line of each test case. On the next line,v positive integerswi indicating the weights ofv nodes are given in one line. On the followinge lines, each line contain three positive integersa, b,c indicating the edge which is able to connect two nodesa 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
22 11 11 2 157 7200 10 20 30 40 50 601 2 12 3 32 4 23 5 43 7 23 6 31 5 9
Sample Output
151210
Source
#include<iostream>#include<algorithm>#include<map>#include<cstring>#include<cstdio>#include <queue>using namespace std;typedef long long ll;const int maxn=50005;int tot;int head[maxn*2+100];int vis[maxn*2+100];ll dis[maxn*2+100];struct node{ int u,v,next; ll w;} edge[maxn*10+100];void add(int u,int v,ll w){ edge[tot].u=u; edge[tot].v=v; edge[tot].w=w; edge[tot].next=head[u]; head[u]=tot++; edge[tot].u=v; edge[tot].v=u; edge[tot].w=w; edge[tot].next=head[v]; head[v]=tot++;}void spfa(int s){ memset(vis,0,sizeof(vis)); queue<int>que; que.push(s); vis[s]=1; while(!que.empty()) { int u=que.front(); que.pop(); vis[u]=0; for(int i=head[u]; i!=-1; i=edge[i].next) { int v=edge[i].v; ll cost=edge[i].w; if(dis[v]>dis[u]+cost) { dis[v]=dis[u]+cost; if(!vis[v]) { vis[v]=1; que.push(v); } } } }}int main(){ int t,i,j; ll w1[maxn]; scanf("%d",&t); while(t--) { int n,m; scanf("%d%d",&n,&m); int a,b; ll c; for(i=1; i<=n; i++) { scanf("%lld",&w1[i]); dis[i]=((ll)1<<63)-1; } tot=0; dis[1]=0; memset(head,-1,sizeof(head)); for(i=0; i<=2*m; i++) edge[i].next=-1; for(i=1; i<=m; i++) { scanf("%d%d%lld",&a,&b,&c); add(a,b,c); } spfa(1); int flag=0; ll res=0; for(i=1; i<=n; i++) { if(dis[i]==((ll)1<<63)-1) { flag=1; break; } res+=dis[i]*w1[i]; } if(flag) printf("No Answer\n"); else printf("%I64d\n",res); } return 0;}
- POJ--3013[Big Christmas Tree] (SPFA)
- POJ 3013 Big Christmas Tree (spfa)
- POJ 3013 Big Christmas Tree(SPFA)
- POJ 3013 Big Christmas Tree SPFA模版
- [SPFA]POJ 3013Big Christmas Tree
- Big Christmas Tree - POJ 3013 spfa
- poj 3013 Big Christmas Tree spfa
- POJ 3013 Big Christmas Tree (SPFA最短路)
- POJ 3013 Big Christmas Tree (最短路 spfa)
- poj 3013 Big Christmas Tree(Dijktra或者spfa)
- POJ-3013: Big Christmas Tree(dij,spfa)
- poj 3013 spfa 最短路 Big Christmas Tree
- 最短路SPFA——Big Christmas Tree ( POJ 3013 )
- poj 3013 big christmas tree 最短路SPFA
- poj 3031 Big Christmas Tree(水spfa)
- Poj 3013 Big Christmas Tree
- poj 3013 big christmas tree
- POJ - 3013 Big Christmas Tree
- c#实现简单计算器
- linux常用c函数 进程操作篇 http://blog.chinaunix.net/uid-25906157-id-3136757.html
- 马哥Linux运维笔记---1
- 03-Block的使用场景
- C++11:并发、多线程
- POJ 3013 Big Christmas Tree (spfa)
- Findbugs 缺陷详解与英文代号的对照表
- 中级软件设计师考点大盘点
- 素数定理
- OCP-V13-600
- 面试准备之css3
- 原码,反码,补码
- 持续集成CI(二)Jenkins
- 百度编辑器ueditor上传图片及文件路径配置