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