3159

1 /*
2 差分约束系统可以做
3
4 映射二叉堆
5
6 用 堆优化的bellman_ford.594ms
7
8 */
9
10 // include file
11 #include <cstdio>
12 #include <cstdlib>
13 #include <cstring>
14 #include <cmath>
15 #include <cctype>
16 #include <ctime>
17
18 #include <iostream>
19 #include <sstream>
20 #include <fstream>
21 #include <iomanip>
22 #include <bitset>
23
24 #include <algorithm>
25 #include <string>
26 #include <vector>
27 #include <queue>
28 #include <set>
29 #include <list>
30 #include <functional>
31
32 using namespace std;
33
34 // typedef
35 typedef long long LL;
36 typedef unsigned long long ULL;
37 typedef __int64 Bint;
38
39 //
40 #define read freopen("in.txt","r",stdin)
41 #define write freopen("out.txt","w",stdout)
42 #define FORi(a,b,c) for(int i=(a);i<(b);i+=c)
43 #define FORj(a,b,c) for(int j=(a);j<(b);j+=c)
44 #define FORk(a,b,c) for(int k=(a);k<(b);k+=c)
45 #define FORp(a,b,c) for(int p=(a);p<(b);p+=c)
46 #define FORii(a,b,c) for(int ii=(a);ii<(b);ii+=c)
47 #define FORjj(a,b,c) for(int jj=(a);jj<(b);jj+=c)
48 #define FORkk(a,b,c) for(int kk=(a);kk<(b);kk+=c)
49
50 #define FF(i,a) for(int i=0;i<(a);i++)
51 #define FFD(i,a) for(int i=(a)-1;i>=0;i--)
52
53 #define Z(a) (a<<1)
54 #define Y(a) (a>>1)
55
56 const double eps = 1e-6;
57 const double INFf = 1e10;
58 const int INFi = 1000000000;
59 const double Pi = acos(-1.0);
60
61 template<class T> inline T sqr(T a){return a*a;}
62 template<class T> inline T TMAX(T x,T y)
63 {
64 if(x>y) return x;
65 return y;
66 }
67 template<class T> inline T TMIN(T x,T y)
68 {
69 if(x<y) return x;
70 return y;
71 }
72 template<class T> inline void SWAP(T &x,T &y)
73 {
74 T t = x;
75 x = y;
76 y = t;
77 }
78 template<class T> inline T MMAX(T x,T y,T z)
79 {
80 return TMAX(TMAX(x,y),z);
81 }
82
83
84 // code begin
85 // 30000个点 150000条边
86 #define MAXN 30010
87 struct node1
88 {
89 int s;
90 int e;
91 int p;
92 int next;
93 };
94 struct node2
95 {
96 int next;
97 };
98 node1 mem[MAXN*5];
99 node2 G[MAXN];
100 int N,M;
101 int dst[MAXN];
102 bool isin[MAXN];
103 int cnt[MAXN];
104 //int qi[MAXN*2];
105 //queue<int> Qi;
106 //采用堆来写
107 int stk[MAXN],top;
108
109 void SPFA()
110 {
111 FORi(1,N+1,1)
112 dst[i] = INFi;
113 memset(isin,0,sizeof(isin));
114 memset(cnt,0,sizeof(cnt));
115 dst[1] = 0;
116 isin[1] = true;
117 cnt[1] = 1;
118 int head = 0;
119 int tail = 0;
120
121 //qi[tail++] = 1;
122 stk[top++]=1;
123 //while(!Qi.empty()) Qi.pop();
124 //Qi.push(1);
125 int dx;
126
127 while(top)
128 {
129 int cur = stk[top-1];
130 top--;
131 //int cur = qi[head];
132 //head = (head+1)%(MAXN*2);
133 //int cur = Qi.front();
134 //Qi.pop();
135 isin[cur] = false;
136
137 dx = G[cur].next;
138 //printf("cur: %d\n",cur);
139 while( dx!=-1 )
140 {
141 //printf("%d ",mem[dx].p);
142 if( dst[cur]+ mem[ dx ].p < dst[ mem[ dx ].e ] )
143 {
144 dst[ mem[ dx ].e ] = dst[cur]+ mem[ dx ].p;
145 if(!isin[ mem[ dx ].e ])
146 {
147 isin[ mem[ dx ].e ] = true;
148 cnt[ mem[ dx ].e ]++;
149 //qi[tail] = mem[ dx ].e;
150 //tail = (tail+1)%(MAXN*2);
151 //Qi.push(mem[dx].e);
152 stk[top++]=mem[dx].e;
153 }
154 }
155 dx = mem[dx].next;
156 }
157 //printf("\n");
158 }
159
160 printf("%d\n",dst[N]);
161 }
162
163 int main()
164 {
165 read;
166 write;
167 int a,b,c;
168 scanf("%d %d",&N,&M);
169 FORi(1,N+1,1)
170 {
171 G[i].next = -1;
172 }
173 FORi(0,M,1)
174 {
175 scanf("%d %d %d",&a,&b,&c);
176 mem[i].s =a;
177 mem[i].e =b;
178 mem[i].p =c;
179
180 mem[i].next = G[a].next;
181 G[a].next = i;
182 }
183 SPFA();
184 return 0;
185 }
posted @ 2011-03-10 22:47  AC2012  阅读(369)  评论(0编辑  收藏  举报