POJ 3013 Big Christmas Tree (dij+ Priority queue optimization shortest)

Source: Internet
Author: User

Template

Serie A championship: give you a picture, 1 always root, each party has a single value, each point has the right to re.

Price value per edge = SUM (subsequent node weight) * unit Price value.

The lowest price requires a tree value, which forms the minimum value of a tree's n-1 edge.


Algorithm:

1, because each side of the value is multiplied by the subsequent access to the weight of the node. And the point that goes to the later interview will pass through this side.

In fact, the total value is the shortest path to each point * the weight of this point.

2, but this problem data volume is really too big. 50,000 points, 50,000 sides.

Write the common DIJ algorithm tle.

Priority queue optimization must be added--

It is said that SPFA can also live. But the SPFA algorithm is not stable----------------the Dijkstra algorithm with priority queue or heap optimization is usually not negative

Should be able to solve this problem.

3, Pit point: The point is 0 or 1 o'clock, the value is 0, to Special award. Otherwise it will be tle.


#include <cstdio> #include <iostream> #include <cstring> #include <algorithm> #include < queue> #define MAXN 50010const __int64 INF = 10000000000;using namespace std;struct node{int to,next,val;} Edge[maxn*2];int V,head[maxn],c[maxn],cnt;long long dis[maxn];bool vis[maxn];typedef pair<long long,int> PII;    PRIORITY_QUEUE&LT;PII, vector<pii>,greater<pii> > Q;void Add (int x,int y,int z) {edge[cnt].to = y;    Edge[cnt].val = Z;    Edge[cnt].next = Head[x]; HEAD[X] = cnt++;}    Long Long Dij () {for (int i=2;i<=v;i++) dis[i] = INF;    while (!q.empty ()) Q.pop ();    int sum = 0;    Long long ret = 0;    Long long X;    int y;    Dis[1] = 0;    Q.push (Make_pair (dis[1],1));        while (!q.empty ()) {PII cur = q.top ();        Q.pop ();        x = Cur.first;        y = Cur.second;        if (Vis[y]) continue;        Vis[y] = true;        sum++;        RET + = X*c[y];          for (int i=head[y];i!=-1;i=edge[i].next) {  int u = edge[i].to,p = Edge[i].val;                if (dis[u]>dis[y]+p) {dis[u] = dis[y]+p;            Q.push (Make_pair (dis[u],u));    }}} if (Sum<v) return-1; else return ret;}    int main () {int t,w,a,b,cost;    scanf ("%d", &t);        while (t--) {scanf ("%d%d", &v,&w);        memset (head,-1,sizeof (head));        CNT = 0;        for (int i=1;i<=v;i++) scanf ("%d", &c[i]);           for (int i=0;i<w;i++) {scanf ("%d%d%d", &a,&b,&cost);           Add (A,b,cost);        Add (B,a,cost);            } if (v<=1) {printf ("0\n");        Continue        } memset (Vis,0,sizeof (VIS));        Long Long ans = dij ();        if (ans = =-1) printf ("No answer\n");    else printf ("%i64d\n", ans); } return 0;}



Copyright notice: This article Bo Master original articles, blogs, without consent may not be reproduced.

POJ 3013 Big Christmas Tree (dij+ Priority queue optimization shortest)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.