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
| #include <iostream> #include <cstdio> #include <cstring> #include <algorithm> #include <queue> #include <cmath> #define mp std::make_pair using namespace std;
const int MAXN = 1000 + 7; const int MAXM = 1000000 + 7; const int INF = 0x7fffffff;
typedef long long int ll; typedef std::pair<int, int> Pair;
int n, m;
inline int getint() { int s = 0, x = 1; char ch = getchar(); while (ch > '9' || ch < '0') { if (ch == '-') x = -x; ch = getchar(); } while (ch >= '0' && ch <= '9') { s = s * 10 + ch - '0'; ch = getchar(); } return s * x; }
struct Node { int v, next, w; }edge[MAXM];
int num = 0, head[MAXN];
bool inQueue[MAXN];
ll dis[MAXN];
inline void addEdge(int u, int v, int w) { edge[++num].v = v; edge[num].w = w; edge[num].next = head[u]; head[u] = num; }
inline int dijkstra(int s, int t, int n) { for (int i = 1; i <= n; ++i) dis[i] = INF; std::priority_queue<std::pair<int, int>, std::vector<std::pair<int, int> >, std::greater<std::pair<int, int> > > q; inQueue[s] = true; dis[1] = 0; q.push(mp(s,0)); while (!q.empty()) { int v = q.top().first; int value = q.top().second; q.pop(); for (int e = head[v]; e; e = edge[e].next) { if (dis[edge[e].v] > value + edge[e].w) { dis[edge[e].v] = (value + edge[e].w) ; q.push(mp(edge[e].v, dis[edge[e].v])); } } } return dis[t]; }
int main(int argc, char const *argv[]) { n = getint(), m = getint(); for (int i = 0; i < m; ++i) { int x, y, z; x = getint(), y = getint(), z = getint(); addEdge(x, y, z); } printf("%d\n", dijkstra(1, n, n)); return 0; }
|