fvdcx's blog


  • 首页

  • 分类

  • 关于

  • 归档

  • 标签

hduoj-kruscal最小生成树

发表于 2016-08-29   |   分类于 算法   |  

题目大意

  http://acm.hdu.edu.cn/showproblem.php?pid=1233]

  此题直接套用最小生成树的算法,这里采用基于并查集实现的Kruscal算法。

题目分析

  并查集标准的操作:find,unite等,并查集要用路径压缩和rank函数进行优化。Kruscal算法要求先对所有边进行从小到大的排序,过多的不介绍了。

代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_SIZE 10005
struct Edge {
int src;
int dest;
int weight;
};
struct Edge e[MAX_SIZE];
int comp(const void* a, const void* b)
{
struct Edge* a1 = (struct Edge*)a;
struct Edge* b1 = (struct Edge*)b;
return a1->weight > b1->weight;
}
int par[MAX_SIZE];
int rank2[MAX_SIZE];
void init(int n) {
for (int i = 0; i < n; i++) {
par[i] = i;
rank2[i] = 0;
}
}
int find(int x) {
if(par[x] == x) {
return x;
} else {
return par[x] = find(par[x]);//路径压缩
}
}
void unite(int x, int y) {
x = find(x);
y = find(y);
if(x == y) {
return;
}
if(rank2[x] < rank2[y]) {
par[x] = y;
} else {
par[y] = x;
if(rank2[x] == rank2[y]) {
rank2[x]++;
}
}
}
bool same(int x, int y) {
return find(x) == find(y);
}
int main() {
int N, M;
while(scanf("%d%d", &M, &N)) {
if(M == 0) {
return 0;
}
for(int m = 0; m < M; m++) {
int s, d, w;
scanf("%d%d%d", &s, &d, &w);
e[m].src = s;
e[m].dest = d;
e[m].weight = w;
}
if(M < N - 1) {
printf("?\n");
continue;
}
//sort
qsort(e, M, sizeof(struct Edge), comp);
int ans = 0;
int count = 0;
init(N);
for(int m = 0; m < M; m++) {
if(same(e[m].src - 1, e[m].dest - 1)) {
continue;
}
unite(e[m].src - 1, e[m].dest - 1);
ans += e[m].weight;
count++;
if(count == N - 1) {
break;
}
}
int root = 0;
for(int n = 0; n < N; n++) {
if(par[n] == n) {
root++;
}
}
if(root > 1) {
printf("?\n");
} else {
printf("%d\n", ans);
}
}
return 0;
}

hihocoder-1290-demo-day

发表于 2016-08-28   |   分类于 算法 , hihocoder   |  

题目大意

  http://hihocoder.com/problemset/problem/1290

  此题是微软2016校园招聘4月在线笔试的第三题。是一个机器人走迷宫的问题,你可以将迷宫任意位置的.变成b或者反之,机器人开始时向右走,遇到b以后向下走,再遇到b以后往右走,如此走法。求机器人从左上角走到右下角最少的变换次数。

题目分析

  思路参考了这篇文章https://glatue.wordpress.com/2016/04/13/hihocoder-1290-demo-day-%E5%BE%AE%E8%BD%AF%E9%A2%98/

  利用动态规划的思想,需要利用两个二维数组dpx和dpy,因为每个位置机器人的方向都可以是向右或者向下,代表从左上角走道当前位置最少需要变换的次数。

  转移方程:

1
2
dpx[i][j] = min(dpx[i][j - 1], dpy[i][j - 1] + (matrix[i + 1][j - 1] != 'b')) + (matrix[i][j] == 'b');
dpy[i][j] = min(dpy[i - 1][j], dpx[i - 1][j] + (matrix[i - 1][j + 1] != 'b')) + (matrix[i][j] == 'b');
阅读全文 »

hihocoder1289-403-forbidden

发表于 2016-08-28   |   分类于 算法 , hihocoder   |  

题目大意

  http://hihocoder.com/problemset/problem/1289

  此题是微软2016校园招聘4月在线笔试的第二题。给出一系列ip和mask的规则,让你判断一个新的ip是allow还是deny。注意的是,有些输入条件没有mask,也就是全部匹配(mask=32),匹配规则是按照最先匹配的原则而不是最长匹配,当没有规则匹配上时返回allow。

题目分析

  思路参考了这篇文章https://glatue.wordpress.com/2016/04/12/hihocoder-1289-403-forbidden/

  比较明显的用trie树来处理前缀匹配,注意数据量是在10万,处理ip转换时要用高效率的位操作而不要用字符串转换处理.

  每个树节点存储当前行号line,是为了最后查找时候找最上面的一行,也就是最先匹配原则。还要存储是否允许,当然该值仅当为word节点才有效,其它节点的isAllow没有意义。还要注意根节点的line和isAllow也是有意义的,保存那些mask值为0的ip,当没有匹配到任何叶节点当然就直接返回根节点的isAllow值

阅读全文 »

leetcode-trie树实现

发表于 2016-08-21   |   分类于 算法 , leetcode   |  

题目大意

  https://leetcode.com/problems/implement-trie-prefix-tree/

  trie树的实现

题目分析

  trie树,实现插入,搜索,复杂度都是o(len),len为单词的长度

阅读全文 »
1…323334…38
fvdcx

fvdcx

149 日志
14 分类
20 标签
GitHub
© 2015 - 2017 fvdcx
由 Hexo 强力驱动
主题 - NexT.Pisces