[hdu2112]最短路
相当于模板题了,用trie来完成字符串到数字的映射比map<string, int>要快不少,令外可以考虑hash。
运行时间对比:
(1)(2)600ms左右 (3)3000ms左右(4)1500ms左右
(1)O(n^2)的dijkstra:
|
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
|
#include <stdio.h>#include <string.h>#include <iostream>#include <algorithm>#include <vector>#include <queue>#include <set>#include <map>#include <string>#include <math.h>#include <stdlib.h>#include <time.h>#include <map>using namespace std;#define umin(a, b) if ((a) > (b)) (a) = (b)const int maxn = 157;int n;struct Trie { int ch[maxn * 32][26]; int val[maxn * 32], sz; void init() { memset(val, 0, sizeof(val)); memset(ch[0], 0, sizeof(ch[0])); sz = 0; } int insert(char *s) { int i = 0, j = 0; for (; s[j]; i = ch[i][s[j]], j ++) { if (s[j] < 'a') s[j] -= 'A'; else s[j] -= 'a'; if (!ch[i][s[j]]) { ch[i][s[j]] = ++ sz; memset(ch[sz], 0, sizeof(ch[sz])); } } if (!val[i]) val[i] = ++ n; return val[i]; }} trie;int dist[maxn][maxn], d[maxn];bool vis[maxn];int main(){#ifndef ONLINE_JUDGE freopen("in.txt", "r", stdin);#endif // ONLINE_JUDGE int m; char s1[40], s2[40]; int t; while (cin >> m, ~m) { n = 0; trie.init(); scanf("%s%s", s1, s2); trie.insert(s1); t = trie.insert(s2); memset(dist, 0x3f, sizeof(dist)); for (int i = 0; i < m; i ++) { int x; scanf("%s%s%d", s1, s2, &x); int u = trie.insert(s1), v = trie.insert(s2); umin(dist[u][v], x); umin(dist[v][u], x); } memset(vis, 0, sizeof(vis)); memset(d, 0x3f, sizeof(d)); d[1] = 0; for (int i = 1; i < n; i ++) { int pos = 0, mind = 0x3f3f3f3f; for (int j = 1; j <= n; j ++) { if (!vis[j] && d[j] < mind) { mind = d[j]; pos = j; } } if (!pos) break; vis[pos] = true; for (int j = 1; j <= n; j ++) { if (!vis[j]) umin(d[j], d[pos] + dist[pos][j]); } } printf("%d\n", d[t] == 0x3f3f3f3f? -1 : d[t]); } return 0;} |
(2)SPFA:
|
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
|
#include <stdio.h>#include <string.h>#include <iostream>#include <algorithm>#include <vector>#include <queue>#include <set>#include <map>#include <string>#include <math.h>#include <stdlib.h>#include <time.h>#include <map>using namespace std;#define umin(a, b) if ((a) > (b)) (a) = (b)const int maxn = 157;int n;struct Trie { int ch[maxn * 32][26]; int val[maxn * 32], sz; void init() { memset(val, 0, sizeof(val)); memset(ch[0], 0, sizeof(ch[0])); sz = 0; } int insert(char *s) { int i = 0, j = 0; for (; s[j]; i = ch[i][s[j]], j ++) { if (s[j] < 'a') s[j] -= 'A'; else s[j] -= 'a'; if (!ch[i][s[j]]) { ch[i][s[j]] = ++ sz; memset(ch[sz], 0, sizeof(ch[sz])); } } if (!val[i]) val[i] = ++ n; return val[i]; }} trie;vector<vector<int> >G, W;queue<int> Q;int d[maxn];bool mark[maxn];bool relax(int u, int v, int w) { if (d[v] > d[u] + w) { d[v] = d[u] + w; return true; } return false;}int main(){#ifndef ONLINE_JUDGE freopen("in.txt", "r", stdin);#endif // ONLINE_JUDGE int m; char s1[40], s2[40]; int t; while (cin >> m, ~m) { n = 0; trie.init(); scanf("%s%s", s1, s2); trie.insert(s1); t = trie.insert(s2); G.clear(); W.clear(); G.resize(maxn + 2); W.resize(maxn + 2); for (int i = 0; i < m; i ++) { int x; scanf("%s%s%d", s1, s2, &x); int u = trie.insert(s1), v = trie.insert(s2); G[u].push_back(v); G[v].push_back(u); W[u].push_back(x); W[v].push_back(x); } while (!Q.empty()) Q.pop(); Q.push(1); memset(d, 0x3f, sizeof(d)); memset(mark, 0, sizeof(mark)); mark[1] = true; d[1] = 0; while (!Q.empty()) { int h = Q.front(); Q.pop(); for (int i = 0; i < G[h].size(); i ++) { int v = G[h][i], r = relax(h, v, W[h][i]); if (!mark[v] && r) { mark[v] = true; Q.push(v); } } mark[h] = false; } printf("%d\n", d[t] == 0x3f3f3f3f? -1 : d[t]); } return 0;} |
(3)map + cin :
|
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
|
#include <stdio.h>#include <string.h>#include <iostream>#include <algorithm>#include <vector>#include <queue>#include <set>#include <map>#include <string>#include <math.h>#include <stdlib.h>#include <time.h>#include <map>using namespace std;const int maxn = 2e4 +7;map<string, int> mp;int n;vector<vector<int> >G, W;queue<int> Q;bool mark[maxn];int d[maxn];bool relax(int u, int v, int w) { if (d[v] > d[u] + w) { d[v] = d[u] + w; return true; } return false;}int main(){#ifndef ONLINE_JUDGE freopen("in.txt", "r", stdin);#endif // ONLINE_JUDGE string s1, s2, S; int x, m; while (cin >> m, ~m) { cin >> s1 >> s2; S = s2; n = 1; mp.clear(); G.clear(); W.clear(); G.resize(maxn + 2); W.resize(maxn + 2); if (!mp[s1]) mp[s1] = n ++; if (!mp[s2]) mp[s2] = n ++; for (int i = 0; i < m; i ++) { cin >> s1 >> s2 >> x; if (!mp[s1]) mp[s1] = n ++; if (!mp[s2]) mp[s2] = n ++; G[mp[s1]].push_back(mp[s2]); G[mp[s2]].push_back(mp[s1]); W[mp[s1]].push_back(x); W[mp[s2]].push_back(x); } while (!Q.empty()) Q.pop(); Q.push(1); memset(d, 0x3f, sizeof(d)); memset(mark, 0, sizeof(mark)); mark[1] = true; d[1] = 0; while (!Q.empty()) { int h = Q.front(); Q.pop(); for (int i = 0; i < G[h].size(); i ++) { int v = G[h][i], r = relax(h, v, W[h][i]); if (!mark[v] && r) { mark[v] = true; Q.push(v); } } mark[h] = false; } printf("%d\n", d[mp[S]] == 0x3f3f3f3f? -1 : d[mp[S]]); } return 0;} |
(4)map + scanf:
|
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
|
#include <stdio.h>#include <string.h>#include <iostream>#include <algorithm>#include <vector>#include <queue>#include <set>#include <map>#include <string>#include <math.h>#include <stdlib.h>#include <time.h>#include <map>using namespace std;const int maxn = 2e4 +7;map<string, int> mp;int n;vector<vector<int> >G, W;queue<int> Q;bool mark[maxn];int d[maxn];bool relax(int u, int v, int w) { if (d[v] > d[u] + w) { d[v] = d[u] + w; return true; } return false;}void read_string(string &s) { char s0[100]; scanf("%s", s0); s = string(s0);}int main(){#ifndef ONLINE_JUDGE freopen("in.txt", "r", stdin);#endif // ONLINE_JUDGE string s1, s2, S; int x, m; while (cin >> m, ~m) { read_string(s1); read_string(s2); S = s2; n = 1; mp.clear(); G.clear(); W.clear(); G.resize(maxn + 2); W.resize(maxn + 2); if (!mp[s1]) mp[s1] = n ++; if (!mp[s2]) mp[s2] = n ++; for (int i = 0; i < m; i ++) { read_string(s1); read_string(s2); scanf("%d", &x); if (!mp[s1]) mp[s1] = n ++; if (!mp[s2]) mp[s2] = n ++; G[mp[s1]].push_back(mp[s2]); G[mp[s2]].push_back(mp[s1]); W[mp[s1]].push_back(x); W[mp[s2]].push_back(x); } while (!Q.empty()) Q.pop(); Q.push(1); memset(d, 0x3f, sizeof(d)); memset(mark, 0, sizeof(mark)); mark[1] = true; d[1] = 0; while (!Q.empty()) { int h = Q.front(); Q.pop(); for (int i = 0; i < G[h].size(); i ++) { int v = G[h][i], r = relax(h, v, W[h][i]); if (!mark[v] && r) { mark[v] = true; Q.push(v); } } mark[h] = false; } printf("%d\n", d[mp[S]] == 0x3f3f3f3f? -1 : d[mp[S]]); } return 0;} |
[hdu2112]最短路的更多相关文章
- HDU2112 HDU Today 最短路+字符串哈希
HDU Today Time Limit: 15000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total ...
- hdu2112(HDU Today 简单最短路)
Problem Description 经过锦囊相助,海东集团终于度过了危机,从此,HDU的发展就一直顺风顺水,到了2050年,集团已经相当规模了,据说进入了钱江肉丝经济开发区500强.这时候,XHD ...
- Hdu-2112 HDU Today (单源多点最短路——Dijsktra算法)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2112 题目大意:给你N个公交车站,起点,终点,各站之间的距离,求起点到终点之间的最短距离.(起点终点相 ...
- hdu2112 HDU Today 基础最短路
这题的关键是把车站的名字转化为点的编号.我用的是map.声明一个map<string,int> st,然后按照字符串出现的次序给st赋值.例如:st[s1]=2;代表这字符串s1出现的次序 ...
- bzoj1001--最大流转最短路
http://www.lydsy.com/JudgeOnline/problem.php?id=1001 思路:这应该算是经典的最大流求最小割吧.不过题目中n,m<=1000,用最大流会TLE, ...
- 【USACO 3.2】Sweet Butter(最短路)
题意 一个联通图里给定若干个点,求他们到某点距离之和的最小值. 题解 枚举到的某点,然后优先队列优化的dijkstra求最短路,把给定的点到其的最短路加起来,更新最小值.复杂度是\(O(NElogE) ...
- Sicily 1031: Campus (最短路)
这是一道典型的最短路问题,直接用Dijkstra算法便可求解,主要是需要考虑输入的点是不是在已给出的地图中,具体看代码 #include<bits/stdc++.h> #define MA ...
- 最短路(Floyd)
关于最短的先记下了 Floyd算法: 1.比较精简准确的关于Floyd思想的表达:从任意节点A到任意节点B的最短路径不外乎2种可能,1是直接从A到B,2是从A经过若干个节点X到B.所以,我们假设maz ...
- bzoj1266最短路+最小割
本来写了spfa wa了 看到网上有人写Floyd过了 表示不开心 ̄へ ̄ 改成Floyd试试... 还是wa ヾ(。`Д´。)原来是建图错了(样例怎么过的) 结果T了 于是把Floyd改回spfa 还 ...
随机推荐
- 装机摸鱼日志--ubuntu16.04安装网易云音乐客户端
之前装的网易云音乐不指定啥原因不能用了,所以打算重新装一个,但是进官网只有deepin15和ubuntu18.04版本的安装包.然后我装了一下18.04的安装包,但是没有成功.甚至因为更换glibc差 ...
- WPF中在Gmap.net中将Marker动起来
前一段时间说过一篇绘制极坐标的,这段时间对它进行了改造已经今非昔比了,功能实现了很多,我目的是让Marker动起来,然后还会绘制Route,上篇也就是简单的绘制了Route,没有关于Marker的相关 ...
- es技术规划
一.业务背景 es服务当前没有专门的部门负责维护和开发,交由各端自行负责维护,随着公司业务查询和统计需求非常多,会面临居多方面问题和挑战: 无人(专业RD或部门)负责 无专业的人进行维护,遇到问题几乎 ...
- 算法笔记刷题5(PAT A1025)
第一次上手PAT的甲级题目,瑟瑟发抖(英语不好对着题目愣了半天) 这一题的要点是使用sort函数. 使用sort函数必须使用 #include <algorithm> using name ...
- xpath爬虫实战-爬取小说斗罗大陆第四部
爬取思路 用到的第三方库文件 lxml,requests,fake_agent 用fake_agent里的UserAgent修饰爬虫 用requests进行基本的请求 用lxml进行html的分析 用 ...
- redis实现排行榜思路
用redis的排序集合类型 sortset()实现排行榜 zadd();添加 ZREVRANGE();查看
- Golang Map实现(四) map 的赋值和扩容
title: Golang Map 实现 (四) date: 2020-04-28 18:20:30 tags: golang map 操作,是map 实现中较复杂的逻辑.因为当赋值时,为了减少has ...
- hdu_1050 Moving Tables 贪心
Moving Tables Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Tot ...
- java之 惰性初始化
class Soap { private String s; Soap(){ System.out.println("Soap()"); s="Constructed&q ...
- 如何把字符串数组从 Swift 传递给 C
作者:Natasha The Robot,原文链接,原文日期:2016-10-27译者:BigbigChai:校对:walkingway:定稿:CMB Swift 允许我们将原生的字符串直接传递给一个 ...