1 #include <iostream> 2 #include <queue> 3 #include <cstring> 4 #define maxn 30005 5 #define scanf(x) scanf("%d", &x) 6 using namespace std; 7 8 struct CNode 9 { 10 int next; // index of the next vector from the same vertex 11 int k; // the end vertex of the vector 12 int w; 13 int d; // distence from k to the source point 14 }edges[5*maxn]; 15 16 bool operator < (const CNode &C1, const CNode &C2) 17 { 18 return C1.w > C2.w; 19 } 20 21 priority_queue<CNode> pq; 22 int cnt, N, head[maxn]; 23 bool bUsed[maxn]; 24 25 void build(int u, int v, int w) 26 { 27 edges[cnt].k = v; 28 edges[cnt].w = w; 29 edges[cnt].d = w; 30 edges[cnt].next = head[u]; 31 head[u] = cnt++; 32 } 33 34 int main() 35 { 36 int M; 37 CNode p; 38 cnt = 1; 39 memset(head, -1, sizeof(head)); 40 memset(bUsed, 0, sizeof(bUsed)); 41 scanf(N), scanf(M); 42 while (M--){ 43 int a, b, r; 44 scanf(a), scanf(b), scanf(r); 45 build(a, b, r); 46 } 47 edges[0].k = 1; 48 edges[0].w = 0; 49 edges[0].d = 0; 50 edges[0].next = -1; 51 pq.push(edges[0]); 52 while (!pq.empty()) { 53 p = pq.top(); pq.pop(); 54 if (bUsed[p.k])continue; 55 else bUsed[p.k] = 1; 56 if (p.k == N)break; 57 for (int i = head[p.k]; i != -1; i = edges[i].next) { 58 if (bUsed[edges[i].k])continue; 59 edges[i].d = p.d + edges[i].w; 60 pq.push(edges[i]); 61 } 62 } 63 printf("%d ", p.d); 64 return 0; 65 }