程序设计天梯赛——L2-001 紧急救援

144 阅读2分钟

程序设计天梯赛——L2-001 紧急救援

题目详情 - L2-001 紧急救援 (pintia.cn)

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

输入格式:

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

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

输出格式:

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

输入样例:

4 5 0 3
20 30 40 10
0 1 1
1 3 2
0 3 3
0 2 2
2 3 2

输出样例:

2 60
0 1 3

问题解析

dijkstra求最短路,中途维护带着的救援队数量即可。

AC代码

#include<iostream>
using namespace std;
#include<vector>
#include<algorithm>
#include<math.h>
#include<set>
#include <random>
#include<numeric>
#include<string>
#include<string.h>
#include<iterator>
#include<fstream>
#include<map>
#include<unordered_map>
#include<stack>
#include<list>
#include<queue>
#include<iomanip>
#include<bitset>
​
//#pragma GCC optimize(2)
//#pragma GCC optimize(3)#define endl '\n'
#define int ll
#define PI acos(-1)
#define INF 0x3f3f3f3f
typedef long long ll;
typedef unsigned long long ull;
typedef pair<ll, ll>PII;
const int N = 2e5 + 50, MOD = 998244353;
​
vector<int>tree[N];
unordered_map<int, unordered_map<int, int>>mymap;
int n, m, f[N], st[N], w[N], ans[N], path[N], num[N];
​
void dijkstra(int x, int y)
{
    while (1)
    {
        int mn = 1e9, idx = -1;
        for (int i = 0; i < n; i++)
        {
            if (f[i] < mn && !st[i])
            {
                mn = f[i];
                idx = i;
            }
        }
        if (idx == -1)return;
        st[idx] = 1;
        for (auto& i : tree[idx])
        {
            if (!st[i]&&i == idx)continue;
            if (f[i] > f[idx] + mymap[idx][i])
            {
                f[i] = mymap[idx][i] + f[idx];
                ans[i] = ans[idx] + w[i];
                path[i] = idx;
                num[i] = num[idx];
                
            }
            else if(!st[i]&&f[i]==f[idx]+mymap[idx][i])
            {
                if (ans[i] < ans[idx] + w[i])
                {
                    ans[i] = ans[idx] + w[i];
                    path[i] = idx;
                }
                num[i] +=  num[idx];
            }
        }
    }
}
​
void solve()
{
    int x, y, z, s, d;
    cin >> n >> m >> s >> d;
    for (int i = 0; i < n; i++)
    {
        f[i] = 1e9;
        cin >> w[i];
        path[i] = -1;
    }
    for (int i = 0; i < m; i++)
    {
        cin >> x >> y >> z;
        
        
            tree[x].push_back(y);
            tree[y].push_back(x);
            mymap[x][y] = z;
            mymap[y][x] = z;
        
    }
    num[s] = 1;
    f[s] = 0;
    ans[s] = w[s];
    dijkstra(s, d);
​
    vector<int>v;
    v.push_back(d);
    x = d;
    while (path[x] != -1)
    {
        v.push_back(path[x]);
        x = path[x];
    }
    //sort(v.begin(), v.end());
    cout << num[d] << " " << ans[d] << endl;
    for (int i = v.size()-1; i >= 0; i--)
    {
        cout << v[i];
        if (i != 0)cout << " ";
    }
    
}
​
signed main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    cout.tie(nullptr);
    int t = 1;
   // cin >> t;
    while (t--)
    {
        solve();
    }
    return 0;
}