#include <bits/stdc++.h>
#define N 505
using namespace std;
int n, m, st, ed;
int g[N][N], dist[N], people_num[N], road_cnt[N], people_sum[N], pre[N];
bool ct[N];
void dijkstra()
{
dist[st] = 0; //初始化路径长度
road_cnt[st] = 1; //初始化方案数量
people_sum[st] = people_num[st]; //初始化一开始的人数
for(int i = 0; i < n; ++i)
{
int hunting = -1;
for(int j = 0; j < n; ++j)
if(!ct[j] && (hunting == -1 || dist[j] < dist[hunting]))
hunting = j;
if(hunting == -1) break;
ct[hunting] = 1;
for(int j = 0; j < n; ++j)
if(!ct[j] && dist[j] > dist[hunting] + g[hunting][j])
{
dist[j] = dist[hunting] + g[hunting][j]; //更新最短路
road_cnt[j] = road_cnt[hunting]; //记录路径条数(不同方案数量, 而不是所需要经过的路径条数)
pre[j] = hunting; //记录前驱城市
people_sum[j] = people_sum[hunting] + people_num[j]; //当前救援队sum = 上个城市救援队sum + 这个城市救援队num
}else if(!ct[j] && dist[j] == dist[hunting] + g[hunting][j])
{
road_cnt[j] += road_cnt[hunting];
if(people_sum[hunting] + people_num[j] > people_sum[j])
{
people_sum[j] = people_sum[hunting] + people_num[j];
pre[j] = hunting;
}
}
}
}
void dfs(int k)
{
if(k == st)
{
cout << st;
return;
}
dfs(pre[k]);
cout << " " << k;
}
int main()
{
memset(g, 0x3f, sizeof g);
memset(dist, 0x3f, sizeof dist);
memset(pre, -1, sizeof pre);
cin >> n >> m >> st >> ed;
for(int i = 0; i < n; ++i) cin >> people_num[i]; //救援队数量
int a, b, c;
for(int i = 0; i < m; ++i)
{
cin >> a >> b >> c;
g[a][b] = g[b][a] = c;
}
dijkstra();
cout << road_cnt[ed] << " " << people_sum[ed] << endl;
dfs(ed);
return 0;
}
7-3 (选做) 城市间紧急救援 (25 分)
最新推荐文章于 2023-04-07 20:57:34 发布