本文主要是介绍prime(最小生成树)——POJ 1789,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
对应POJ题目:点击打开链接
Truck History
Time Limit:2000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u Submit Status Practice POJ 1789
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 t o is the original type and t d the type derived from it and d(t o,t d) 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.
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 Output
The highest possible quality is 1/3.
题意:就是给你一些车牌号,然后你通过比较两个车牌号相同位置字符不同的个数作为权值,再一个完全图的最小生成树算法。
#include<cstdio>
#include<cstdlib>
#include<cmath>
#include<map>
#include<queue>
#include<stack>
#include<vector>
#include<algorithm>
#include<iomanip>
#include<cstring>
#include<string>
#include<iostream>
const int MAXN=2000+10;
const int INF=1<<30;
using namespace std;
int G[MAXN][MAXN];
int cp[MAXN];
int dis[MAXN];
int sum;
char truck[MAXN][10];void prim(int n, int v)
{int i,j;for(i=0; i<n; i++){dis[i]=G[v][i];cp[i]=v;}for(i=1; i<n; i++){int min=INF;int k=v;for(j=0; j<n; j++){if(dis[j] && dis[j]<min){min=dis[j];k=j;}}//printf("%d <-> %d = %d\n", cp[k], k, min);//打印两顶点与两点间权值sum+=dis[k];//计算最短路径和dis[k]=0;//加入集合for(j=0; j<n; j++){if(G[j][k] && G[j][k]<dis[j]){//更新候选边dis[j]=G[j][k];cp[j]=k;}}}printf("The highest possible quality is 1/%d.\n", sum);
}int Distance(char const *str1, char const *str2)
{const char *p;const char *q;int cnt=0;for(p = str1, q = str2; *p && *q;)if(*p++ != *q++) cnt++;return cnt;
}int main()
{//freopen("in.txt","r",stdin);int n;while(scanf("%d", &n), n)//节点为0~n-1{sum=0;int i,j;for(i=0; i<n; i++) scanf("%s", truck[i]);for(i=0; i<n; i++){//初始化图for(j=0; j<n; j++){if(i==j){ G[i][j]=0; continue;}G[i][j]=INF;}}for(i=0; i<n; i++){for(j=i+1; j<n; j++){int len = Distance(truck[i], truck[j]);G[i][j] = G[j][i] = len;}}prim(n, 0);}return 0;
}
这篇关于prime(最小生成树)——POJ 1789的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!