题目传送门

题意:给出n个长度为7的字符串,一个字符串到另一个的距离为不同的字符数,问所有连通的最小代价是多少

分析:Kuskal/Prim: 先用并查集做,简单好写,然而效率并不高,稠密图应该用Prim而且要用邻接矩阵,邻接表的效率也不高。裸题但题目有点坑爹:(

Kruskal:

#include <cstdio>
#include <cstring>
#include <string>
#include <algorithm>
#include <iostream>
#include <cmath>
using namespace std; const int MAXN = 2e3 + 10;
const int INF = 0x3f3f3f3f;
struct UF {
int rt[MAXN], rk[MAXN];
void init(void) {
memset (rt, -1, sizeof (rt));
memset (rk, 0, sizeof (rk));
}
int Find(int x) {
return (rt[x] == -1) ? x : rt[x] = Find (rt[x]);
}
void Union(int x, int y) {
x = Find (x), y = Find (y);
if (x == y) return ;
if (rk[x] > rk[y]) {
rt[y] = x; rk[x] += rk[y] + 1;
}
else {
rt[x] = y; rk[y] += rk[x] + 1;
}
}
bool same(int x, int y) {
return Find (x) == Find (y);
}
}uf;
char s[MAXN][10];
struct Node
{
int u, v, w;
}node[MAXN*MAXN/2];
int n, tot; bool cmp(Node x, Node y) {return x.w < y.w;} void get_w(int x)
{
for (int i=1; i<x; ++i)
{
int res = 0;
for (int j=0; j<7; ++j)
{
if (s[i][j] != s[x][j]) res++;
}
node[++tot].u = i; node[tot].v = x; node[tot].w = res;
}
} int main(void) //POJ 1789 Truck History
{
// freopen ("POJ_1789.in", "r", stdin); while (scanf ("%d", &n) == 1)
{
if (n == 0) break; tot = 0;
for (int i=1; i<=n; ++i)
{
scanf ("%s", s[i]);
get_w (i);
}
sort (node+1, node+1+tot, cmp); int ans = 0; uf.init ();
for (int i=1; i<=tot; ++i)
{
int u = node[i].u; int v = node[i].v; int w = node[i].w;
if (!uf.same (u, v)) {uf.Union (u, v); ans += w;}
} printf ("The highest possible quality is 1/%d.\n", ans);
} return 0;
}

Prim:

/*
模版搞错了,纠结半天。。。算法还是想清楚才行啊
*/
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <string>
#include <iostream>
#include <vector>
#include <queue>
using namespace std; const int MAXN = 2e3 + 10;
const int INF = 0x3f3f3f3f;
int d[MAXN], w[MAXN][MAXN];
bool vis[MAXN];
int n;
char s[MAXN][10]; int Prim(int s) {
memset (vis, false, sizeof (vis));
memset (d, INF, sizeof (d)); d[s] = 0;
int ret = 0;
for (int i=1; i<=n; ++i) {
int mn = INF, u = -1;
for (int i=1; i<=n; ++i) {
if (!vis[i] && d[i] < mn) mn = d[u=i];
}
if (u == -1) return -1;
vis[u] = true; ret += d[u];
for (int i=1; i<=n; ++i) {
if (!vis[i] && d[i] > w[u][i]) {
d[i] = w[u][i];
}
}
}
return ret;
} void get_w(int x) {
for (int i=1; i<x; ++i) {
int res = 0;
for (int j=0; j<7; ++j) {
if (s[i][j] != s[x][j]) res++;
}
w[i][x] = w[x][i] = res;
}
} int main(void) {
while (scanf ("%d", &n) == 1) {
if (n == 0) break;
memset (w, INF, sizeof (w));
for (int i=1; i<=n; ++i) {
scanf ("%s", s[i]); get_w (i);
}
printf ("The highest possible quality is 1/%d.\n", Prim (1));
} return 0;
}

Kuskal/Prim POJ 1789 Truck History的更多相关文章

  1. POJ 1789 -- Truck History(Prim)

     POJ 1789 -- Truck History Prim求分母的最小.即求最小生成树 #include<iostream> #include<cstring> #incl ...

  2. poj 1789 Truck History

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

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

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

  4. POJ 1789 Truck History【最小生成树简单应用】

    链接: http://poj.org/problem?id=1789 http://acm.hust.edu.cn/vjudge/contest/view.action?cid=22010#probl ...

  5. POJ 1789 Truck History (Kruskal)

    题目链接:POJ 1789 Description Advanced Cargo Movement, Ltd. uses trucks of different types. Some trucks ...

  6. poj 1789 Truck History 最小生成树

    点击打开链接 Truck History Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 15235   Accepted:  ...

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

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

  8. poj 1789 Truck History【最小生成树prime】

    Truck History Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 21518   Accepted: 8367 De ...

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

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

随机推荐

  1. 202. Segment Tree Query

    最后更新 二刷 09-Jan-17 正儿八经线段树的应用了. 查找区间内的值. 对于某一个Node,有这样的可能: 1)需要查找区间和当前NODE没有覆盖部分,那么直接回去就行了. 2)有覆盖的部分, ...

  2. Swift简单介绍 教程

     Swift是什么? Swift是苹果于WWDC 2014公布的编程语言.这里引用The Swift Programming Language的原话: Swift is a new programmi ...

  3. 微信小程序 自定义组件(stepper)

    项目目录: 步骤一:创建组件 声明这一组文件为自定义组件 stepper.json { "component": true, "usingComponents" ...

  4. Hashmap在JDK8中的提升

    HashMap使用key的hashCode()和equals()方法来将值划分到不同的桶里. 桶的数量通常要比map中的记录的数量要稍大.这样 每一个桶包含的值会比較少(最好是一个).当通过key进行 ...

  5. unix时间戳(unix timestamp)与北京时间的互转方法

    1.在linux bash下北京时间与unix时间戳互转: 获取unix timestamp: 命令:date "+%s" 输出:1372654714 获取北京时间: 命令:dat ...

  6. crm使用soap删除下拉框

    //C# 代码: //DeleteOptionSetRequest request = new DeleteOptionSetRequest(); //request.Name = "new ...

  7. PHP开发者实用的代码

    一.查看邮件是否已被阅读 当你在发送邮件时,你或许很想知道该邮件是否被对方已阅读.这里有段非常有趣的代码片段能够显示对方IP地址记录阅读的实际日期和时间. <? error_reporting( ...

  8. android各种菜单使用介绍

    Android菜单的有这几种: 1,OptionMenue:选项菜单 2,contextMenu:上下文菜单 3,SubMenu子菜单 其中,OptionMenue与contextMenu的区别(Op ...

  9. 远程调试 Asp.Net 项目

    项目部署到产品环境后,难免会发生一些故障,有一些可以在本地测试环境中直接重现,而有一些则无法重现.对于可以在本地测试环境中重现的Bug,开发人员往往能够很迅速地进行问题排查.而对于无法重现的Bug,就 ...

  10. Cron Expression

    CronTrigger CronTriggers往往比SimpleTrigger更有用,如果您需要基于日历的概念,而非SimpleTrigger完全指定的时间间隔,复发的发射工作的时间表.CronTr ...