L2-001. 紧急救援(最短路的变形)*

时间:2024-01-06 16:11:38

L2-001. 紧急救援

 #include <cstdio>
#include <algorithm>
using namespace std;
int const MAX = ;
int const INF = 0x3fffffff;
int mp[MAX][MAX], val[MAX], path[MAX], dis[MAX], re[MAX], totval[MAX], pathnum[MAX];
bool vis[MAX];
int n, m, s, d; void Dijkstra(int v0)
{
for(int i = ; i < n; i++)
dis[i] = INF;
vis[v0] = true;
dis[v0] = ;
totval[v0] = val[v0];
pathnum[v0] = ;
for(int i = ; i < n; i++)
{
if(mp[v0][i] != INF && i != v0)
{
dis[i] = mp[v0][i];
path[i] = v0;
totval[i] = val[v0] + val[i];
pathnum[i] = ;
}
}
for(int i = ; i < n - ; i++)
{
int mi = INF, mival = , u = v0;
for(int j = ; j < n; j++)
{
if(!vis[j] && dis[j] < mi)
{
mi = dis[j];
u = j;
}
}
vis[u] = true;
for(int j = ; j < n; j++)
{
if(!vis[j])
{
if(dis[u] + mp[u][j] < dis[j])
{
pathnum[j] = pathnum[u];
dis[j] = dis[u] + mp[u][j];
totval[j] = totval[u] + val[j];
path[j] = u;
}
else if(dis[u] + mp[u][j] == dis[j])
{
pathnum[j] += pathnum[u];
if(totval[j] < totval[u] + val[j])
{
totval[j] = totval[u] + val[j];
path[j] = u;
}
}
}
}
}
} int main()
{
scanf("%d %d %d %d", &n, &m, &s, &d);
for(int i = ; i < n; i++)
scanf("%d", &val[i]);
for(int i = ; i < n; i++)
for(int j = ; j < n; j++)
mp[i][j] = INF;
int x, y, l;
for(int i = ; i < m; i++)
{
scanf("%d %d %d", &x, &y, &l);
mp[x][y] = min(mp[x][y], l);
mp[y][x] = mp[x][y];
}
Dijkstra(s);
int num = , cur = d;
while(cur != s)
{
re[num ++] = cur;
cur = path[cur];
}
re[num ++] = s;
printf("%d %d\n", pathnum[d], totval[d]);
for(int i = num - ; i > ; i--)
printf("%d ", re[i]);
printf("%d\n", re[]);
}