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 ...
随机推荐
- 利用WiFi Pineapple Nano渗透客户端获取SHELL
前言: 前两篇文章介绍了The WiFi Pineapple Nano设备的一些主要功能模块,例如PineAP.SSLsplit和Ettercap等.今天给大家实际场景演示下如何利用Pineapple ...
- 新浪微博发送消息和授权机制原理(WeiboSDK)
1.首先是在微博发送消息,对于刚開始做weibo发送消息的刚開始学习的人会有一个误区,那就是会觉得须要授权后才干够发送消息.事实上发送消息仅仅须要几行代码就能够实现了,很easy,不须要先授权再发送消 ...
- mtk刷机错误汇总
MTK常见错误解读与解决方法: 1.刷机过了红条,到了紫色条卡住.(错误代码4008) 解决方法:这种情况出现的话,大家可以把电池拿下来,然后重新安装上,进入REC后选择关机.然后重新刷. 2.驱动安 ...
- LoadRunner系列之—-04 录制基于https协议的脚本
实际性能测试过程中,有些需录制脚本的页面或接口是基于https协议的,按原来方法录制脚本,录完了脚本是空的.为解决这个问题,第一步了解https协议的具体实现,这块网上资料很多,可参考页面下方参考资料 ...
- .NET中lock的使用方法及注意事项[转]
标准-->ms官方: http://msdn.microsoft.com/zh-cn/library/c5kehkcz(v=vs.90).aspx A.为什么不要 "lock(this ...
- 李洪强iOS开发之录音和播放实现
李洪强iOS开发之录音和播放实现 //首先导入框架后,导入头文件.以下内容为托控件,在storyboard中拖出两个按钮为录音和播放按钮 //创建一个UIViewController在.h文件中写 # ...
- JavaScript基础教程复习笔记
document.write("<h1>这是一个标题</h1>"); 您只能在 HTML 输出中使用 document.write.如果您在文档加载后使用该 ...
- SQL外键的作用
貌似很有用,但没有真正用过: SQL的主键和外键的作用: 外键取值规则:空值或参照的主键值. (1)插入非空值时,如果主键表中没有这个值,则不能插入. (2)更新时,不能改为主键表中没有的值. (3) ...
- ubuntu LNMP环境下安装Redis,以及php的redis扩展
1.下载 sudo wget http://download.redis.io/releases/redis-4.0.9.tar.gz 2.解压 sudo tar zvxf redis-4.0.9.t ...
- IOS应用在iPhone5和iPhone5s上不能全屏显示,应用画面上下各有1条黑色的解决方案
设置启动图片就可以了: 添加启动图: 接着设置为启动图: 这样就ok了