#include<cstdio>
#include<algorithm>
#include<cstring>
using namespace std;
const int maxn = 510;
const int INF = 1e9;
int n, m, s, d;
int G[maxn][maxn];
int cost[maxn][maxn];
int pre[maxn], dis[maxn],acost[maxn];
bool vis[maxn] = { 0 };
void dijkstra(int st)
{
fill(dis, dis + maxn, INF);
fill(acost, acost + maxn, INF);
for (int i = 0; i < n; i++) { pre[i] = i; }
dis[st] = 0;
acost[st] = 0;
for (int i = 0; i < n; i++)
{
int u = -1, min = INF;
for (int j = 0; j < n; j++)
{
if (vis[j] == 0 && dis[j] < min)
{
u = j;
min = dis[j];
}
}
if (u == -1) return;
vis[u] = 1;
for (int v = 0; v < n; v++)
{
if (vis[v] == 0 && G[u][v] != INF)
{
if (G[u][v] + dis[u] < dis[v])
{
dis[v] = G[u][v] + dis[u];
acost[v] = acost[u] + cost[u][v];
pre[v] = u;
}
else if (G[u][v] + dis[u] == dis[v])
{
if (acost[v] > acost[u] + cost[v][u])
{
acost[v] = acost[u] + cost[u][v];
pre[v] = u;
}
}
}
}
}
}
void dfs(int v)
{
if (v == s)
{
printf("%d ", v);
return;
}
dfs(pre[v]);
printf("%d ", v);
}
int main()
{
fill(G[0], G[0] + maxn * maxn, INF);
//fill(cost[0], cost[0] + maxn * maxn, INF);
int c1,c2,c, distance;
scanf("%d%d%d%d", &n, &m, &s, &d);
for (int i = 0; i < m; i++)
{
scanf("%d%d%d%d", &c1, &c2, &distance, &c);
G[c1][c2] = G[c2][c1] = distance;
cost[c1][c2] = cost[c2][c1] = c;
}
dijkstra(s);
dfs(d);
printf("%d %d", dis[d], acost[d]);
return 0;
}
A1030 Travel Plan (30 分)
最新推荐文章于 2020-08-16 17:14:32 发布