#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;}
新闻热点
疑难解答