#include <iostream>
#include <stack>
using namespace std;
const int INF = 0x6fffffff;
const int MAX = 501;
int map[MAX][MAX], value[MAX][MAX], dist[MAX], cost[MAX], pre[MAX], visit[MAX];
stack<int> path;
void init(int n)
{
for (int i = 0; i < n; i++)
{
visit[i] = 0;
pre[i] = -1;
for (int j = 0; j < n; j++)
{
map[i][j] = INF;
value[i][j] = INF;
}
}
}
void dijkstra(int start, int end, int n)
{
int mind, v;
for (int i = 0; i < n; i++)
{
dist[i] = map[start][i];
cost[i] = value[start][i];
}
visit[start] = 1;
for (int i = 1; i < n; i++)
{
mind = INF;
for (int j = 0; j < n; j++)
{
if (visit[j] == 0 && dist[j] < mind)
{
mind = dist[j];
v = j;
}
}
visit[v] = 1;
if (v == end) return;
for (int j = 0; j < n; j++)
{
if (visit[j] == 0 && map[v][j] < INF)
{
if (dist[j] > dist[v] + map[v][j])
{
dist[j] = dist[v] + map[v][j];
cost[j] = cost[v] + value[v][j];
pre[j] = v;
}
else if (dist[j] == dist[v] + map[v][j] && cost[j] > cost[v] + value[v][j])
{
cost[j] = cost[v] + value[v][j];
pre[j] = v;
}
}
}
}
}
int main()
{
int N, M, S, D;
cin >> N >> M >> S >> D;
init(N);
int x, y, d, c;
for (int i = 0; i < M; i++)
{
cin >> x >> y >> d >> c;
if (map[x][y] > d)
{
map[x][y] = map[y][x] = d;
value[x][y] = value[y][x] = c;
}
else if (map[x][y] == d && value[x][y] > c)
{
value[x][y] = value[y][x] = c;
}
}
if (S == D)
{
cout << S << " " << D << "0 0" << endl;
return 0;
}
dijkstra(S, D, N);
int p = pre[D];
while (p != -1)
{
path.push(p);
p = pre[p];
}
cout << S;
while (!path.empty())
{
cout << " " << path.top();
path.pop();
}
cout << " " << D << " " << dist[D] << " " << cost[D] << endl;
}
参考:
http://blog.youkuaiyun.com/iaccepted/article/details/21322745