Truck History(卡车历史)

Time Limit: 2000MS Memory Limit: 65536K
Total Submissions: 26547 Accepted: 10300

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. 

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.

Source

题意:
给你一个n;
n个长为7的字符串;
每个字符串表示一个节点,每个节点向其他所有点都有边,边长为两个节点字符串同一位置不同字符的数量;
需要你生成最短路的边权和。
代码实现(prim):
 1 #include<cstdio>
 2 #include<cstring>
 3 #include<iostream>
 4 using namespace std;
 5 const int maxn=2010;
 6 const int inf=maxn*7;
 7 int n;
 8 int a[maxn],map[maxn][maxn];
 9 char ch[maxn][10];
10 bool v[maxn];
11 int bj(int x,int y){
12     int ret=0;
13     for(int i=0;i<7;i++)
14     if(ch[x][i]!=ch[y][i]) ret++;
15     return ret;
16 }
17 void intn(){
18     for(int i=1;i<=n;i++) scanf("%s",ch[i]);
19     for(int i=1;i<=n;i++)
20     for(int j=i+1;j<=n;j++)
21     map[j][i]=map[i][j]=bj(i,j);
22 }
23 int prim(){
24     memset(v,0,sizeof(v));
25     int ans=0,p,b;
26     v[1]=1;
27     for(int i=1;i<=n;i++) a[i]=map[1][i];
28     for(int m=1;m<n;m++){
29         p=inf;
30         for(int i=1;i<=n;i++) if(a[i]<p&&!v[i]){p=a[i];b=i;}
31         ans+=a[b];v[b]=1;
32         for(int i=1;i<=n;i++) a[i]=min(a[i],map[b][i]);
33     }
34     return ans;
35 }
36 int main(){
37     while(scanf("%d",&n)){
38         if(!n) break;
39         else intn();
40         printf("The highest possible quality is 1/%d.\n",prim());
41     }
42     return 0;
43 }

之前的prim打的丑,poj一直给我TLE(只有TLE),果然poj逼格这么高的地方不适合我这样不会英语,又很娇弱的蒟蒻。

题目来源:POJ

转载于:https://www.cnblogs.com/J-william/p/6371718.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,让我们来写一个简单的Java卡车Truck吧! 首先,我们需要定义Truck类并声明一些属性。一个卡车通常有以下属性: - 车牌号码 - 品牌 - 载重量 - 油箱容量 - 当前油量 那么我们可以这样定义Truck类: ```java public class Truck { private String licensePlate; private String brand; private double loadCapacity; private double tankCapacity; private double fuelLevel; // 构造方法 public Truck(String licensePlate, String brand, double loadCapacity, double tankCapacity) { this.licensePlate = licensePlate; this.brand = brand; this.loadCapacity = loadCapacity; this.tankCapacity = tankCapacity; this.fuelLevel = 0; // 初始油量为0 } // getter和setter方法 public String getLicensePlate() { return licensePlate; } public String getBrand() { return brand; } public double getLoadCapacity() { return loadCapacity; } public double getTankCapacity() { return tankCapacity; } public double getFuelLevel() { return fuelLevel; } public void setFuelLevel(double fuelLevel) { this.fuelLevel = fuelLevel; } } ``` 现在,我们已经定义了一个简单的Truck类,其中包含了该类的属性以及getter和setter方法。接下来,我们可以添加一些方法来模拟卡车的行为,例如加油、行驶等。以下是一个简单的例子: ```java public class Truck { // ... // 加油方法 public void refuel(double fuel) { double totalFuel = fuelLevel + fuel; if (totalFuel > tankCapacity) { System.out.println("加油失败,油箱容量不足!"); } else { fuelLevel = totalFuel; System.out.println(String.format("成功加入 %.2f 升油,当前油量为 %.2f 升。", fuel, fuelLevel)); } } // 行驶方法 public void drive(double distance) { double fuelConsumption = distance / 100 * 10; // 每百公里消耗10升油 if (fuelConsumption > fuelLevel) { System.out.println("行驶失败,油量不足!"); } else { fuelLevel -= fuelConsumption; System.out.println(String.format("成功行驶 %.2f 公里,消耗 %.2f 升油,剩余油量为 %.2f 升。", distance, fuelConsumption, fuelLevel)); } } } ``` 现在,我们已经添加了加油和行驶方法,可以使用它们来模拟卡车的行为了。例如: ```java Truck myTruck = new Truck("京A12345", "Foton", 10, 100); myTruck.refuel(50); // 加入50升油 myTruck.drive(500); // 行驶500公里 ``` 以上就是一个简单的Java卡车Truck的实现。当然,这只是一个例子,实际开发中可能需要考虑更多的属性和方法。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值