POJ-3013: Big Christmas Tree(dij,spfa)
来源:互联网 发布:网络方面 证书 编辑:程序博客网 时间:2024/06/11 13:40
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
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
题意:哈理工1419是本题的中文题意。
分析: 第二个样例
得到:最小的答案=每个点*该点到根结点的最短路
注意: INF开到0x3f3f3f3f3f3f3f3f
- dij
#include<queue>#include<stdio.h>#include<vector>#include<string.h>#define INF 0x3f3f3f3f3f3f3f3fusing 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); else printf("No Answer\n"); } return 0;}
- spfa
#include<stdio.h>#include<string.h>#include<vector>#include<queue>#define INF 0x3f3f3f3f3f3f3f3fusing 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); else printf("No Answer\n"); } return 0;}
- POJ-3013: Big Christmas Tree(dij,spfa)
- 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+优先队列优化 求最短路)
- 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
- 保存文件
- 垃圾回收实例
- error LNK2019: 无法解析的外部符号 _SQLConnect@28,该符号在函数
- 动态规划:最大子矩阵
- 将二叉树拆成链表
- POJ-3013: Big Christmas Tree(dij,spfa)
- dede中php标签用法
- vb.net 教程 5-13 图像处理之像素处理 6
- 动态规划总结
- 有关线程的相关知识(上)
- 计算机操作系统-磁盘存储器的管理
- 前端jquery之后的进阶
- ubuntu16.04 编译安装php7,增加 pthreads 多线程控制器
- 机器视觉开源库学习