hdu 1874 畅通工程续

题目连接

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

畅通工程续

Description

某省自从实行了很多年的畅通工程计划后,终于修建了很多路。不过路多了也不好,每次要从一个城镇到另一个城镇时,都有许多种道路方案可以选择,而某些方案要比另一些方案行走的距离要短很多。这让行人很困扰。

现在,已知起点和终点,请你计算出要从起点到终点,最短需要行走多少距离

Input

本题目包含多组数据,请处理到文件结束。
每组数据第一行包含两个正整数$N$和$M\ (0<N<200,0<M<1000)$,分别代表现有城镇的数目和已修建的道路的数目。城镇分别以$0~N-1$编号。
接下来是$M$行道路信息。每一行有三个整数$A,B,X\ (0 \leq A,B<N,A \neq B,0<X<10000)$,表示城镇$A$和城镇$B$之间有一条长度为$X$的双向道路。
再接下一行有两个整数$S,T\ (0 \leq S,T<N)$,分别代表起点和终点。

Output

对于每组数据,请在一行里输出最短需要行走的距离。如果不存在从$S$到$T$的路线,就输出-1.

Sample Input

3 3
0 1 1
0 2 3
1 2 1
0 2
3 1
0 1 1
1 2

Sample Output

2
-1

单源最短路。。

 1 #include<algorithm>
 2 #include<iostream>
 3 #include<cstdlib>
 4 #include<cstring>
 5 #include<cstdio>
 6 #include<vector>
 7 #include<queue>
 8 #include<map>
 9 using std::cin;
10 using std::cout;
11 using std::endl;
12 using std::find;
13 using std::sort;
14 using std::pair;
15 using std::vector;
16 using std::multimap;
17 using std::priority_queue;
18 #define pb(e) push_back(e)
19 #define sz(c) (int)(c).size()
20 #define mp(a, b) make_pair(a, b)
21 #define all(c) (c).begin(), (c).end()
22 #define iter(c) decltype((c).begin())
23 #define cls(arr,val) memset(arr,val,sizeof(arr))
24 #define cpresent(c, e) (find(all(c), (e)) != (c).end())
25 #define rep(i, n) for (int i = 0; i < (int)(n); i++)
26 #define tr(c, i) for (iter(c) i = (c).begin(); i != (c).end(); ++i)
27 const int N = 100010;
28 struct P {
29     int w, v;
30     P(int i = 0, int j = 0) :w(i), v(j) {}
31     inline bool operator<(const P &a) const {
32         return w > a.w;
33     }
34 };
35 struct Node { int to, w, next; };
36 struct Dijkstra {
37     Node G[N];
38     int tot, u, v, w, dist[N], head[N];
39     inline void init() {
40         tot = 0;
41         cls(head, -1), cls(dist, 0x3f);
42     }
43     inline void add_edge(int u, int v, int w) {
44         G[tot] = { v, w, head[u] }; head[u] = tot++;
45     }
46     inline void built(int m) {
47         rep(i, m) {
48             scanf("%d %d %d", &u, &v, &w);
49             add_edge(u, v, w), add_edge(v, u, w);
50         }
51         scanf("%d %d", &u, &v);
52     }
53     inline void dijkstra() {
54         dist[u] = 0;
55         priority_queue<P> q;
56         q.push(P(0, u));
57         while (!q.empty()) {
58             P t = q.top(); q.pop();
59             int x = t.v;
60             if (dist[x] < t.w) continue;
61             for (int i = head[x]; ~i; i = G[i].next) {
62                 int &d = dist[G[i].to];
63                 if (d > dist[x] + G[i].w) {
64                     d = dist[x] + G[i].w;
65                     q.push(P(d, G[i].to));
66                 }
67             }
68         }
69         printf("%d\n", dist[v] == (int)0x3f3f3f3f ? -1 : dist[v]);
70     }
71 }go;
72 int main() {
73 #ifdef LOCAL
74     freopen("in.txt", "r", stdin);
75     freopen("out.txt", "w+", stdout);
76 #endif
77     int n, m;
78     while (~scanf("%d %d", &n, &m)) {
79         go.init();
80         go.built(m);
81         go.dijkstra();
82     }
83     return 0;
84 }
View Code
posted @ 2015-07-14 22:12  GadyPu  阅读(105)  评论(0编辑  收藏  举报