zoukankan      html  css  js  c++  java
  • Truck History---poj1789

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

    题意: They defined the distance of truck types as the number of positions with different letters in truck type codes

    这句话说明距离被定义成编号之间字母的不同个数;编号是由7个字符构成的字符串;

    然后求最小生成树;

    #include<stdio.h>
    #include<string.h>
    #include<map>
    #include<iostream>
    #include<algorithm>
    #include<math.h>
    #define N 2010
    #define INF 0xfffffff
    
    using namespace std;
    
    int maps[N][N],vis[N],dist[N], n;
    
    int Prim(int start)
    {
        int ans=0;
        vis[start] = 1;
        for(int i=1; i<=n; i++)
        {
            dist[i] = maps[start][i];
        }
        for(int i=1; i<=n; i++)
        {
            int Min = INF, index = -1;
            for(int j = 1; j <= n; j++)
            {
                if(vis[j]==0 && Min>dist[j])
                {
                    Min = dist[j];
                    index = j;
                }
            }
            if(index == -1) break;
            ans += Min;
            vis[index] = 1;
            for(int j=1; j<=n; j++)
            {
                if(vis[j]==0 && dist[j] > maps[index][j])
                    dist[j] = maps[index][j];
            }
        }
        return ans;
    }
    
    int main()
    {
        int i, j, k;
        char s[N][20];
        while(scanf("%d", &n), n)
        {
            for(i=1; i<=n; i++)
            {
                scanf("%s", s[i]);
                vis[i] = 0;
                dist[i] = INF;
                for(j=1; j<=n; j++)
                    maps[i][j] = INF;
            }
    
            for(i=1; i<=n; i++)
            {
                for(j=1; j<i; j++)
                {
                    int cnt = 0;
                    for(k=0; k<7; k++)
                        if(s[i][k] != s[j][k])
                            cnt++;
                    maps[i][j] = maps[j][i] = cnt;
                }
            }
            int ans;
            ans = Prim(1);
            printf("The highest possible quality is 1/%d.
    ",  ans);
        }
        return 0;
    }
  • 相关阅读:
    转:深入 AngularUI Router
    angularJS $scope的$apply方法实现model刷新
    CSS 如何让 height:100%; 起作用
    【AngularJs】---$sce 输出Html
    angular 组件之间传值
    kendo Grid 列添加自定义模板
    关于“内控点”
    关于总结
    咏春
    一只老鼠夹
  • 原文地址:https://www.cnblogs.com/zhengguiping--9876/p/4681751.html
Copyright © 2011-2022 走看看