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 ...
随机推荐
- 迭代器(Iterator)模式
转自:http://blog.csdn.net/lilu_leo/article/details/7609496 概述 迭代器(Iterator)模式,又叫做游标(Cursor)模式.GOF ...
- SharePoint 2013 Nintex Workflow 工作流帮助(四)
博客地址 http://blog.csdn.net/foxdave 接上篇点击打开链接 我们来看下一个配置标签Ribbon Option: Task Notification(用于配置分配任务时的提醒 ...
- Linux 上的常用文件传输方式介绍与比较
ftp ftp 命令使用文件传输协议(File Transfer Protocol, FTP)在本地主机和远程主机之间或者在两个远程主机之间进行文件传输. FTP 协议允许数据在不同文件系统的主机之间 ...
- 《JAVA学习笔记 (final关键字)》
[14-9]面向对象-final关键字 /* 继承的弊端,打破封装性. 不让其他类继承该类,就不会有重写. 怎么能实现呢?通过Java中的一个关键子来实现,final(最终化). [final关键字] ...
- Android IPC(inter-process Communitcation)
Android IPC(inter-process Communitcation) http://www.cnblogs.com/imlucky/archive/2013/08/08/3246013. ...
- IOS 使用block完成网络请求的自定义类BlockURLConnection
一,头文件 #import <Foundation/Foundation.h>//定义下载中block类型typedef void(^ProcessBlock) (NSURLRespons ...
- Jumping Cows_贪心
Description Farmer John's cows would like to jump over the moon, just like the cows in their favorit ...
- Request的参数信息
Request.ServerVariables["Url"] 返回服务器地址 Request.ServerVariables["Path_Info"] 客户端提 ...
- 【转发】构建高可伸缩性的WEB交互式系统(上)
原文转自:http://kb.cnblogs.com/page/503460/ 可伸缩性是一种对软件系统处理能力的设计指标,高可伸缩性代表一种弹性,在系统扩展过程中,能够保证旺盛的生命力,通过很少的改 ...
- iOS-UITableView性能优化
使用不透明视图. 不透明的视图可以极大地提高渲染的速度.因此如非必要,可以将table cell及其子视图的opaque属性设为YES(默认值).其中的特例包括背景色,它的alpha值应该为1 ...