首页 > 学院 > 开发设计 > 正文

prim算法学习

2019-11-06 06:07:34
字体:
来源:转载
供稿:网友
#include <iostream>#include <string.h>#include <algorithm>#include <stdio.h>using namespace std;#define INF 10000000int N, E;int sum;int town[600][1000];void PRim(){    int dis[1000], vis[1000], now = 0, Min;    sum = 0;    memset(vis, 0, sizeof(vis));    for(int i = 0; i < N; i++)        dis[i] = INF;    vis[0] = 1, dis[0] = 0;    for(int i = 0; i < N; i++){        for(int j = 0; j < N; j++)            if(!vis[j] && dis[j] > town[now][j])                dis[j] = town[now][j];        Min = INF;        for(int j = 0; j < N; j++){            if(!vis[j] && dis[j] < Min)                Min = dis[now = j];        }        vis[now] = 1;    }    for(int i = 0; i < N; i++)        sum += dis[i];    printf("%d/n", sum);    return ;}int main(){    int T;    cin >> T;    while(T--){        sum = 0;        cin >> N >> E;        for(int i = 0; i < N; i++)            for(int j = 0; j < N; j++)                town[i][j] = INF;        int A, B, K;        for(int i = 0; i < E; i++){            cin >> A >> B >> K;            town[A][B] = K;            town[B][A] = K;        }        prim();    }    return 0;}
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表