F - Truck History(最小生成树)

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.

题解:

最小生成树Kruskal算法超时
1.Prim在稠密图中比Kruskal优,在稀疏图中比Kruskal劣。
所以我们应该使用prim算法。
一开始使用vector存储string,一直超时,后来改用字符串数组400多Ms

代码

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
using namespace std;

const int maxn = 2005;

const int INF = 0x3f3f3f3f;
bool vis[maxn];
int lowc[maxn];
int cost[maxn][maxn];

char str[maxn][8];

void update(int x,int y,int v)
{
    cost[x][y] = v;
    cost[y][x] = v;
    return ;
}

int Len(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 Prim(int cost[][maxn],int n)
{
    int ans=0;
    memset(vis,false,sizeof(vis));
    vis[0]=true;
    for(int i=1;i<n;i++)
    lowc[i]=cost[0][i];
    for(int i=1;i<n;i++)
    {
        int minc=INF;
        int p=-1;
        for(int j=0;j<n;j++)
        {
            if(!vis[j]&&minc>lowc[j])
            {
                minc = lowc[j];
                p = j;
            }
        }
        if(minc==INF) return -1;
        ans+=minc;
        vis[p]=true;
        for(int j=0;j<n;j++)
        {
            if(!vis[j]&&lowc[j]>cost[p][j])
            {
                lowc[j]=cost[p][j];
            }
        }
    }
    return ans;
}


int main()
{
    int n;
    while((scanf("%d",&n)!=EOF)&&n)
    {

        for(int i=0;i<n;i++)
        {
            scanf("%s",str[i]);
        }

        for(int i=0;i<n;i++)
            for(int j=i+1;j<n;j++)
        {
            int cost = Len(i,j);
            update(i,j,cost);
        }

        int ans = Prim(cost,n);
        printf("The highest possible quality is 1/%d.\n", ans);

    }
    return 0;
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
动态卡车-无人机路径规划问题是指在计划交付和即时需求的情况下,如何有效安排卡车和无人机的路径以完成交付任务。 在这个问题中,动态卡车-无人机路径规划需要考虑到多个因素。首先,需要根据交付任务的性质和需求类型确定使用卡车还是无人机。根据交付任务的时间要求和优先级,可以将任务分配给卡车或无人机,以最大程度地提高交付效率。 其次,需要根据交付点的位置和距离进行路径规划。通过算法和模型,可以确定卡车和无人机之间的最佳配送路径,以最小化总体成本和交付时间。这可能包括选择最短路径、减少交通堵塞和选择合适的送货点等策略。 然后,需要考虑资源利用效率。可以通过优化装载和卸载过程,使卡车和无人机的载重能力最大化,以减少行程次数和成本。同时,还可以考虑车辆和无人机的能源消耗,通过合理规划路线和使用动态调度算法来降低能源消耗。 最后,需要考虑实时任务调度和监控。根据任务和道路状况的实时变化,可以动态调整路径和分配任务,以适应不同的需求和情况。同时,应建立有效的监控系统,实时追踪卡车和无人机的位置和交付进度,以及解决可能出现的问题和异常情况。 综上所述,动态卡车-无人机路径规划问题是一个复杂的优化问题,在解决过程中需要考虑到任务类型、路径规划、资源利用效率和实时调度等关键因素,以提高交付效率和降低成本。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值