3 2 1 2 5 6 2 3 4 5 1 3 0 0
9 11
#include <stdio.h> #include <string.h> #define maxn 1002 #define maxm 100002 #define inf 0x7fffffff int head[maxn], id; struct Node{ int to, cost, dist, next; } E[maxm << 1]; bool vis[maxn]; int dist[maxn], cost[maxn]; void addEdge(int u, int v, int d, int c) { E[id].to = v; E[id].cost = c; E[id].dist = d; E[id].next = head[u]; head[u] = id++; E[id].to = u; E[id].cost = c; E[id].dist = d; E[id].next = head[v]; head[v] = id++; } int getNext(int n) { int i, tmp = inf, u = -1; for(i = 1; i <= n; ++i) if(!vis[i] && dist[i] < tmp){ tmp = dist[i]; u = i; } return u; } void Dijkstra(int n, int s, int t) { int u, v, i, count, tmpd, tmpc; memset(vis, 0, sizeof(vis)); memset(dist, 0x7f, sizeof(dist)); memset(cost, 0x7f, sizeof(cost)); dist[s] = 0; u = s; cost[s] = 0; while(u != -1){ for(i = head[u]; i != -1; i = E[i].next){ tmpd = dist[u] + E[i].dist; tmpc = cost[u] + E[i].cost; v = E[i].to; if(tmpd < dist[v]){ dist[v] = tmpd; cost[v] = tmpc; }else if(tmpd == dist[v] && tmpc < cost[v]){ dist[v] = tmpd; cost[v] = tmpc; } } vis[u] = true; if(vis[t]) break; u = getNext(n); } } int main() { int n, m, i, u, v, c, d, s, t; while(scanf("%d%d", &n, &m), n || m){ memset(head, -1, sizeof(head)); for(i = id = 0; i < m; ++i){ scanf("%d%d%d%d", &u, &v, &d, &c); addEdge(u, v, d, c); } scanf("%d%d", &s, &t); Dijkstra(n, s, t); printf("%d %d\n", dist[t], cost[t]); } return 0; }
2015.1.27更新
#include <stdio.h> #include <string.h> #define maxn 1002 #define inf 0x3f3f3f3f int G[maxn][maxn], N; int Gp[maxn][maxn]; struct Node { int d, p; } D[maxn]; bool vis[maxn]; int getNext() { int u = -1, d = inf, p = inf, i; for (i = 1; i <= N; ++i) if (!vis[i] && D[i].d < d) { u = i; d = D[i].d; p = D[i].p; } else if (!vis[i] && D[i].d == d && D[i].p > p) { u = i; p = D[i].p; } return u; } void Dijkstra(int s, int t) { int u = s, i; for (i = 1; i <= N; ++i) { vis[i] = 0; D[i].d = D[i].p = inf; } D[u].d = D[u].p = 0; while (u != t) { vis[u] = 1; for (i = 1; i <= N; ++i) { if (D[u].d + G[u][i] < D[i].d) { D[i].d = D[u].d + G[u][i]; D[i].p = D[u].p + Gp[u][i]; } else if (D[i].d == D[u].d + G[u][i] && D[i].p > D[u].p + Gp[u][i]) D[i].p = D[u].p + Gp[u][i]; } u = getNext(); } printf("%d %d\n", D[t].d, D[t].p); } int main() { freopen("stdin.txt", "r", stdin); int M, u, v, d, p, s, t; int i, j; while (scanf("%d%d", &N, &M), N | M) { for (i = 1; i <= N; ++i) for (j = 1; j <= N; ++j) G[i][j] = Gp[i][j] = inf; while (M--) { scanf("%d%d%d%d", &u, &v, &d, &p); if (G[u][v] > d) { G[u][v] = G[v][u] = d; Gp[u][v] = Gp[v][u] = p; } else if (G[u][v] == d && Gp[u][v] > p) Gp[u][v] = Gp[v][u] = p; } scanf("%d%d", &s, &t); Dijkstra(s, t); } return 0; }