7-3 (选做) 城市间紧急救援 (25 分)

#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;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值