有了一张自驾旅游路线图,你会知道城市间的高速公路长度、以及该公路要收取的过路费。现在需要你写一个程序,帮助前来咨询的游客找一条出发地和目的地之间的最短路径。如果有若干条路径都是最短的,那么需要输出最便宜的一条路径。
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <queue>
using namespace std;
typedef pair<int, int> P;
const int INF = 0x3f3f3f3f;
const int maxn = 550;
const int maxm = maxn*maxn;
int tot = 0, head[maxn];
void init() {
tot = 0;
memset(head, -1, sizeof(head));
}
struct Edge {
int from, to, dis, cost, next;
Edge(int f = 0, int t = 0, int d = 0, int c = 0, int n = -1) : from(f), to(t), dis(d), cost(c), next(n) {}
}edge[maxm];
void addedge(int u, int v, int dis, int cost) {
++tot;
edge[tot].to = v, edge[tot].dis = dis, edge[tot].cost = cost;
edge[tot].next = head[u];
head[u] = tot;
}
int n, m, s, e;
bool vis[maxn];
int sdis[maxn], scost[maxn];
void dijkstra() {
for (int i = 0; i <= n; i++) sdis[i] = INF, scost[i] = 0, vis[i] = false;
sdis[s] = 0;
priority_queue<P, vector<P>, greater<P> > q; q.push(make_pair(0, s));
while (!q.empty()) {
int u = q.top().second; q.pop();
if (vis[u]) continue;
vis[u] = true;
for (int i = head[u]; i != -1; i = edge[i].next) {
int v = edge[i].to, dis = edge[i].dis, cost = edge[i].cost;
if (sdis[v] > sdis[u] + dis) {
sdis[v] = sdis[u] + dis;
scost[v] = scost[u] + cost;
q.push(make_pair(sdis[v], v));
}
else if (sdis[v] == sdis[u] + dis && scost[v] > scost[u] + cost) {
scost[v] = scost[u] + cost;
}
}
}
}
int main() {
scanf("%d %d %d %d", &n, &m, &s, &e);
init();
int u, v, cost, dis;
for (int i = 1; i <= m; i++) {
scanf("%d %d %d %d", &u, &v, &dis, &cost);
addedge(u, v, dis, cost);
addedge(v, u, dis, cost);
}
dijkstra();
printf("%d %d\n", sdis[e], scost[e]);
}