zoukankan      html  css  js  c++  java
  • POJ 1789 prim求最小生成树

    以前写的报告,可以参考下http://blog.sina.com.cn/s/blog_99ca2df501019bkd.html

    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 #define codelen 10//代码长度
     67 int n;
     68 char codes[maxn][codelen+5];//存储每种卡车类型的编号
     69 int d[maxn][maxn];//没对卡车类型之间的距离(邻接矩阵)
     70 int lowcost[maxn];//
     71 int dis;
     72 int edge[maxn][maxn];
     73 int sumweight=0;
     74 int nearvex[maxn];
     75 void prim(int u0){
     76     //从顶点u0出发执行普里姆算法
     77     sumweight=0;//生成树的权值
     78     for(int i=0;i<n;i++){
     79         //初始化lowcost[]数组和neartxt数组
     80         lowcost[i]=edge[u0][i];
     81         nearvex[i]=u0;
     82     }
     83     nearvex[u0]=-1;
     84     for(int i=0;i<n-1;i++){
     85         int min=INF;
     86         int v=-1;
     87         //在lowcoat数组的nearvex[]值为-1的元素中找最小值
     88         for(int j=0;j<n;j++){
     89             if(nearvex[j]!=-1&&lowcost[j]<min){
     90                 v=j;
     91                 min=lowcost[j];
     92             }
     93         }
     94         if(v!=-1){
     95             //v==-1表示没找到权值最小的边
     96            // printf("%d %d %d\n",nearvex[v],v,lowcost[v]);
     97             nearvex[v]=-1;
     98             sumweight+=lowcost[v];
     99             for(int j=0;j<n;j++){
    100                 if(nearvex[j]!=-1&&edge[v][j]<lowcost[j]){
    101                     lowcost[j]=edge[v][j];
    102                     nearvex[j]=v;
    103                 }
    104             }
    105         }
    106     }
    107     printf("The highest possible quality is 1/%d.\n",sumweight);
    108 }
    109 
    110 int main(){
    111     while(scanf("%d",&n)!=EOF){
    112         if(n==0)  break;
    113         memset(codes,0,sizeof(codes));
    114         for(int i=0;i<n;i++){
    115             scanf("%s",codes[i]);
    116         }
    117         //初始化
    118         for(int i=0;i<n;i++){
    119             for(int j=0;j<n;j++){
    120                 d[i][j]=0;
    121             }
    122             d[i][i]=0;
    123         }
    124         for(int i=0;i<n;i++){
    125             for(int j=i+1;j<n;j++){
    126                 dis=0;
    127                 for(int k=0;k<7;k++){
    128                     dis+=codes[i][k]!=codes[j][k];
    129                 }
    130                 edge[i][j]=edge[j][i]=dis;
    131             }
    132         }
    133         prim(0);
    134     }
    135     return 0;
    136 }
  • 相关阅读:
    常见邮件服务器(接收服务器和发送邮件服务器)地址
    Linux下搭建SVN服务器(CentOS)
    macBook下更新python
    画画练习20180627
    如何用Photoshop画一个发光金币(unity游戏素材教程)
    Python+VSCode+Git 学习总结
    如何在MFC DLL中向C#类发送消息
    MFC信号量使用指南
    回归cnBlogs
    Web自动化测试框架Watir(基于Ruby)
  • 原文地址:https://www.cnblogs.com/lanjiangzhou/p/2981711.html
Copyright © 2011-2022 走看看