[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;

}

时间: 2024-08-11 08:03:07

[hdu2112]最短路的相关文章

hdu2112(HDU Today 简单最短路)

Problem Description 经过锦囊相助,海东集团终于度过了危机,从此,HDU的发展就一直顺风顺水,到了2050年,集团已经相当规模了,据说进入了钱江肉丝经济开发区500强.这时候,XHD夫妇也退居了二线,并在风景秀美的诸暨市浬浦镇陶姚村买了个房子,开始安度晚年了.这样住了一段时间,徐总对当地的交通还是不太了解.有时很郁闷,想去一个地方又不知道应该乘什么公交车,在什么地方转车,在什么地方下车(其实徐总自己有车,却一定要与民同乐,这就是徐总的性格).徐总经常会问蹩脚的英文问路:“Can

Hdu-2112 HDU Today (单源多点最短路——Dijsktra算法)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2112 题目大意:给你N个公交车站,起点,终点,各站之间的距离,求起点到终点之间的最短距离.(起点终点相同距离为0)不能到达输出-1. 说真的开始看到这个题,我想利用数字标记那些地名,再利用dijsktra算法,但不知道如何用代码实现,后来在网上看博客 才知道有这样一个头文件#include<map>,map 映射,可以有这种效果,那么这题也就so easy!了, 我的AC代码 #include&l

【最短路】HDU2112:HDU Today

Problem Description 经过锦囊相助,海东集团终于度过了危机,从此,HDU的发展就一直顺风顺水,到了2050年,集团已经相当规模了,据说进入了钱江肉丝经济开发区500强.这时候,XHD夫妇也退居了二线,并在风景秀美的诸暨市浬浦镇陶姚村买了个房子,开始安度晚年了.这样住了一段时间,徐总对当地的交通还是不太了解.有时很郁闷,想去一个地方又不知道应该乘什么公交车,在什么地方转车,在什么地方下车(其实徐总自己有车,却一定要与民同乐,这就是徐总的性格).徐总经常会问蹩脚的英文问路:“Can

hdu-2112 HDU Today(最短路)

题目链接: HDU Today Time Limit: 15000/5000 MS (Java/Others)     Memory Limit: 32768/32768 K (Java/Others) Problem Description 经过锦囊相助,海东集团终于度过了危机,从此,HDU的发展就一直顺风顺水,到了2050年,集团已经相当规模了,据说进入了钱江肉丝经济开发区500强.这时候,XHD夫妇也退居了二线,并在风景秀美的诸暨市浬浦镇陶姚村买了个房子,开始安度晚年了.这样住了一段时间,

hdu2112 HDU Today 基础最短路

这题的关键是把车站的名字转化为点的编号.我用的是map.声明一个map<string,int> st,然后按照字符串出现的次序给st赋值.例如:st[s1]=2;代表这字符串s1出现的次序是2.出现过的已经被标记.不会重复.接下来用模版就好.不过有一点要注意的是当起点和终点一样是,要输出0. #include<iostream> #include<cstdio> #include<cstring> #include<string> #includ

hdu3461Marriage Match IV 最短路+最大流

//给一个图.给定起点和终点,仅仅能走图上的最短路 //问最多有多少种走的方法.每条路仅仅能走一次 //仅仅要将在最短路上的全部边的权值改为1.求一个最大流即可 #include<cstdio> #include<cstring> #include<iostream> #include<queue> #include<vector> using namespace std ; const int inf = 0x3f3f3f3f ; const

UESTC30-最短路-Floyd最短路、spfa+链式前向星建图

最短路 Time Limit: 3000/1000MS (Java/Others) Memory Limit: 65535/65535KB (Java/Others) 在每年的校赛里,所有进入决赛的同学都会获得一件很漂亮的T-shirt.但是每当我们的工作人员把上百件的衣服从商店运回到赛场的时候,却是非常累的!所以现在他们想要寻找最短的从商店到赛场的路线,你可以帮助他们吗? Input 输入包括多组数据. 每组数据第一行是两个整数NN ,MM (N≤100N≤100 ,M≤10000M≤1000

ACM: HDU 2544 最短路-Dijkstra算法

HDU 2544最短路 Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Description 在每年的校赛里,所有进入决赛的同学都会获得一件很漂亮的t-shirt.但是每当我们的工作人员把上百件的衣服从商店运回到赛场的时候,却是非常累的!所以现在他们想要寻找最短的从商店到赛场的路线,你可以帮助他们吗? Input 输入包括多组数据.每组数据第一行是两个整数N.M(N<=100,M<

ACM/ICPC 之 昂贵的聘礼-最短路解法(POJ1062)

//转移为最短路问题,枚举必经每一个不小于酋长等级的人的最短路 //Time:16Ms Memory:208K #include<iostream> #include<cstring> #include<cstdio> #include<cmath> #include<algorithm> using namespace std; #define INF 0x3f3f3f3f #define MAX 105 int lim, n; int p[M