题目链接
堆优化迪杰斯特拉
#include <iostream>
#include <queue>
#include <vector>
#include <cstring>
using namespace std;
typedef pair<int, int> pii;
const int N = 1010;
struct node {
int to, d, m;
};
vector<vector<node>> v(N);
int n, m;
int dis[N], cos[N];
bool vis[N];
void djk(int s) {
priority_queue<pii> q;
dis[s] = 0;
cos[s] = 0;
q.emplace(0, s);
while (q.size()) {
auto [_, x] = q.top();
q.pop();
if (vis[x]) continue;
vis[x] = true;
for (int i = 0; i < v[x].size(); i++) {
int to = v[x][i].to;
if (dis[to] > dis[x] + v[x][i].d) {
dis[to] = dis[x] + v[x][i].d;
cos[to] = cos[x] + v[x][i].m;
q.emplace(dis[to], to);
} else if (dis[to] == dis[x] + v[x][i].d) {
if (cos[to] > cos[x] + v[x][i].m) {
cos[to] = cos[x] + v[x][i].m;
q.emplace(dis[to], to);
}
}
}
}
}
int main() {
#ifndef ONLINE_JUDGE
freopen("in.txt", "r", stdin);
#endif
while (cin >> n >> m && n && m) {
int a, b, d, p, s, t;
memset(dis, 0x3f, sizeof dis);
memset(cos, 0x3f, sizeof cos);
memset(vis, false, sizeof vis);
for (int i = 0; i < m; i++) {
cin >> a >> b >> d >> p;
v[a].push_back({b, d, p});
v[b].push_back({a, d, p});
}
cin >> s >> t;
djk(s);
cout << dis[t] << " " << cos[t] << endl;
}
return 0;
}