题目传送门

题意:给出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. 【转】Wireshark技巧-过滤规则和显示规则

    原文: http://www.cnblogs.com/icez/p/3973873.html ----------------------------------------------------- ...

  2. Deepin-安装laravel

    首先获取到composer.phar wget https://getcomposer.org/download/1.6.3/composer.phar 下载以后移动到目标区域 sudo mv com ...

  3. Zookeeper 简单操作

    1.  连接到zookeeper服务 [java2000_wl@localhost zookeeper-3]$ bin/zkCli.sh -server 127.0.0.1:2181 也可以连接远端的 ...

  4. React笔记

    React JS Tutorials for Beginners - 1 - Getting Started https://www.youtube.com/watch?v=-AbaV3nrw6E&a ...

  5. RabbitMQ通过shovel插件迁移数据

    前言 生产环境中会遇到RabbitMQ数据迁移的场景,例如:切换云服务厂商.不同Region之间数据迁移.新搭建RabbitMQ实例,数据需要同步至新的RabbitMQ实例. 前提条件: 源Rabbi ...

  6. 解决javah生成.h头文件找不到找不到android.support.v7.app.AppCompatActivity的问题

    问题描写叙述: 在使用Android Studio进行JNI开发时,须要使用javah生成C或C++的头文件,可是可能会遇到: 错误: 无法訪问android.support.v7.app.AppCo ...

  7. DLR之 ExpandoObject和DynamicObject的使用演示样例

    ExpandoObject :动态的增删一个对象的属性,在低层库(比如ORM)中非常实用.因为ExpandoObject实现了IDictionay<string, object>接口,常见 ...

  8. debian repository的成长过程

    1 基本概念 1.1 健康的安装 在端系统中的一次健康的安装指的是,在安装的包的集合中,所有的依赖都满足,并且没有冲突存在. 这的健康的安装是相对于端系统而言的,并不是相对于整个repo而言的.对整个 ...

  9. POJ2289 Jamie's Contact Groups —— 二分图多重匹配/最大流 + 二分

    题目链接:https://vjudge.net/problem/POJ-2289 Jamie's Contact Groups Time Limit: 7000MS   Memory Limit: 6 ...

  10. Struts2中怎样配置struts。xml?

    <?xml version="1.0" encoding="UTF-8" ?><!DOCTYPE struts PUBLIC "-/ ...