zoukankan      html  css  js  c++  java
  • poj 1789 Truck History(最小生成树)

    模板题

    题目:http://poj.org/problem?id=1789

    题意:有n个型号,每个型号有7个字母代表其型号,每个型号之间的差异是他们字符串中对应字母不同的个数d[ta,tb]代表a,b之间的差异数
    问1/Σ(to,td)d(to,td)最大值

    prime:

     1 #include <iostream>
     2 #include<cstdio>
     3 #include<cstring>
     4 #include<cstdlib>
     5 #include<stack>
     6 #include<queue>
     7 #include<cmath>
     8 #include<algorithm>
     9 using namespace std;
    10 
    11 int grah[2100][2100];
    12 int sum=0;
    13 void pim(int n)
    14 {
    15     int i,j,pos;
    16     int min,v[2100]= {0},d[2100];
    17     for(i=1; i<=n; i++)
    18         d[i]=grah[1][i];
    19     v[1]=1;
    20     for(i=2; i<=n; i++)
    21     {
    22         min=99999999;
    23         for(j=1; j<=n; j++)
    24         {
    25             if(!v[j]&&min>d[j])
    26             {
    27                 min=d[j];
    28                 pos=j;
    29             }
    30         }
    31         sum+=min;
    32         v[pos]=1;
    33         for(j=1; j<=n; j++)
    34         {
    35             if(!v[j])
    36             {
    37                 if(d[j]>grah[pos][j])
    38                     d[j]=grah[pos][j];
    39             }
    40         }
    41     }
    42 
    43 }
    44 int main()
    45 {
    46     int n,i,j,k,cou;
    47     char s[2100][80];
    48     while(cin>>n&&n)
    49     {
    50         getchar();
    51         for(i=1; i<=n; i++)
    52             cin>>s[i];
    53         for(i=1; i<=n; i++)
    54         {
    55             for(j=i+1; j<=n; j++)
    56             {
    57                 cou=0;
    58                 for(k=0; k<7; k++)
    59                 {
    60                     if(s[i][k]!=s[j][k])//如果有一个字母不同就+1
    61                         cou++;
    62                 }
    63                 grah[i][j]=grah[j][i]=cou;
    64             }
    65             grah[i][i]=0;
    66         }
    67         pim(n);
    68         printf("The highest possible quality is 1/%d.
    ",sum);
    69         sum=0;
    70     }
    71     return 0;
    72 }
  • 相关阅读:
    Asp.Net上传大文件(页面超时)
    C#文件的大小
    设计模式简单工厂、工厂方法、抽象工厂方法
    设计模式迭代器模式
    Asp.Net下载文件
    设计模式桥接模式
    CSS尺寸(Dimensions)
    设计模式单件模式
    Android 换肤
    像QQtab切换效果的例子
  • 原文地址:https://www.cnblogs.com/bfshm/p/3234206.html
Copyright © 2011-2022 走看看