Shirlies
宁静专注认真的程序媛~

心血来潮,用优先队列实现了这个Dijkstra,顺便练练静态链表,不过结果也蛮不错的,在acm steps里面是93msA的,在外面却是109ms,排名很靠前,好开心,不过也郁闷,竟然有人31ms过了,汗...到底是怎么样的一个牛人啊......

代码如下:
 1 #include <cstdio>
 2 #include <cstring>
 3 #include <queue>
 4 using namespace std;
 5 
 6 const int maxn = 20000 + 100;
 7 int n,m,endd;
 8 typedef pair<int,int> pii;
 9 struct node
10 {
11     int x,y;
12     int w;
13 }p[maxn];
14 int nextt[maxn];
15 int first[maxn];
16 int d[1010];
17 
18 void dij()
19 {
20     priority_queue<pii,vector<pii>,greater<pii> > q;
21     memset(d,-1,sizeof(d));
22     d[0] = 0;
23     q.push(make_pair(d[0],0));
24     while(!q.empty())
25     {
26         pii u = q.top();
27         q.pop();
28         int x = u.second;
29         if(u.first != d[x])
30             continue;
31 
32         for(int e = first[x];e != -1;e = nextt[e])
33         {
34             if(d[p[e].y] == -1 || d[p[e].y] > d[x] + p[e].w)
35             {
36                 d[p[e].y] = d[x] + p[e].w;
37                 q.push(make_pair(d[p[e].y],p[e].y));
38             }
39         }
40     }
41     printf("%d\n",d[endd]);
42 }
43 int main()
44 {
45     while(scanf("%d%d%d",&n,&m,&endd) == 3)
46     {
47         memset(first,-1,sizeof(first));
48         for(int i = 0;i < m;i ++)
49         {
50             int a,b,w;
51             scanf("%d%d%d",&a,&b,&w);
52             p[i].x = a;
53             p[i].y = b;
54             p[i].w = w;
55             nextt[i] = first[a];
56             first[a] = i;
57         }
58         int aa;
59         scanf("%d",&aa);
60         int t;
61         for(int i = 0;i < aa;i ++)
62         {
63             scanf("%d",&t);
64             p[i + m].x = 0;
65             p[i + m].y = t;
66             p[i + m].w = 0;
67             nextt[i + m] = first[0];
68             first[0] = i + m;
69         }
70         dij();
71     }
72 
73     return 0;
74 }

 

posted on 2012-05-02 20:44  Shirlies  阅读(286)  评论(0编辑  收藏  举报