|
发表于 2016-8-3 17:36:48
|
显示全部楼层
- #include <cstdio>
- #include <cstdlib>
- #include <cstring>
- #include <cmath>
- #include <vector>
- #include <queue>
- #include <algorithm>
- using namespace std;
- #define inf 0x3f3f3f3f
-
- const int maxn = 505;
- int dist[maxn];
- struct node {
- int x, val;
- node(int a, int b) {x = a; val = b;}
- bool operator < (const node & a) const {
- if (val == a.val) return x < a.x;
- else return val > a.val;
- }
- };
- vector<node> v[maxn];
- void Dijkstra(int s, int n) {
- for (int i = 1; i <= n; i++) dist[i] = inf;
- dist[s] = 0;
- priority_queue<node> q;
- q.push(node(s, dist[s]));
- while(!q.empty()) {
- node now = q.top(); q.pop();
- for (int i = 0; i < v[now.x].size(); i++) {
- node next = v[now.x][i];
- if (dist[next.x] > now.val + next.val) {
- dist[next.x] = now.val + next.val;
- q.push(node(next.x, dist[next.x]));
- }
- }
- }
- printf("%d\n", dist[n]);
- }
- int main() {
- int n, m;
- while(scanf("%d%d", &n, &m) != EOF, n + m) {
- for (int i = 1; i <= n; i++) v[i].clear();
- for (int i = 1; i <= m; i++) {
- int a, b, c;
- scanf("%d%d%d", &a, &b, &c);
- v[a].push_back(node(b, c));
- v[b].push_back(node(a, c));
- }
- Dijkstra(1, n);
- }
- system("pause");
- return 0;
- }
复制代码 |
|