L2-001 紧急救援 (25 分)

题目描述

作为一个城市的应急救援队伍的负责人,你有一张特殊的全国地图。在地图上显示有多个分散的城市和一些连接城市的快速道路。每个城市的救援队数量和每一条连接两个城市的快速道路长度都标在地图上。当其他城市有紧急求助电话给你的时候,你的任务是带领你的救援队尽快赶往事发地,同时,一路上召集尽可能多的救援队。

输入格式:

输入第一行给出4个正整数N、M、S、D,其中N(2≤N≤500)是城市的个数,顺便假设城市的编号为0 ~ (N−1);M是快速道路的条数;S是出发地的城市编号;D是目的地的城市编号。

第二行给出N个正整数,其中第i个数是第i个城市的救援队的数目,数字间以空格分隔。随后的M行中,每行给出一条快速道路的信息,分别是:城市1、城市2、快速道路的长度,中间用空格分开,数字均为整数且不超过500。输入保证救援可行且最优解唯一。

输出格式:

第一行输出最短路径的条数和能够召集的最多的救援队数量。第二行输出从S到D的路径中经过的城市编号。数字间以空格分隔,输出结尾不能有多余空格。

思路

1. 最短路采用Dijkstra算法, 同时选择相同长度时救援数更多的路径 记录路径数和路径.

2. 保存路径的方法:记录前一个节点 如: pre[1] = 2, pre[2] = 3   =>  3 -> 2 - > 1.

3.记录路径数cnt时: 距离更短的应该使用 = 覆盖, 距离相同的应该使用 += 累加.

代码1(链式向前星存图)

#include <bits/stdc++.h>
#define PII pair<int, int>
typedef long long LL;
const int inf = 0x3f3f3f3f;
const int N = 500005;
using namespace std;

int n, m, s, d;
int hp[N], dis[N], cnt[N], resc[N], pre[N], vis[N];
int h[N], v[N], nxt[N], w[N], idx = 1;    //链式向前星

void add(int x, int y, int z){    //一条 x->y 权值为z的边
    w[idx] = z;
    v[idx] = y;
    nxt[idx] = h[x];
    h[x] = idx++;
}

void dij(){
    memset(dis, 0x3f, sizeof(dis));
    priority_queue<PII, vector<PII>, greater<PII>> q;
    q.push({0, s});
    dis[s] = 0;
    resc[s] = hp[s];
    cnt[s] = 1;

    while(q.size()){
        auto p = q.top(); q.pop();
        int x = p.second;
        if(vis[x]) continue;
        vis[x] = 1;
        for(int i = h[x]; i != -1; i = nxt[i]){
            int y = v[i];
            if(dis[y] > dis[x] + w[i]){     //路径更短
                dis[y] = dis[x] + w[i];
                cnt[y] = cnt[x];            //路径条数
                resc[y] = resc[x] + hp[y];  //救援数量
                pre[y] = x;                 //保存路径
            }else if(dis[y] == dis[x] + w[i]){  //路径相同时
                cnt[y] += cnt[x];
                if(resc[y] < resc[x] + hp[y]){  //选择救援数更多的路
                    resc[y] = resc[x] + hp[y];
                    pre[y] = x;
                }
            }
            q.push({dis[y], y});
        }
    }
}
void print(int n){    //递归打印
    if(n != s) print(pre[n]);
    if(n == s)
        cout << n;
    else
        cout << " " << n;
}

int main(){
    memset(h, -1, sizeof(h));
    cin >> n >> m >> s >> d;
    for(int i = 0; i < n; i++) cin >> hp[i];
    for(int i = 0; i < m; i++){
        int a, b, c;
        cin >> a >> b >> c;
        add(a, b, c);
        add(b, a, c);
    }
    dij();
    cout << cnt[d] << " " << resc[d] << endl;
    print(d);
    return 0;
}

代码2(邻接表存图)

#include <bits/stdc++.h>
#define PII pair<int, int>
typedef long long LL;
const int inf = 0x3f3f3f3f;
const int N = 500005;
using namespace std;

int n, m, s, d;
int hp[N], dis[N], cnt[N], resc[N], pre[N], vis[N];
vector<PII> g[N];   //g[x] = {y, z}  => 存储 x->z 权值为 z 的边

void dij(){
    memset(dis, 0x3f, sizeof(dis));
    priority_queue<PII, vector<PII>, greater<PII>> q;
    q.push({0, s});
    dis[s] = 0;
    resc[s] = hp[s];
    cnt[s] = 1;

    while(q.size()){
        auto p = q.top(); q.pop();
        int x = p.second;
        if(vis[x]) continue;
        vis[x] = 1;
        for(auto edge : g[x]){  //所有的邻边
            int y = edge.first, w = edge.second;
            if(dis[y] > dis[x] + w){     //路径更短
                dis[y] = dis[x] + w;
                cnt[y] = cnt[x];            //路径条数
                resc[y] = resc[x] + hp[y];  //救援数量
                pre[y] = x;                 //保存路径
            }else if(dis[y] == dis[x] + w){  //路径相同时
                cnt[y] += cnt[x];
                if(resc[y] < resc[x] + hp[y]){  //选择救援数更多的路
                    resc[y] = resc[x] + hp[y];
                    pre[y] = x;
                }
            }
            q.push({dis[y], y});
        }
    }
}
void print(int n){    //递归打印
    if(n != s) print(pre[n]);
    if(n == s)
        cout << n;
    else
        cout << " " << n;
}

int main(){
    cin >> n >> m >> s >> d;
    for(int i = 0; i < n; i++) cin >> hp[i];
    for(int i = 0; i < m; i++){
        int a, b, c;
        cin >> a >> b >> c;
        g[a].push_back({b,c});
        g[b].push_back({a,c});  //双向边
    }
    dij();
    cout << cnt[d] << " " << resc[d] << endl;
    print(d);
    return 0;
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值