ZOJ 1586 prim算法求最小生成树

来源:互联网 发布:linux oracle 进程 编辑:程序博客网 时间:2024/06/02 23:59

题意:建立QS网络的最小费用。

分析:运用prim算法求解最小生成树

关键:本题关键是建图。在构造有向网时,每条边的权值为两个QS的适配器的加个加上两个QS之间网线的加个。这样就可以解决问题了。

简化:本题只需最小生成树的权值,不需要记录构造最小生成树时选择的边,因此可以将lowcost数组和nearvex数组合二为一。

自己做法还是一起解决吧,省得记那么多。呵呵

View Code
  1 // I'm the Topcoder  2 //C  3 #include <stdio.h>  4 #include <stdlib.h>  5 #include <string.h>  6 #include <ctype.h>  7 #include <math.h>  8 #include <time.h>  9 //C++ 10 #include <iostream> 11 #include <algorithm> 12 #include <cstdio> 13 #include <cstdlib> 14 #include <cmath> 15 #include <cstring> 16 #include <cctype> 17 #include <stack> 18 #include <string> 19 #include <list> 20 #include <queue> 21 #include <map> 22 #include <vector> 23 #include <deque> 24 #include <set> 25 using namespace std; 26  27 //*************************OUTPUT************************* 28 #ifdef WIN32 29 #define INT64 "%I64d" 30 #define UINT64 "%I64u" 31 #else 32 #define INT64 "%lld" 33 #define UINT64 "%llu" 34 #endif 35  36 //**************************CONSTANT*********************** 37 #define INF 0x3f3f3f3f 38 #define eps 1e-8 39 #define PI acos(-1.) 40 #define PI2 asin (1.); 41 typedef long long LL; 42 //typedef __int64 LL;   //codeforces 43 typedef unsigned int ui; 44 typedef unsigned long long ui64; 45 #define MP make_pair 46 typedef vector<int> VI; 47 typedef pair<int, int> PII; 48 #define pb push_back 49 #define mp make_pair 50  51 //***************************SENTENCE************************ 52 #define CL(a,b) memset (a, b, sizeof (a)) 53 #define sqr(a,b) sqrt ((double)(a)*(a) + (double)(b)*(b)) 54 #define sqr3(a,b,c) sqrt((double)(a)*(a) + (double)(b)*(b) + (double)(c)*(c)) 55  56 //****************************FUNCTION************************ 57 template <typename T> double DIS(T va, T vb) { return sqr(va.x - vb.x, va.y - vb.y); } 58 template <class T> inline T INTEGER_LEN(T v) { int len = 1; while (v /= 10) ++len; return len; } 59 template <typename T> inline T square(T va, T vb) { return va * va + vb * vb; } 60  61 // aply for the memory of the stack 62 //#pragma comment (linker, "/STACK:1024000000,1024000000") 63 //end 64  65 #define maxn 2000+10 66 int n,m; 67 int edge[maxn][maxn];//邻接矩阵 68 int lowcost[maxn]; 69 int nearvex[maxn]; 70 int sumweight=0; 71 int value[maxn]; 72 void prim(int u0){ 73     //从顶点u0出发执行普里姆算法 74     sumweight=0;//生成树的权值 75     for(int i=1;i<=n;i++){ 76         //初始化lowcost[]数组和neartxt数组 77         lowcost[i]=edge[u0][i]; 78         nearvex[i]=u0; 79     } 80     nearvex[u0]=-1; 81     for(int i=1;i<n;i++){ 82         int min=INF; 83         int v=-1; 84         //在lowcoat数组的nearvex[]值为-1的元素中找最小值 85         for(int j=1;j<=n;j++){ 86             if(nearvex[j]!=-1&&lowcost[j]<min){ 87                 v=j; 88                 min=lowcost[j]; 89             } 90         } 91         if(v!=-1){ 92             //v==-1表示没找到权值最小的边 93            // printf("%d %d %d\n",nearvex[v],v,lowcost[v]); 94             nearvex[v]=-1; 95             sumweight+=lowcost[v]; 96             for(int j=1;j<=n;j++){ 97                 if(nearvex[j]!=-1&&edge[v][j]<lowcost[j]){ 98                     lowcost[j]=edge[v][j]; 99                     nearvex[j]=v;100                 }101             }102         }103     }104     printf("%d\n",sumweight);105 }106 107 int main(){108     int w;109     int t;110     scanf("%d",&t);111     while(t--){112         scanf("%d",&n);113         sumweight=0;114         //初始化115         for(int i=1;i<=n;i++){116             for(int j=1;j<=n;j++){117                 edge[i][j]=INF;118             }119             edge[i][i]=0;120         }121 122         for(int i=1;i<=n;i++){123             value[i]=0;124         }125 126         for(int i=1;i<=n;i++){127             scanf("%d",&value[i]);128         }129         for(int i=1;i<=n;i++){130             for(int j=1;j<=n;j++){131             scanf("%d",&w);132             edge[i][j]=w;133             edge[i][j]+=value[i]+value[j];134             }135         }136         prim(1);137     }138     return 0;139 }