题目传送门

题意:给出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. ubuntu16.04LTS安装软件

    1.安装chrome 下载源加入到系统的源列表 sudo wget http://www.linuxidc.com/files/repo/google-chrome.list -P /etc/apt/ ...

  2. jquery在ajax新加入的元素后绑定事件click

    使用YII在做一个点击小图.能够在弹出窗体中显示大图的功能的时候,发现.GridView首页面的列表项按点击时一切正常,但按下了下一页后. 再点击小图,就不起作用了.原来,这是GridView使用了a ...

  3. DosBox 报错 this program requires dosxnt.exe to be in your path

    也就是少了dosxnt.exe文件,能够上网搜索下载,把dosxnt 拷贝到你挂截文件夹下就能够执行 Dosxnt文件下载

  4. .NET中lock的使用方法及注意事项[转]

    标准-->ms官方: http://msdn.microsoft.com/zh-cn/library/c5kehkcz(v=vs.90).aspx A.为什么不要 "lock(this ...

  5. codeforces 394E Lightbulb for Minister 简单几何

    题目链接:点我点我 题意:给定n个点. 以下n行给出这n个点坐标. 给定m个点,以下m行给出这m个点坐标. 这m个点是一个凸包,顺时针给出的. 问:在凸包上随意找一个点(x, y) 使得这个点距离n个 ...

  6. poj1840Eqs(哈希判重)

    题目链接: 传送门 思路: 这道题是一个简单的hash的应用,假设直接暴力的话肯定承受不了5重for循环,所以比赛的时候我先到分成两组.可是后来用到了很多数组,然后想到数字太大,还先到stl判重, 后 ...

  7. SQL Server 存储过程具体解释

    SQL Server 存储过程具体解释 存储过程的优缺点 ◆长处: 运行速度更快. 存储过程仅仅在创造时进行编译,而一般SQL语句每运行一次就编译一次,所以使用存储过程运行速度更快. 存储过程用于处理 ...

  8. 2016/3/17 Mysq select 数学函数 字符串函数 时间函数 系统信息函数 加密函数

    一,数学函数主要用于处理数字,包括整型.浮点数等. ABS(X) 返回x的绝对值 SELECT ABS(-1)--返回1 CEll(X),CEILING(x)  返回大于或等于x的最小整数 SELEC ...

  9. 设备没有可用空间 /var/spool/clientmqueue sendmail

    [root@hadoop3 /]# crontab -e/tmp/crontab.TB7A7w: 设备上没有空间[root@hadoop3 /]# df -Bg文件系统 1G-块 已用 可用 已用% ...

  10. 深度解析Struts2中ValueStack

    1.什么是ValueStack 对于每一个action的调用,Struts在执行相应的动作方法之前会先创建一个名为ValueStack的对象.Value Stack用来保存该动作对象或者对象.由于最终 ...