标签:des style blog http color os
| Time Limit: 2000MS | Memory Limit: 65536K | |
| Total Submissions: 17610 | Accepted: 6786 |
Description
Input
Output
Sample Input
4 aaaaaaa baaaaaa abaaaaa aabaaaa 0
Sample Output
The highest possible quality is 1/3.
用一个7位的string代表一个编号,两个编号之间的distance代表这两个编号之间不同字母的个数。
一个编号只能由另一个编号“衍生”出来,代价是这两个编号之间相应的distance,
现在要找出一个“衍生”方案,使得总代价最小,也就是distance之和最小。
此题的关键是将问题转化为最小生成树的问题。
每一个编号为图的一个顶点,顶点与顶点间的编号差即为这条边的权值,题目所要的就是我们求出最小生成树来。
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
#include<vector>
#include<queue>
#define INF 0x3f3f3f
using namespace std;
const int maxn = 2000 + 50;
int f[maxn];
int map[maxn][maxn];
bool vist[maxn];
char str[maxn][10];
int ans[maxn];
int n;
int find(int x, int y)
{
int cnt = 0;
for(int i=0; i<7; i++)
if( str[x][i]!=str[y][i] )
cnt++;
return cnt;
}
void init()
{
memset( map, 0, sizeof(map) );
memset( vist, false, sizeof(vist) );
memset( ans, 0, sizeof(ans) );
for(int i=0; i<n; i++)
scanf( "%s", str[i] );
//for(int i=0; i<n; i++)
// printf("%s\n", str[i]);
for(int i=0; i<n; i++)
for(int j=0; j<=i; j++)
map[i][j] = map[j][i] = find(i, j);
}
void prim()
{
int minc, mind;
vist[0] = true;
ans[0] = 0;
for(int i=1; i<n; i++)
ans[i] = map[0][i];
for(int j=0; j<n-1; j++)
{
minc = INF;
for(int i=0; i<n; i++)
{
if( !vist[i] && minc>ans[i] )
{
minc = ans[i];
mind = i;
}
}
if(minc != INF)
{
vist[mind] = true;
for(int i=0; i<n; i++)
if( !vist[i] && ans[i]>map[mind][i] )
ans[i] = map[mind][i];
}
}
}
void output()
{
int sum = 0;
for(int i=1; i<n; i++)
sum += ans[i];
printf("The highest possible quality is 1/%d.\n", sum);
}
int main()
{
while( scanf( "%d", &n )==1 &&n )
{
init();
prim();
output();
}
return 0;
}
POJ 1789:Truck History(prim&&最小生成树),布布扣,bubuko.com
POJ 1789:Truck History(prim&&最小生成树)
标签:des style blog http color os
原文地址:http://blog.csdn.net/u013487051/article/details/37989795