poj1789Truck History 最小生成树水题

//n个车牌号,刚开始只有一个车牌,其他车牌都是由一个车牌直接或间接产生
//一个车牌到另一个车牌的产生权值是它们之间的数字不同的个数
//问产生的最小的边权和
//最小生成树
#include<cstdio>
#include<cstring>
#include<iostream>
using namespace std ;
const int maxn = 2010 ;
const int inf = 0x3f3f3f3f  ;
char str[maxn][10] ;
int map[maxn][maxn] ;
int vis[maxn] ;
int dis[maxn] ;
int n ;
int prime()
{
    for(int i = 2;i <= n;i++)
    dis[i] = inf ;
    dis[1] = 0 ;
    memset(vis , 0 ,sizeof(vis)) ;
    int ans = 0 ;
    while(1)
    {
        int mi = inf ;int pos ;
        for(int i = 1;i <= n;i++)
        if(!vis[i] && dis[i] < mi)
        mi = dis[pos = i] ;
        if(mi == inf)break;
        vis[pos] = 1;
        ans += mi ;
        for(int j = 1;j <= n ;j++)
        dis[j] = min(dis[j] , map[pos][j]) ;
    }
    return ans ;
}
int main()
{
    while(scanf("%d" , &n) && n)
    {
        for(int i = 1;i <= n;i++)
        {
            scanf("%s" , &str[i][1]) ;
            for(int j = 1;j <= i;j++)
            {
                int sum = 0 ;
                for(int k = 1;k <= 7 ;k++)
                if(str[i][k] != str[j][k])
                sum++ ;
                map[i][j] = map[j][i] = sum ;
            }
        }
        int ans = prime() ;
        printf("The highest possible quality is 1/%d.\n" , ans) ;
    }
    return 0 ;
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

时间: 2024-07-29 10:38:43

poj1789Truck History 最小生成树水题的相关文章

POJ-1861,Network,最小生成树水题,,注意题面输出有问题,不必理会~~

Network Time Limit: 1000MS   Memory Limit: 30000K          Special Judge http://poj.org/problem?id=1861 Description Andrew is working as system administrator and is planning to establish a new network in his company. There will be N hubs in the compa

HDU 1879--继续畅通工程【kruskal &amp;&amp; 最小生成树 &amp;&amp; 水题】

继续畅通工程 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 17798    Accepted Submission(s): 7662 Problem Description 省政府"畅通工程"的目标是使全省任何两个村庄间都可以实现公路交通(但不一定有直接的公路相连,只要能间接通过公路可达即可).现得到城镇道路统计表,表中列

hdu 2682 Tree 最小生成树~~~~水题一枚,,用到了筛法求素数,我竟然在格式上面PE了两次!!

Tree Time Limit: 6000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 1754    Accepted Submission(s): 509 Problem Description There are N (2<=N<=600) cities,each has a value of happiness,we consider two cities

poj1789Truck History(最小生成树prim算法)

题目链接: 啊哈哈,点我点我 思路:根据字符串中不同的长度建图,然后求图的最小生成树.. 题目: Truck History Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 18272   Accepted: 7070 Description Advanced Cargo Movement, Ltd. uses trucks of different types. Some trucks are used for vege

【最小生成树杂题】

这里谈一下最小生成树 生成树的概念:连通图G的一个子图如果是一棵包含G的所有顶点的树,则该子图称为G的生成树.生成树是连通图的极小连通子图.所谓极小是指:若在树中任意增加一条边,则将出现一个回路:若去掉一条边,将会使之变成非连通图. 生成树各边的权值总和称为生成树的权.权最小的生成树称为最小生成树. 最小生成树一个有 n 个结点的连通图的生成树是原图的极小连通子图,且包含原图中的所有 n 个结点,并且有保持图连通的最少的边.常用于求最小生成树得算法包括kruskal(克鲁斯卡尔)算法或Prim(

POJ百道水题列表

以下是poj百道水题,新手可以考虑从这里刷起 搜索1002 Fire Net1004 Anagrams by Stack1005 Jugs1008 Gnome Tetravex1091 Knight Moves1101 Gamblers1204 Additive equations 1221 Risk1230 Legendary Pokemon1249 Pushing Boxes 1364 Machine Schedule1368 BOAT1406 Jungle Roads1411 Annive

2015南阳CCPC L - Huatuo&#39;s Medicine 水题

L - Huatuo's Medicine Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 无 Description Huatuo was a famous doctor. He use identical bottles to carry the medicine. There are different types of medicine. Huatuo put medicines into the bottles and chain these b

sdut 2841 Bit Problem (水题)

题目 贴这个题是因为看题解有更简单的方法, 我做的时候是直接算的, 也很简单. 贴一下题解吧: 如果一个整数不等于 0,那么该整数的二进制表示中至少有一位是 1. 这个题结果可以直接输出 x - (x&(x-1)); 因为x-1 之后二进制下,就是最右边的1变成了0, 最右边的1的 右边所有的0变成了1, 不影响最左边. 我的代码: 1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4

sdut 2413:n a^o7 !(第三届山东省省赛原题,水题,字符串处理)

n a^o7 ! Time Limit: 1000MS Memory limit: 65536K 题目描述 All brave and intelligent fighters, next you will step into a distinctive battleground which is full of sweet and happiness. If you want to win the battle, you must do warm-up according to my inst