POJ【1789】 -- Truck History
2016-01-11 20:27
316 查看
Truck History
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
Sample Output
The highest possible quality is 1/3.
本题大意:求出不同字符串之间的distance(即不同的字母数)利用Prime或者kruskal求解此问题。
注意事项:使用邻接表存边注意将数组开到足够大,本题为4000000.
#include <cstdio>
#include <iostream>
#include <cstring>
using namespace std;
const int INF = 999999999;
const int maxn = 2005;
char str[maxn][10];
struct Edge
{
int from,to,dis,next;
};
Edge e[4000005];
int flag[maxn];
int d[maxn];
int pre[maxn];
int compare(int i,int j)
{
int sum = 0;
for(int k = 0;k < 7;k++)
if(str[i][k] != str[j][k])
sum++;
return sum;
}
int main()
{
int n,i,j;
while(scanf("%d",&n)!=EOF && n)
{
memset(pre,-1,sizeof(pre));
for(i = 0;i < n;i++)
scanf("%s",str[i]);
int m = 0;
for(i = 0;i < n-1;i++)
for(j = i+1 ; j < n; j++)
{
e[m].from = i;
e[m].to = j;
e[m].dis = compare(i,j);
e[m].next = pre[i];
pre[i] = m;
m++;
e[m].from = j;
e[m].to = i;
e[m].dis = compare(i,j);
e[m].next = pre[j];
pre[j] = m;
m++;
}
memset(flag,0,sizeof(flag));
flag[e[pre[0]].from] = 1;
for(i = 0;i < n;i++)
d[i] = INF;
for(i = pre[0];i != -1;i = e[i].next)
d[e[i].to] = e[i].dis;
int sum = 0;
for(i = 1 ; i < n; i++)
{
int x,minn = INF;
for(j = 0 ; j < n; j++)
if(!flag[j] && d[j] <= minn)
minn = d[x = j];
flag[x] = 1;
sum += d[x];
for(j = pre[x] ; j != -1; j = e[j].next)
{
if(!flag[e[j].to] && d[e[j].to] > e[j].dis)
d[e[j].to] = e[j].dis;
}
}
printf("The highest possible quality is 1/%d.\n",sum);
}
return 0;
}
Time Limit: 2000MS | Memory Limit: 65536K | |
Total Submissions: 22865 | Accepted: 8871 |
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.
本题大意:求出不同字符串之间的distance(即不同的字母数)利用Prime或者kruskal求解此问题。
注意事项:使用邻接表存边注意将数组开到足够大,本题为4000000.
#include <cstdio>
#include <iostream>
#include <cstring>
using namespace std;
const int INF = 999999999;
const int maxn = 2005;
char str[maxn][10];
struct Edge
{
int from,to,dis,next;
};
Edge e[4000005];
int flag[maxn];
int d[maxn];
int pre[maxn];
int compare(int i,int j)
{
int sum = 0;
for(int k = 0;k < 7;k++)
if(str[i][k] != str[j][k])
sum++;
return sum;
}
int main()
{
int n,i,j;
while(scanf("%d",&n)!=EOF && n)
{
memset(pre,-1,sizeof(pre));
for(i = 0;i < n;i++)
scanf("%s",str[i]);
int m = 0;
for(i = 0;i < n-1;i++)
for(j = i+1 ; j < n; j++)
{
e[m].from = i;
e[m].to = j;
e[m].dis = compare(i,j);
e[m].next = pre[i];
pre[i] = m;
m++;
e[m].from = j;
e[m].to = i;
e[m].dis = compare(i,j);
e[m].next = pre[j];
pre[j] = m;
m++;
}
memset(flag,0,sizeof(flag));
flag[e[pre[0]].from] = 1;
for(i = 0;i < n;i++)
d[i] = INF;
for(i = pre[0];i != -1;i = e[i].next)
d[e[i].to] = e[i].dis;
int sum = 0;
for(i = 1 ; i < n; i++)
{
int x,minn = INF;
for(j = 0 ; j < n; j++)
if(!flag[j] && d[j] <= minn)
minn = d[x = j];
flag[x] = 1;
sum += d[x];
for(j = pre[x] ; j != -1; j = e[j].next)
{
if(!flag[e[j].to] && d[e[j].to] > e[j].dis)
d[e[j].to] = e[j].dis;
}
}
printf("The highest possible quality is 1/%d.\n",sum);
}
return 0;
}
相关文章推荐
- Python 版桶排序
- 教你基带N9008ZMUDNB1 锁定2G /E 网 [转]
- RGB同步信号 DCLK/HS/VS/DE信号介绍
- Redis "MISCONF Redis is configured to save RDB snapshots, but is currently not able to persist on di
- nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
- Exercise1.1.22
- HDU 2089 不要62(挖个坑=-=)
- Office365-----Skype for business
- Yii的CSRF验证
- WebFrontEndStack
- mysql copy data from table to another
- python 获取探针页面,自动查询公司出口
- [Leetcode] 1.Two Sum @python
- iOS_视图控制对象生命周期以及区别和用途
- bind--C++11
- 原来mysql只能显示1000条数据
- 颜如玉——一月第2周
- 高性能Mysql主从架构的复制原理及配置详解
- Struts2的Convention插件
- [开发笔记]-未找到与约束ContractName Microsoft.VisualStudio.Text.ITextDocumentFactoryService...匹配的导出【转载自:酷小孩】