#include <bits/stdc++.h>
using namespace std;
const int N = 510;
int n, m, S, T;
int e[N][N], dist[N]; // ×î¶Ì¾àÀë
int c[N][N], cost[N]; // ×îÉÙ»¨·Ñ
int pre[N]; // ¼Ç¼·¾¶
bool st[N];
void dijkstra()
{
memset(dist, 0x3f, sizeof dist);
memset(cost, 0x3f, sizeof cost);
dist[S] = 0, cost[S] = 0;
for(int i = 0; i < n - 1; i ++)
{
int t = -1;
for(int j = 0; j < n; j ++)
if(!st[j] && (t == -1 || dist[j] < dist[t]))
t = j;
st[t] = true;
for(int j = 0; j < n; j ++)
{
if(dist[j] > dist[t] + e[t][j])
{
dist[j] = dist[t] + e[t][j];
cost[j] = cost[t] + c[t][j];
pre[j] = t;
}
else if(dist[j] == dist[t] + e[t][j] && cost[j] > cost[t] + c[t][j])
{
cost[j] = cost[t] + c[t][j];
pre[j] = t;
}
}
}
}
int main()
{
memset(e, 0x3f, sizeof e);
memset(c, 0x3f, sizeof c);
cin >> n >> m >> S >> T;
for(int i = 0; i < m; i++)
{
int a, b, x, y;
cin >> a >> b >> x >> y;
e[a][b] = e[b][a] = x;
c[a][b] = c[b][a] = y;
}
dijkstra();
vector<int> route;
for(int i = T; i != S; i = pre[i]) route.push_back(i);
cout << S;
for(int i = route.size() - 1; i >= 0; i --) cout << ' ' << route[i];
cout << ' ' << dist[T] << ' ' << cost[T] << endl;
}
【PAT甲级】1030 Travel Plan(dijkstra单源最短路)
最新推荐文章于 2022-08-12 11:26:28 发布