首页 > 代码库 > POJ 3013 dijkstra+优先队列
POJ 3013 dijkstra+优先队列
Time Limit: 3000MS | Memory Limit: 131072K | |
Total Submissions: 20918 | Accepted: 4523 |
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
题目意思:
给一棵结点数目为n,边数为m的树,每个结点都有一个权值,每个边也有一个权值,咱们需要减一些树枝使得树的价值最小,剪枝时不能去掉任何结点但是可以去掉边。
树的价值为每个结点的价值*该结点到树根1的长度之和。若给的数据没法构造树输出No Answer,否则输出最小价值。
思路:
很明显,若求树的最小值则需要求每个结点到根的最短路径,那么就需要dijkstra了,但是给的数据量太大,所以用优先队列优化一下就行了。
代码:
1 #include <cstdio> 2 #include <cstring> 3 #include <algorithm> 4 #include <iostream> 5 #include <vector> 6 #include <queue> 7 using namespace std; 8 9 const long long inf=(long long)(1<<16)*50005;10 #define N 5000511 12 13 struct mem{14 int y, w;15 };16 struct node{17 long long d;18 int e;19 bool operator<(const node a)const{20 return a.d<d;21 }22 };23 24 25 long long dis[N];26 int visited[N];27 vector<mem>ve[N];28 int w[N];29 int n, m;30 31 void dijkstra(){32 int i, j;33 priority_queue<node>Q;34 int u, v;35 node p, pp;36 mem q;37 for(i=0;i<=n;i++) dis[i]=inf;38 memset(visited,0,sizeof(visited));39 dis[1]=0;40 p.d=0;p.e=1;41 Q.push(p);42 while(!Q.empty()){43 p=Q.top();44 Q.pop();45 if(visited[p.e]) continue;46 visited[p.e]=1;47 for(i=0;i<ve[p.e].size();i++){48 q=ve[p.e][i];49 if(dis[q.y]>dis[p.e]+q.w){50 dis[q.y]=dis[p.e]+q.w;51 pp.e=q.y;pp.d=dis[q.y];52 Q.push(pp);53 }54 }55 }56 // for(i=0;i<=n;i++) printf("%I64d ",dis[i]);57 // cout<<endl<<endl;58 }59 60 main()61 {62 int t, i, j, x, y, z;63 cin>>t;64 while(t--){65 scanf("%lld %lld",&n,&m);66 for(i=1;i<=n;i++) scanf("%d",&w[i]);67 for(i=0;i<=n;i++) ve[i].clear();68 mem p;69 while(m--){70 scanf("%d %d %d",&x,&y,&z);71 p.y=y;p.w=z;72 ve[x].push_back(p);73 p.y=x;74 ve[y].push_back(p);75 }76 dijkstra();77 78 int f=1;79 long long ans=0;80 for(i=1;i<=n;i++){81 ans+=dis[i]*w[i];82 if(dis[i]==inf){83 f=0;break;84 }85 }86 if(!f) printf("No Answer\n");87 else printf("%lld\n",ans);88 }89 }
POJ 3013 dijkstra+优先队列