Kuskal/Prim POJ 1789 Truck History
题意:给出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的更多相关文章
- POJ 1789 -- Truck History(Prim)
POJ 1789 -- Truck History Prim求分母的最小.即求最小生成树 #include<iostream> #include<cstring> #incl ...
- poj 1789 Truck History
题目连接 http://poj.org/problem?id=1789 Truck History Description Advanced Cargo Movement, Ltd. uses tru ...
- poj 1789 Truck History 最小生成树 prim 难度:0
Truck History Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 19122 Accepted: 7366 De ...
- POJ 1789 Truck History【最小生成树简单应用】
链接: http://poj.org/problem?id=1789 http://acm.hust.edu.cn/vjudge/contest/view.action?cid=22010#probl ...
- POJ 1789 Truck History (Kruskal)
题目链接:POJ 1789 Description Advanced Cargo Movement, Ltd. uses trucks of different types. Some trucks ...
- poj 1789 Truck History 最小生成树
点击打开链接 Truck History Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 15235 Accepted: ...
- POJ 1789 Truck History (最小生成树)
Truck History 题目链接: http://acm.hust.edu.cn/vjudge/contest/124434#problem/E Description Advanced Carg ...
- poj 1789 Truck History【最小生成树prime】
Truck History Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 21518 Accepted: 8367 De ...
- POJ 1789 Truck History(Prim+邻接矩阵)
( ̄▽ ̄)" #include<iostream> #include<cstdio> #include<cstring> #include<algo ...
随机推荐
- Deepin-安装(读写文件)权限
在安装NODE管理模块N时,遇到了权限问题 1.给予程序读写权限(仅限文件夹) 查看权限:ls -l 或 ls 添加权限: 示例:chmod +rw xx 实例:chmod +rw node 关于权限 ...
- js实现的美女瀑布流效果代码
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/stri ...
- 把A表中的a字段和b字段数据 复制到B表中的aa字段和bb字段
insert into tab2 (column1,column2) select column1,column2 from tab1
- 视频录制软件&远程支持软件
视频录制软件 软件名:SCREEN2SWF 录制完成后,可以剪辑: 工程文件,需要保存为svp文件:将视频文件保存为.exe self play 文件,或者.swf flash 文件. 远程支持,远程 ...
- windows下的两个等待函数
windows下的两个等待技术 第一种: Win32 Sleep()函数 这个函数要求操作系统中止线程动作.直到读过某个指定的时间之后才恢复.能在某个线程结束时(而不是某段时间结束时)被调 ...
- Java 中 泛型的限定
泛型 一般 出如今集合中,迭代器中 也会出现! 泛型 是为了 提高代码的 安全性. 泛型 确保数据类型的唯一性. 在我们经常使用的容器中. 越是 单一 约优点理啊! ...
- redis缓存数据库的详解
1,什么是redis? Redis是完全开源免费的,遵守BSD协议,是一个高性能的key-value数据库 Redis与其他key-value缓存产品有以下三个特点: Redis支持数据的持久化,可以 ...
- 请说出作用域public,private,protected,以及不写时的区别
这四个作用域的可见范围如下表所示. 说明:如果在修饰的元素上面没有写任何访问修饰符,则表示friendly. 作用域 当前类 同一package 子孙类 其他package public ...
- form之action的绝对路径与相对路径(转载)
1.当你的form要提交到你自己的站点之外的URL的时候,就采取绝对路径: <form action="http://www.xxx.yyy:zzzz/mmm/nn/kkk.jsp&q ...
- What's the difference between jquery.js and jquery.min.js?
They are both the same functionally but the .min one has all unnecessary characters removed in order ...