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
| #include <algorithm> #include <cstdio> #include <cstring> #include <iostream> #include <queue> #include <vector> #define pair_int std::pair<int, int> using namespace std; #define DIJKSTRA_MAX 10010
struct node { int v, w; node() {} node(int v0, int w0) : v(v0), w(w0) {} bool operator<(const node& b) const { return w < b.w; } }; bool vis[DIJKSTRA_MAX]; int dis[DIJKSTRA_MAX];
vector<node> son[DIJKSTRA_MAX]; inline int dijkstra(int s, int t) { priority_queue<pair_int, vector<pair_int>, greater<pair_int> > q; memset(dis, 127, sizeof(dis)); memset(vis, false, sizeof(vis)); dis[s] = 0; q.push(make_pair(dis[s], s)); while (!q.empty()) { int now = q.top().second; q.pop(); if (vis[now]) continue; vis[now] = true; for (int i = 0; i < son[now].size(); i++) { node x = son[now][i]; if (dis[now] + x.w < dis[x.v]) { dis[x.v] = dis[now] + x.w; q.push(make_pair(dis[x.v], x.v)); } } } return dis[t]; } inline void insert(int a, int b, int w) { son[a].push_back(node(b, w)); } inline void insert_multi(int a, int b, int w) { son[a].push_back(node(b, w)); son[b].push_back(node(a, w)); }
|