首页 > 代码库 > poj1789 MST 读题生涯永不停歇

poj1789 MST 读题生涯永不停歇

     题目: 链接在此

1、图论刷刷乐#1的第一题,无奈看了好长时间题目还是看不懂= =,明知是最水的题目

2、搜懂题目后,比较裸的MST,但还是决定写个题解,虽然没什么可说的,只是来警戒自己,还是要努力读题,YY大法,这也是水平的一个体现!


#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <vector>
#include <cmath>
#include <queue>
#include <stack>
#include <map>
#include <set>

using namespace std;

#define clr(x) memset(x,0,sizeof(x))
#define fp1 freopen("in.txt","r",stdin)
#define fp2 freopen("out.txt","w",stdout)
#define pb push_back

#define INF 0x3c3c3c3c
typedef long long LL;

#define typec int
const int V = 2005;
const typec inf = 0x3f3f3f3f;
int vis[V]; typec lowc[V];
int g[V][V];
typec prim(typec cost[][V], int n){
    int i, j, p;
    typec minc, res = 0;
    memset(vis, 0, sizeof(vis));
    vis[0] = 1;
    for(i = 1;i < n;i++) lowc[i] = cost[0][i];
    for(i = 1;i < n;i++){
        minc = inf; p = -1;
        for(j = 0;j < n;j++)
        if(0==vis[j] && minc>lowc[j]){
            minc = lowc[j]; p = j;
        }
        if(inf == minc) return -1;
        res += minc;  vis[p] = 1;
        for(j = 0;j < n;j++){
            if(0 == vis[j] && lowc[j] > cost[p][j])
                lowc[j] = cost[p][j];
        }
    }
    return res;
}
string s[V];
int main()
{
    //fp1;
    int n, m;
    while(scanf("%d", &n)==1 && n) {
        for(int i = 0;i < n;i++)
            cin >> s[i];
        clr(g);
        for(int i = 0;i < n;i++){
            for(int j = 0;j < n;j++){
                if(i == j) { g[i][j] = 0; continue; }
                else {
                    int sum = 0;
                    for(int k = 0;k < 7;k++){
                        if(s[i][k] != s[j][k]) sum++;
                    }
                    g[i][j] = g[j][i] = sum;
                }
            }
        }
        printf("The highest possible quality is 1/%d.\n", prim(g, n));
    }

    return 0;
}