1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98
|
const int maxN = 1100; const int maxM = 110000; int first[maxN]; int nex[maxM]; int u[maxM]; int v[maxM]; int w[maxM]; int tp;
void AddEdge(int s, int e, int weight) { nex[tp] = first[s]; first[s] = tp; u[tp] = s; v[tp] = e; w[tp] = weight; tp ++; } void DbEdge(int s, int e, int weight) { AddEdge(s, e, weight); AddEdge(e, s, weight); }
bool vis[maxN] = {0}; void DFS(int now) { for(int i = first[now]; i != -1; i = nex[i]) { if(!vis[v[i]]) { vis[v[i]] = true; DFS(v[i]); } } }
#include<queue> #include<algorithm> struct Node { int vNum; int pathLen; Node(){} Node(int v_, int p_){vNum = v_, pathLen = p_;} bool operator<(const Node &b)const { return pathLen > b.pathLen; } }; int key[maxN]; int Dijkstra(int s, int e) { memset(key, 0x3f, sizeof(key)); key[s] = 0; std::priority_queue<Node> q; q.push(Node(0, 0)); while(!q.empty()) { Node now = q.top(); q.pop(); if(now.pathLen != key[now.vNum]) continue; if(now.vNum == e) return now.pathLen; for(int i = first[now.vNum]; i != -1; i = nex[i]) { if(now.pathLen + w[i] < key[v[i]]) { key[v[i]] = now.pathLen + w[i]; q.push(Node(v[i], key[v[i]])); } } } return 0x3f3f3f3f; }
|