zoukankan      html  css  js  c++  java
  • POJ 1789 Truck History (Kruskal最小生成树) 模板题

    Description

    Advanced Cargo Movement, Ltd. uses trucks of different types. Some trucks are used for vegetable delivery, other for furniture, or for bricks. The company has its own code describing each type of a truck. The code is simply a string of exactly seven lowercase letters (each letter on each position has a very special meaning but that is unimportant for this task). At the beginning of company's history, just a single truck type was used but later other types were derived from it, then from the new types another types were derived, and so on. 

    Today, ACM is rich enough to pay historians to study its history. One thing historians tried to find out is so called derivation plan -- i.e. how the truck types were derived. They defined the distance of truck types as the number of positions with different letters in truck type codes. They also assumed that each truck type was derived from exactly one other truck type (except for the first truck type which was not derived from any other type). The quality of a derivation plan was then defined as 
    1/Σ(to,td)d(to,td)

    where the sum goes over all pairs of types in the derivation plan such that to is the original type and td the type derived from it and d(to,td) is the distance of the types. 
    Since historians failed, you are to write a program to help them. Given the codes of truck types, your program should find the highest possible quality of a derivation plan. 

    Input

    The input consists of several test cases. Each test case begins with a line containing the number of truck types, N, 2 <= N <= 2 000. Each of the following N lines of input contains one truck type code (a string of seven lowercase letters). You may assume that the codes uniquely describe the trucks, i.e., no two of these N lines are the same. The input is terminated with zero at the place of number of truck types.

    Output

    For each test case, your program should output the text "The highest possible quality is 1/Q.", where 1/Q is the quality of the best derivation plan.

    Sample Input

    4
    aaaaaaa
    baaaaaa
    abaaaaa
    aabaaaa
    0
    

    Sample Output

    The highest possible quality is 1/3.

    题意:每两个点之间都有一条带权边,求最小生成树。因为是稠密图, 所以其实是更适合prim的。。但是这题比较松,krusal也过了

    一共有n个点, 有n * n / 2 条边 ,(为防止溢出,要写成n / 2 * n),边数写错会RE

    因为是无向图,所以注意一下两个for循环的起点,这样边数才是上面的数量,不然万一写成n * n条边又RE了

    模板跑一遍,注意这里kruskal返回的是最小生成树的所有边的权值之和

    #include<cstdio>
    #include<cstring>
    #include<algorithm>
    
    typedef long long ll;
    using namespace std;
    const int maxn = 2000 + 100;//最大点数 
    const int maxm = maxn / 2 * maxn;//最大边数 
    int F[maxn];//并查集, F[x]储存x的父节点 
    int tol;
    char a[maxn][7];
    
    struct Edge{
    	int u, v, w;//储存每条边的两个端点以及边的权值 
    }edge[maxm];
    bool cmp(Edge a, Edge b){
    	return a.w < b.w;
    } 
    void addedge(int u, int v, int w){//加入最小生成树当中 
    	edge[tol].u = u;
    	edge[tol].v = v;
    	edge[tol++].w = w;
    }
    int find(int x){
    	if(F[x] == -1) return x;
    	else return F[x] = find(F[x]);
    }
    int kruskal(int n){//参数为点的数量 
        memset(F, -1, sizeof(F));//刚开始每一个点都是一个独立的连通图
    	int cnt = 0;//已经加入到最小生成树中的边 
        sort(edge, edge + tol, cmp);
        int ans = 0;//最小生成树的边的权值之和 
        for(int i = 0 ; i < tol; i++){
        	int u = edge[i].u;
        	int v = edge[i].v;
        	int w = edge[i].w;
        	int t1 = find(u), t2 = find(v);
        	if(t1 != t2){//u和v不在一个连通图中 
        		ans += w;
        		F[t1]  = t2;
        		cnt++;
    		}
    		if(cnt == n-1) break;
    	}
    	if(cnt < n-1) return -1;//不连通 
    	return ans;
    }
    int getw(int i, int j){
    	int qq = 0;
    	for(int k = 0; k <7; k++){
    		if(a[i][k] != a[j][k])qq++;
    	}
    	return qq;
    }
    int main(){
    	int n;
    	
    
        //下面建立最小生成树的来源图,即n ^2 /2条边的图
        while(scanf("%d", &n) && n){
    	    for(int i = 1; i <= n; i++){
    		    scanf("%s", a[i]);
        	}
        	tol = 0;
        	for(int i = 1; i <= n-1; i++)
        	    for(int j = i; j <= n; j++)addedge(i, j, getw(i, j));
        	printf("The highest possible quality is 1/%d.
    ", kruskal(n));
        }
    	return 0;
    }

    因为是多组数据,所以memset要放在while里面,不然会WA

  • 相关阅读:
    2010年第二批中关村高端领军人才公示公告
    关于代理的TIPS——HTTP权威指南读书心得(九)
    Lua 集成开发环境 Decoda 开源
    boost bind使用指南
    分享:反向代理服务器的工作原理
    javascript权威指南:javascript 变量
    Json简介
    css layout/fluid03: 2 column left navigation
    css layout/fluid01: 1 column no navigation
    Typeahead (autocomplete) suggest 开发
  • 原文地址:https://www.cnblogs.com/wrjlinkkkkkk/p/9552011.html
Copyright © 2011-2022 走看看