POJ 2485 Highways: minimum spanning tree Prim

Source: Internet
Author: User

highways:http://poj.org/problem?id=2485

To give you a graph of n vertices stored in the form of adjacency matrices, let you ask for its minimum spanning tree and find the weights of the largest edges in the spanning tree.

Train of thought: use prim to seek, judge condition to change on the line.

Ps:dis array initialization with memset has been re, I hope to know how the generous enlighten, thanks ~

More Wonderful content: http://www.bianceng.cnhttp://www.bianceng.cn/Programming/sjjg/

#include <stdio.h> #include <string.h> #define INF 0x3f3f3f3f int map[510][510];
int dis[510];
  
int n, m;
  
int min (int a, int b) {return a > b b:a;}
    int Prim () {int min_ele, min_node;
    for (int i = 1; I <= n; i++) {dis[i] = INF;
    ///here if using memset (DIS, INF, sizeof (DIS)), the words will always be re int r = 1;
    int Ans = 0;
        for (int i = 1; i < n; i++) {min_ele = INF;
        DIS[R] =-1; for (int j = 1; J <= N; j +) {if (J!= R && Dis[j] >= 0) {di
                S[j] = min (Dis[j], map[r][j]);
                    if (Dis[j] < Min_ele) {Min_ele = Dis[j];
                Min_node = j;
        }} R = Min_node;
    if (Min_ele > ans) ans = min_ele;
return Ans;
    } void Solve () {scanf ("%d", &m);
        while (m--) {scanf ("%d", &n); memset (Map, 0, sizeof (map)); for (int i = 1; I <= n; i++) {for (int j = 1; J <= N; j +) {scanf ("%
            D ", &map[i][j]);
    } printf ("%d\n", Prim ());
  
    int main () {Solve ()};
return 0; }

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.