kuangbin_MST A (POJ 1251)
模板题 Kruskal直接过 调试时候居然在sort(edge + 1, edge + 1 + m)上浪费好多时间...
不过本着ACMer的心态自然要测试一下两种方法分别的速度
Kruskal : Memory 392 Time 16
Prim: Memory 576 Time 16
...........哦居然一样啊
Prim
#include <cstdio>
#include <cstring>
#include <queue>
#include <vector>
#include <algorithm>
#define INF 0x3F3F3F3F
using namespace std; typedef pair<int, int> pii;
struct cmp{
bool operator() (const pii a, const pii b){
return a.first > b.first;
}
}; int size, head[], point[], nxt[], val[];
int n, dist[]; void init()
{
size = ;
memset(head, - ,sizeof head);
} void add(int from, int to, int value)
{
val[size] = value;
point[size] = to;
nxt[size] = head[from];
head[from] = size++;
} int prim(int s)
{
int ans = ;
bool vis[];
memset(dist, 0x3f, sizeof dist);
memset(vis, false, sizeof vis); priority_queue<pii, vector<pii>, cmp> q;
for(int i = head[s]; ~i; i = nxt[i]){
dist[point[i]] = val[i];
q.push(make_pair(val[i], point[i]));
}
dist[s] = ;
vis[s] = true;
while(!q.empty()){
pii u = q.top();
q.pop();
if(vis[u.second]) continue;
vis[u.second] = true;
ans += u.first;
for(int i = head[u.second]; ~i; i = nxt[i]){
int j = point[i];
//printf("check edge[%d][%d] vis = %d value = %d\n", u.second, j, vis[j], val[i]);
if(!vis[j] && dist[j] > val[i]){
//printf("add edge[%d][%d]\n", u.second, j);
dist[j] = val[i];
q.push(make_pair(dist[j], j));
}
}
}
return ans;
} int main()
{
while(scanf("%d", &n), n){
init();
for(int i = ; i < n; i++){
int k;
scanf("%*s%d", &k);
while(k--){
char to[];
int value;
scanf("%s%d", to, &value);
add(i, to[]-'A'+, value);
add(to[]-'A'+, i, value);
}
}
printf("%d\n", prim());
}
return ;
}
Kruskal
#include <cstdio>
#include <cstring>
#include <queue>
#include <vector>
#include <algorithm>
#define INF 0x3F3F3F3F
using namespace std; int n, m;
int parent[], rank[];
struct Edge{int u, v, w;}edge[]; bool cmp(Edge a, Edge b)
{
return a.w < b.w;
} inline int FindSet(int x)
{
while(x != parent[x]) x = parent[x];
return x;
} void UnionSet(int l, int r)
{
int x = FindSet(l);
int y = FindSet(r);
if(rank[x] > rank[y]){
rank[x] += rank[y];
parent[y] = x;
}
else{
rank[y] += rank[x];
parent[x] = y;
}
} int kruskal()
{
for(int i = ; i <= n; i++){
parent[i] = i;
rank[i] = ;
}
int ans = ;
sort(edge+, edge+m+, cmp);
for(int i = , num = ; num < n- && i <= m; i++){
int x = FindSet(edge[i].u);
int y = FindSet(edge[i].v);
if(x != y){
num++;
UnionSet(x, y);
ans += edge[i].w;
}
}
return ans;
} int main()
{
while(scanf("%d", &n), n){
m = ;
for(int i = ; i < n; i++){
int k;
scanf("%*s%d", &k);
while(k--){
m++;
edge[m].u = i;
char v[];
scanf("%s%d", v, &edge[m].w);
edge[m].v = v[] - 'A' + ;
//printf("Edge %d : u = %d v = %d w = %d\n", m, edge[m].u, edge[m].v, edge[m].w);
}
}
printf("%d\n", kruskal());
}
return ;
}
kuangbin_MST A (POJ 1251)的更多相关文章
- poj 1251 Jungle Roads (最小生成树)
poj 1251 Jungle Roads (最小生成树) Link: http://poj.org/problem?id=1251 Jungle Roads Time Limit: 1000 ...
- POJ 1251 Jungle Roads (prim)
D - Jungle Roads Time Limit:1000MS Memory Limit:10000KB 64bit IO Format:%I64d & %I64u Su ...
- poj 1251 poj 1258 hdu 1863 poj 1287 poj 2421 hdu 1233 最小生成树模板题
poj 1251 && hdu 1301 Sample Input 9 //n 结点数A 2 B 12 I 25B 3 C 10 H 40 I 8C 2 D 18 G 55D 1 E ...
- Poj(1251),Prim字符的最小生成树
题目链接:http://poj.org/problem?id=1251 字符用%s好了,方便一点. #include <stdio.h> #include <string.h> ...
- OpenJudge/Poj 1251 丛林中的路/Jungle Roads
1.链接地址: http://bailian.openjudge.cn/practice/1251/ http://poj.org/problem?id=1251 2.题目: 总时间限制: 1000m ...
- (最小生成树) Jungle Roads -- POJ -- 1251
链接: http://poj.org/problem?id=1251 Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 2177 ...
- POJ 1251 Jungle Roads (zoj 1406) MST
传送门: http://poj.org/problem?id=1251 http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=406 P ...
- POJ 1251 Jungle Roads - C语言 - Kruskal算法
Description The Head Elder of the tropical island of Lagrishan has a problem. A burst of foreign aid ...
- poj 1251 统计难题(字典树)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1251 AC代码: #include<iostream> #include<algor ...
随机推荐
- uart与usart
字面意义:UART:universal asynchronous receiver and transmitter通用异步收发器:USART:universal synchronous asynchr ...
- UNICODE字符集(20140520)
1多字节字符集,如"IT学吧",sizeof内存长度为7,因为前面2个字母各占用一个字节,后面两个汉字各占用2个字节,结尾的\0占用一个字节.strlen即字符串长度的结果为6. ...
- UIkit框架之UIimage
1.继承链:NSObject 2.以下有三种方法来创建图片对象 (1) imageNamed:inBundle:compatibleWithTraitCollection:从image asset或者 ...
- 【LeetCode】172. Factorial Trailing Zeroes
Factorial Trailing Zeroes Given an integer n, return the number of trailing zeroes in n!. Note: Your ...
- yii2归档安装
1.http://www.yiiframework.com/download/ 下载文件 2.如果inint.bat文件一闪而过,没有提示是开发还是生产环境 用编辑器(phpstorm)打开文件在对应 ...
- iOS事件:触摸事件.运动事件.远程控制事件
iOS中,提供了事件处理:触摸事件,运动事件,远程控制事件.这很大得方便程序猿的工作. 这里先简单做个介绍: // // ViewController.m // demo // // Created ...
- C#处理Json文件
JSON(全称为JavaScript Object Notation) 是一种轻量级的数据交换格式.它是基于JavaScript语法标准的一个子集. JSON采用完全独立于语言的文本格式,可以很容易在 ...
- How to migrate from VMware and Hyper-V to OpenStack
Introduction I migrated >120 VMware virtual machines (Linux and Windows) from VMware ESXi to Open ...
- CentOS安装Xen
1.服务器环境及Xen版本: CentOS 5.4 64bit Xen-3.4.3,已经自带安装包 2.自制本地yum源: 安装httpd,指向本地xen yum源 3.修改yum.repo使其指向本 ...
- Windows 10下Chrome不能启动的问题
不能启动了 Chrome突然不能启动了,点击图标也没反应,打开任务管理器,发现点击图标后,Google Ghrome短暂地出现,随即消失. 如何解决? 解决方案 打开安装目录: C:\Program ...