L2-001 紧急救援

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

输入格式:

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

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

思路

求两点之间的最短距离,再观察一下数据范围,不难发现使用普通的Dijistra算法就行,不用优化。

问题就是要求出,最短路径有几条,在最短路径中能召集到的最多救援团队,并且求这个最短路径经过的点。我们可以先写出Dijistra算法,写的时候就能意识到题目要求的数据都能在Dijistra里求出,是该算法使用时同时能得到的数据,用数组记录下来就行。

code


#include<iostream>
#include<algorithm>
#include<queue>
#include<string>
#include<vector>
#include<cstring>
#include<map>
#include<set>
#include<unordered_set>
#include<stack>
using namespace std;
const int manx = 2e5 + 10;

int a[manx];
int b[manx];

int g[510][510];
const int INF = 0x3f;
int dist[510];
bool st[510];
int n, m, s, d;
int sum = 0;
int cnt[510];//记录最短路径有多少
int pre[510];//用来记录节点前驱的,比如pre[2]=3,就说明2前面是3,也就是3->2
void dijistra()
{
    memset(cnt, 0, sizeof(cnt));
    memset(dist, INF, sizeof(dist));
    dist[s] = 0;
    cnt[s] = 1;
    b[s] = a[s];
    for (int i = 0; i < n-1; i++)
    {
        int t = -1;
        for (int j = 0; j < n; j++)
        {
            if (!st[j] && (t == -1 || dist[t] > dist[j]))
            {
                t=j;
            }
        }
        st[t] = true;
        for (int j = 0; j < n; j++)
        {
            if (dist[j] > dist[t] + g[t][j])
            {
                dist[j] = dist[t] + g[t][j];
                cnt[j] = cnt[t];
                b[j] = b[t] + a[j];
                pre[j] = t;
            }
            else if (dist[j] == dist[t] + g[t][j])//相等就不用更新dist,更新其他
            {
                cnt[j] += cnt[t];
                if (b[j] < b[t] + a[j])
                {
                    b[j] = b[t] + a[j];
                    pre[j] = t;
                }
            }
        }
    }
}
void solve()
{    cin >> n>>m>>s>>d;
    memset(g, INF, sizeof(g));
    
    for (int i = 0; i < n; i++)
    {
        cin >> a[i];
    }
    for (int i = 0; i < m; i++)
    {
        int x, y,c;
        cin >> x >> y>>c;
        g[x][y] = min(g[x][y], c);
        g[y][x] = min(g[y][x], c);
    }
    dijistra();
    cout << cnt[d] << " " << b[d] << endl;
    stack<int>k;
    int pot = d;//pre数组里没有记录d所以要单独输出。
    while (d!=s)
    {
        k.push(pre[d]);
        d = pre[d];
    }//反转一下数组,因为是s到d
    while (!k.empty())
    {
        cout << k.top()<<" ";
        k.pop();
    }
    cout << pot;
}
int main()
{
    int t;
    ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
    //cin >> t;
    t = 1;
    while (t--)
    {
        solve();
    }
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值