POJ 1789 -- Truck History

Prim求分母的最小。即求最小生成树

 #include<iostream>
#include<cstring>
#include<algorithm>
using namespace std;
const int maxn = + ;
const int INF = ;
int n;//有几个卡车
char str[maxn][];
int d[maxn];//记录编号的数值
int Edge[maxn][maxn];
int dist[maxn];
void prim()
{
int sum=;
//加入源点
dist[] = -;
for(int i=;i<n;i++)
{
dist[i] = Edge[][i];
} for(int i=;i<n;i++)//依次加入n-1条边
{
int min = INF,pos;
for(int j=;j<n;j++)//找出最小的一条
{
if(min>dist[j] && dist[j]!=-)
{
min = dist[j];pos = j;
}
}
//将pos加入
dist[pos] = -;sum+=min;
//进行路径的更新
for(int k=;k<n;k++)
{
if(dist[k] > Edge[pos][k])
dist[k]=Edge[pos][k];
}
}
cout<<"The highest possible quality is 1/"<<sum<<"."<<endl;
} int main()
{
while(cin>>n && n)
{
for(int i=;i<n;i++)
{
cin>>str[i];
}
for(int i=;i<n;i++)
{
int num=;
for(int k=;k<;k++)
num += (int)(str[i][k] - 'a');
d[i] = num;
}
memset(Edge,,sizeof(Edge));
for(int i=;i<n;i++)
{
for(int j=i+;j<n;j++)
{
int temp = ;
for(int k=;k<;k++)
{
temp += str[i][k]!=str[j][k];
}
Edge[i][j] = Edge[j][i] = temp;
}
}
memset(dist,INF,sizeof(dist));
prim(); } return ;
}

POJ 1789 -- Truck History(Prim)的更多相关文章

  1. POJ 1789 Truck History (最小生成树)

    Truck History 题目链接: http://acm.hust.edu.cn/vjudge/contest/124434#problem/E Description Advanced Carg ...

  2. POJ 1789 Truck History(Prim+邻接矩阵)

    ( ̄▽ ̄)" #include<iostream> #include<cstdio> #include<cstring> #include<algo ...

  3. poj 1789 Truck History(最小生成树)

    模板题 题目:http://poj.org/problem?id=1789 题意:有n个型号,每个型号有7个字母代表其型号,每个型号之间的差异是他们字符串中对应字母不同的个数d[ta,tb]代表a,b ...

  4. poj 1789 Truck History 最小生成树 prim 难度:0

    Truck History Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 19122   Accepted: 7366 De ...

  5. poj 1789 Truck History(kruskal算法)

    主题链接:http://poj.org/problem?id=1789 思维:一个一个点,每两行之间不懂得字符个数就看做是权值.然后用kruskal算法计算出最小生成树 我写了两个代码一个是用优先队列 ...

  6. Kuskal/Prim POJ 1789 Truck History

    题目传送门 题意:给出n个长度为7的字符串,一个字符串到另一个的距离为不同的字符数,问所有连通的最小代价是多少 分析:Kuskal/Prim: 先用并查集做,简单好写,然而效率并不高,稠密图应该用Pr ...

  7. POJ 1789:Truck History(prim&amp;&amp;最小生成树)

    id=1789">Truck History Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 17610   ...

  8. poj 1789 Truck History

    题目连接 http://poj.org/problem?id=1789 Truck History Description Advanced Cargo Movement, Ltd. uses tru ...

  9. POJ 1251 Jungle Roads (prim)

    D - Jungle Roads Time Limit:1000MS     Memory Limit:10000KB     64bit IO Format:%I64d & %I64u Su ...

随机推荐

  1. java-操作string的常用语法

    1.Java判断String是否以某个字符串开头: String mobile = "8618730600000";System.out.println(mobile.starts ...

  2. Oracle中函数关键字简介

    常用的语法:select--from--where--group by--having--order by 1.分组子句group by +con 按什么分组 2.having子句  对上面分组的数据 ...

  3. 批量kill指定名称的进程

     以Airflow举例: ps -ef | grep “airflow" | grep -v grep | cut -c 9-15 | xargs kill -9   分析: ps -ef  ...

  4. touchgfx MVP

  5. TouchGFX版本

    TouchGFX 4.12.3版本 本文概述了TouchGFX 4.12.3版本,其总体功能以及如何与CubeMX和CubeIDE集成. 总览 有关4.12.3新增功能的一般概述,请查看发行版中的ch ...

  6. Centos7.4安装RabbitMQ

    1.1 安装RabbitMQ 1.1.1 系统环境 [root@rabbitmq ~]# cat /etc/redhat-release CentOS Linux release 7.4.1708 ( ...

  7. Oracle数据库结构

    之前写了一篇文章<Oracle-知识结构漫谈> 粗略的介绍了Oracle数据库接口,在这里再更加详细的描述一下,当做是对原有知识的巩固,温故知新. Oracle体系结构数据库的体系结构是从 ...

  8. Java实验1 - 类的继承(super)- 创建checkaccount继承account

    笔记总结: /** 任务81: 继承性,(降低代码亢余度) * 1.class 子类A Extends 父类B,(private 的内容无法被继承) * 2. 方法可以覆盖(Overrides), 注 ...

  9. python : import详解。

    用户输入input() input()函数: 用于从标准输入读取数值. >>> message = input('tell me :') tell me :hahah >> ...

  10. python 上传多文件

    后台 import json from django.shortcuts import render,HttpResponse,HttpResponseRedirect import os impor ...