poj 1789 kruscal水题
继续水水题。。。
题目:http://poj.org/problem?id=1789
把车看成结点,车之间的距离看作权重就是一个图了,然后求最小生成树。。。
的确水题,但看题目花了挺长时间,不知道如果现场遇到这种题目会多蛋疼。。。
这题时限2000ms,但用kruscal+sort写出来wa了,换用qsort的话果断超时了,翻了下网上的题解,发现貌似得用const void *传入参数才能快点。
修改完发现又wa了,然后gdb调了一遍,最后发现输出忘了个.。。。无语死。。。
额,后来是sort 1200+ms,qsort 600+ms。。。
贴代码(qsort):
#include <cstdio>
#include <algorithm>
using namespace std;
const int maxn = 2001;
//poj1789struct Edge{int x, y, v;
};
struct Edge e[maxn*maxn];
int f[maxn];void init(int n) {for (int i = 0; i < n; i++)f[i] = i;
}int find(int x) {if (f[x] != x)return f[x] = find(f[x]);return x;
}int chcmp(const char* a, const char * b) {int cnt = 0;for (int i = 0; i < 7; i++)if (a[i] != b[i])cnt++;return cnt;
}int cmp(const void * a, const void * b){return (*(struct Edge *)a).v - (*(struct Edge *)b).v;
}int main() {int n;int i, j, cnt, shortest, select;char str[maxn][7], tmp[7];
// freopen ("in", "r", stdin);while (scanf("%d", &n) && n) {init(n);gets(str[0]);for (i = 0; i < n; i++)gets(str[i]);cnt = 0;for (i = 0; i < n; i++)for (j = i + 1; j < n; j++) {e[cnt].x = i;e[cnt].y = j;e[cnt].v = chcmp(str[i], str[j]);cnt++;}qsort(e, cnt, sizeof(e[0]), cmp);shortest = 0;select = 0;for (i = 0; i < cnt; i++) {int a, b;a = find(e[i].x);b = find(e[i].y);if (a != b){f[a] = b;shortest += e[i].v;if (++select == n - 1)break;}}printf("The highest possible quality is 1/%d.\n", shortest);}return 0;
}
prim
poj 1789 kruscal水题相关推荐
- POJ 1003 Hangover 水题
题目看着挺复杂,还配了个看上去就很高大上的图,但是看完题就发现,完完全全是一道水题= =好吧,题意简单说就是有一排数,是1/2,1/2+1/3,1/2+1/3+1/4.......然后给出一个数,问这 ...
- POJ 2967 (水题,考察putchar()按位输入)
题目链接:http://poj.org/problem?id=2967 题目大意: 给定系列长度,求是否有规定的三角形 (不是所有的都可以组成三角形&&有至少一种组合可以组成三角形)- ...
- poj 2291/3637 水题排序
poj 2291题意:给你几根绳子,每根最大承重能力都不同,从中选出几根,问最大承重力量是多少?称重是选出绳子的最小值*根数(比如有1,10,15承重的3跟绳子,如果用一根的话,选15的,能承重15, ...
- POJ 1936 字符匹配(水题)
题目链接: http://poj.org/problem?id=1936 题目大意: 给定字符a,b,问b中去掉一些字符后能不能得到a 解题思路: 暴力从前往后扫描一遍即可. AC代码: /*** @ ...
- poj 3095 Linear Pachinko 模拟水题
题意: 给一个字符串,求小球随机放在上面进洞或到达两边之外的期望. 分析 水题,直接模拟. 代码: //poj 3095 //sep9 #include <iostream> using ...
- 水题/poj 1852 Ants
1 /* 2 PROBLEM:poj1852 3 AUTHER:Nicole 4 MEMO:水题 5 */ 6 #include<cstdio> 7 using namespace std ...
- poj 2388 排序的水题
纯纯的水题. #include <iostream> #include <fstream> #include <cstdlib>using namespace st ...
- POJ 2291水题
题意:给你几根绳子,每根最大承重能力都不同,从中选出几根,问最大承重力量是多少?(比如有1,10,15承重的3跟绳子,如果用一根的话,选15的,能承重15,用2根的话,用10和15的,能承重10*2= ...
- 图论刷水题记录(二)(最短路-----SPFA算法)
继第一篇的后续,又来刷水题了,写的是SPFA算法,这个算法的复杂度比较玄学,感觉能不用就不用了,但是他的好处就是可以判断负圈. 3月26日: 1.POJ 1847 Tram 题意:在一个交通网络上有N ...
最新文章
- 【转】Linux思维导图
- “惊群”,看看nginx是怎么解决它的
- .NET+IIS+MSSQL配置
- AD5933测量容性负载时的神秘振荡信号
- Round Robin 算法
- 【安全牛学习笔记】kali TOP10 安全工具:
- Linux学习之服务器搭建——DHCP服务器
- 3D游戏建模就是那么简单
- Pads logic 创建文件时发生严重错误
- Eclipse学习笔记——快捷键
- Unity中使用VideoPlayer控制播放暂停进度条
- 秒杀系统设计思路与实现
- 阿西莫夫机器人三定律或已过时?伯克利教授定义AI发展“新三原则”!
- 叶俊:别让“对抗”惯性摧毁你的幸福人生
- 部署测试fabric1.0及源码解析
- imx6ul移植madplay播放器
- springBoot配置全局异常处理器
- varbinary转换成字符串
- Python入门之控制结构-顺序与选择结构
- 用SQL语句备份SQL数据库