标签:des style blog color io os for 数据 div
1 2 1 2
16
算法:构造图,最小生成树
#include <iostream> #include <stdio.h> #include <string.h> #include <string> #include <algorithm> using namespace std; int a[1000]; int map[1000][1000]; int vis[1000]; int dis[1000]; void prim(int n) { int i, j; int sum=0; memset(vis, 0, sizeof(vis)); for(i=0; i<n; i++) { dis[i] = map[0][i] ; } vis[0]=1; int pos; for(i=1; i<n; i++) { int min=100000000; for(j=0; j<n; j++) { if(dis[j]<min && vis[j]==0) { min = dis[j]; pos = j; } } vis[pos]=1; sum =sum+min; for(j=0; j<n; j++) { if(vis[j]==0 && map[pos][j]<dis[j] ) dis[j] = map[pos][j] ; } } printf("%d\n", sum ); } int main() { int t, i, j, n; int dd; cin>>t; while(t--) { cin>>n; for(i=0; i<n; i++) { cin>>a[i] ; } for(i=0; i<n; i++) { for(j=i+1; j<n; j++) { dd = 3*(a[i]+a[j])+7 ; map[i][j] = dd; map[j][i] = dd; } } prim(n) ; } return 0; }
标签:des style blog color io os for 数据 div
原文地址:http://www.cnblogs.com/yspworld/p/3971403.html