Hiho, 29th Week. Minimum spanning tree three • prim algorithm for heap optimization "14 winter vacation got a long time not understand prim optimization: Prim algorithm + heap optimization"

Source: Internet
Author: User

Topic 1: Minimum spanning tree three • Prim algorithm time limit for heap optimization:10000msSingle Point time limit:1000msMemory Limit:256MBDescribe

Back two weeks ago, after the successful use of the kruscal algorithm to solve the problem, small Ho produced a question, on the sparse graph of such an algorithm than prim optimization where?

Tip: No reason to optimize! Input

Each test point (input file) has and has only one set of test data.

In a set of test data:

The 1th act of 2 integers n, M, indicates the number of cities owned by small hi and small hi filters out the number of routes.

The next M-line, each line describes a route, where I acts 3 integers n1_i, n2_i, V_i, respectively, representing the two endpoints of the route and the cost of building roads on this route.

For 100% of data, meet N<=10^5, m<=10^6, at any I meet 1<=n1_i, N2_i<=n, N1_i≠n2_i, 1<=v_i<=10^3.

For 100% of the data, to meet a certain existence of a scheme, so that any two cities can reach each other.

Output

For each set of test data, Output 1 integer ans, indicating that in order for any two cities to reach each other at least the required construction costs through the roads they build.

Sample input
5 291 2 6742 3 2493 4 6724 5 9331 2 7883 4 1472 4 5043 4 381 3 653 5 61 5 8651 3 5901 4 6822 4 2272 4 6361 4 3121 3 1432 5 1582 3 5163 5 1021 5 6051 4 994 5 2242 4 1983 5 8941 5 8453 4 72 4 141 4 185
Sample output
92
Analysis: A long time ago, saw a lot of material also did not understand how to optimize the prim. Today in the operating system class, careless distracted. So, it took only a few minutes to
Come out how exactly to optimize. Before looking at someone else's blog or something, it feels complicated. Now write down my thoughts:
Heap optimization Prim algorithm: First we want to do this before writing prim, starting with the current spanning tree, traversing all the edges that can reach the current spanning tree and finding
A shortest edge, adds the weight of the edge to the sum of the weights of the spanning tree, adds that point marker access, and adds it to the spanning tree. Now if we can optimize the method to find the most
Short side, then the complexity is not reduced. How to optimize it? The previous practice is to use an array to save each node to the spanning tree distance, each time the process is to go through the
Number of groups. Now we use a priority queue (Keng Gen) to save all the edges that can reach the spanning tree, and each time we take out the front and legal edges of the queue and add to the spanning tree
Came up and woke up. Illegal edges will be discarded in this process!
There is a description of the picture with the following text:



Code:
#include <stdio.h> #include <string.h> #include <stdlib.h> #include <ctype.h> #include < math.h> #include <iostream> #include <string> #include <stack> #include <vector> #include <set> #include <queue> #include <algorithm> #define LL long int#define N 100000+10//maximum number of nodes # define M 1    000000+10//maximum number of sides # define MOD 142857//n<=10^5, m<=10^6using namespace Std;int N, m;struct node{int V, W;    BOOL operator< (const node &AMP;DD) const{return w>dd.w; }//Weighted small priority};vector<node>q[n];bool vis[n];//heap optimized prim algorithm ll ans;void Queue_prim () {//Start with Node 1 for extended secure edge generate minimum tree priorities    _queue<node>que; while (!que.empty ()) Que.pop ();    Initialize the emptying priority queue to maintain a small Gan//so that the speed of each search for a secure edge increases ans = 0;    Memset (Vis, false, sizeof (VIS));    for (int i=0; i<q[1].size (); i++) {Que.push (q[1][i]);//Add all the connecting edges of the starting point to the queue} vis[1]=true;    int edge=n-1;//number of sides node cur; while (edge--) {cur = quE.top (); Que.pop ();//This place needs to be noticed//not every edge that is taken out of the priority queue can be added to the spanning tree if (vis[cur.v]==true) {while (VIS[CU            R.V]) {cur=que.top (); Que.pop (); }} ans = ANS+CUR.W;        printf ("%d--", CUR.W); Vis[cur.v]=true; The point that joins the spanning tree is marked to access the for (int i=0; i<q[cur.v].size (); i++) {if (vis[q[cur.v][i].v]==false)//The points currently added to the spanning tree can be expanded The node Que.push (Q[cur.v][i]) to which the filled edge points, or//is added to the queue if not accessed}}}int main () {scanf ("%d%d", &n, &am    P;M);    int I, J;    int U, V, W;    Node cur;    for (i=0; i<=n; i++) q[i].clear ();       for (i=0; i<m; i++) {scanf ("%d%d%d", &u, &v, &w); Cur.v=v;       Cur.w=w;       Q[u].push_back (cur);       Cur.v=u; Q[v].push_back (cur);    Establish two-way Edge} Queue_prim ();    printf ("%lld\n", ans); return 0;}

Hiho, 29th Week. Minimum spanning tree three • prim algorithm for heap optimization "14 winter vacation got a long time not understand prim optimization: Prim algorithm + heap optimization"

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.