#1097: Minimum spanning tree one · Prim algorithm Time limit:10000msSingle Point time limit:1000msMemory Limit:256MBDescribe
Recently, little hi is very fond of playing a game simulation of the city opened a new mod, in this mod, the player can have more than one city!
But the problem also followsLittle hi now has n cities in hand, and it is known that the cost of building roads between any two cities in these n cities, little hi would like to know that the minimum cost would allow any two cities to reach each other through the roads they built (assuming a, B, c three cities, Only the road between AB and BC can be built, and the two roads will be connected by AC.
Tip: I don't know why prim algorithm and Dijstra algorithm are like σ (っ°д°;) our store. xClose Tip: I don't know why prim algorithm and Dijstra algorithm are like σ (っ°д°;) our store.
Small Ho is also a way to watch the little hi play this game over, and then put forward their own ideas: "Why must be the least cost?" Just create a free pass. ”
Little Hi nu Way: "Obsessivecompulsive disorder do you know what meaning?" ”
So little ho shut up and couldn't say.
Little Hi grunted, and then said: "Well, take this opportunity, I can teach you the minimum spanning tree is what a matterfirst say the origin of the name, spanning tree is relative to a certain figure g, that is, you can not say a tree T is a spanning tree, can only say T is the formation of GTree, The spanning tree means that the node of T is the same as the node of G, and the edge set of T is a subset of G's edge set, which is called spanning tree. And the smallest spanning treemeaning the right edge and the smallest tree in all the spanning trees of G. "
"So the smallest spanning tree can have more than one tree?" Little Ho asked.
Little hi nodded, "Yes, so this problem only needs to output the Benquan of the smallest spanning tree and that's itit's definitely the only thing." ”
Small Ho scratched his scratching his heads and said, "What should I do then?" I don't have a clueif you use enumerations, the time complexity is a number of levels, and certainly not! ”
"And listen to me slowly!" said the man. "Little Hi Way:" first I want to prove a conclusion: for the city I (i≠1), if I and the City 1 distance not more than any other city J (j≠1) and the City 1 distance, then (1, i) this edge must exist in a tree of the smallest spanning. "
"Do you mean that the shortest side departing from City 1 must belong to the smallest spanning tree?" That sounds a little bit plausible, but why? Little Ho asked.
Little hi nodded: "The proof is this: for a minimum spanning tree tif (1, i) in which the conclusion has been proved, so we consider (1, i) is not in the case." and 1 and I will certainly be connected through a path (because the tree is not a forest), it may be assumed that 1P1P2...pki. "
"Well, then what?" ”
"Well, if I delete this edge (1, p1) and add it on (1, i), then is this a spanning tree?" "Little Hi asked."
"The point has not changed, the edge set is still a subset of Figure g, then only need to see or not a treeall the points are still connected, and there is no ring (add edge (1, i) only a ring 1p1p2...pki1, but (1, p1) was deleted)! So this is still a tree, that is to say ... This change is still a spanning tree. Little Ho thought, said.
"If we call the altered spanning Tree T ', it is not difficult to find that because (1, p1) costs are greater than or equal to (1, i), we can know T ' Benquan and less than equals T, and because T itself is already the smallest spanning tree, it is not difficult to find T ' will also be the smallest spanning tree ..." Small Ho suddenly shouted: "That is to say that the conclusion has been proved: (1, i) this edge must exist in a minimum spanning tree. "
"That's right!" So long march the first step has already gone, next only need to follow the first step to go down on the line! "Little Hi Way."
"What do you mean ... Is it going to be a lot more complicated then? Little Ho was puzzled.
Little hi sighed and said, "stupid ... You think so, if I'm sure (1, i) this edge must exist in a tree of the smallest spanning, then I follow the idea of the Dijstra algorithm, merge 1 and I into a point, then the question is to ask for the remaining N2 points and the smallest spanning tree of this point? The problem does not change, but the scale is reduced, then only one time to carry out such a step, the problem can not be a perfect solution? ”
Small Ho side thinking side way: That is to say ... For a minimum spanning tree problem with n points, I have found the nearest point I with Point 1th, merging the two points, leaving N1 points, and then seeking the nearest point I of the new 1th point, merging the two points, leaving the N2 points ... And so on, until the last point left, add up the cost of all the previously merged edgesthat's the answer! ”
"You also, hurriedly go to write the program!" I'm going to use it for a while! ”
CloseInput
Each test point (input file) has and has only one set of test data.
In a set of test data:
The 1th behavior is 1 integer n, which indicates the number of cities owned by small hi.
The next n rows, for a n*n matrix A, describe the cost of building a road between any two cities, with the number of J in line I being AIJ, which represents the cost of building roads between the city of Block I and Block J.
For 100% of data, satisfies n<=10^3, for any I, satisfies aii=0, for any I, J satisfies Aij=aji, 0<aij<10^4.
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




Sample output

4178
vector variablelength arrays can emulate adjacency tables, but when this storage structure is used to process multiple sets of data, it is important to empty the vector array
otherwise, Inevitable wa!
Code:
#include <stdio.h> #include <string.h> #include <algorithm> #include <vector> #include < Iostream> #define INF 2100000000using namespace std;struct node{int dd; int W;} T;vector<node>q[1001];int dis[1001];bool vis[1001];void Vector_prim (int n) {int i, J; Vector<node>::iterator it; Memset (Vis, false, sizeof (VIS)); memset (DIS, INF, sizeof (DIS)); For (It=q[1].begin (); It!=q[1].end (); it++) {dis[it>dd]=it>w; } vis[1]=true; int POS; int sum=0; for (I=1; i<n; i++) {int mm=inf; for (j=1; j<=n; J + +) {if (!vis[j] && dis[j]<mm) {mm=dis[j]; Pos=j; }} sum+=mm; Vis[pos]=true; For (It=q[pos].begin (); It!=q[pos].end (); it++) {if (!vis[it>dd] && it>w <dis[it> ;DD]) {dis[it>dd]=it>w; } } } printf ("%d\n", sum);} int main () {int n; int I, J; int W; scanf ("%d", &n); for (i=0; i<=n; i++) {q[i].clear (); } for (I=1; i<=n; i++) {for (j=1; j<=n; J + +) {scanf ("%d", &w); if (i!=j) {t.dd=j; T.w=w; Q[i].push_back (t); }}} Vector_prim (n); return 0;}
The #1097 of Hihocoder: minimum spanning tree one · Prim algorithm (using vector twodimensional simulation adjacency table, the prim () spanning tree algorithm, * "template")