# Graph theory--the minimum spanning tree

Source: Internet
Author: User

By: poj-1287

`#include <stdio.h> #include <iostream> #include <string.h> #include <algorithm> #include < math.h> #include <stack> #include <queue>using namespace std; #define INF 10000000int N, M;int A, B, c;int map [100] [+], dis[100], v[100];int Prim (int n) {int I, j, K, min, sum = 0;for (i = 1; I <= n; i++) dis[i] = Map[1][i];memset (V, 0, sizeof (v)); v[1] = 1;dis[1] = 0;for (i = 2; I <= n; i++) {k = 1;min = Inf;for (j = 1; J <= N; j + +) if (!v[j] &AMP;&A mp Min>dis[j]) {k = J;min = Dis[j];} Sum + = Min;v[k] = 1;for (j = 1; J <= N; j + +) if (!v[j] && dis[j]>map[k][j]) dis[j] = Map[k][j];} return sum;} int main () {while (scanf ("%d%d", &n, &m)! = EOF) {if (n = = 0) break;for (int i = 1; I <= n; i++) for (int j = 1; J <= N; J + +) Map[i][j] = Inf;while (m--) {scanf ("%d%d%d", &a,&b,&c), if (Map[a][b] >= c) {map[a][b] = map[b][a] = c;}} printf ("%d\n", Prim (n));} return 0;}`

Graph theory--minimum spanning tree

Related Keywords:

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.