L2-001 紧急救援 (最短路Dijkstra)(25 分)

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的路径中经过的城市编号。数字间以空格分隔,输出结尾不能有多余空格。

输入样例:

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

题解

跑一遍最短路,记一下最短路中可救援的人数,如果存在多条最短路,则比较路径上的救援队的数目,选择救援队数目多的那条路,由于输入保证救援可行且最优解唯一,这样总是可以找到最优解的。

代码

#include<iostream>
#include<vector>
#include<queue>
static const int INF = 0x3fffffff;
using namespace std;
struct Edge
{
    int t,v;
    Edge(int _t, int _v)
    {
        t = _t, v = _v;
    }
    bool operator<(const Edge &a) const 
    {
        return v > a.v;
    }
};
void dijkstra(const vector<vector<Edge>> &adj, vector<int> &P, int n, int s, int d)
{
    vector<bool> flag(n, false);
    vector<int> D(n, INF);
    vector<int> path(n,-1);
    vector<int> cnt(n,0);
    vector<int> num(n,0);
    D[s] = path[s] = 0;
    cnt[s] = 1;
    num[s] = P[s];
    priority_queue<Edge> pq;
    pq.push(Edge(s, D[s]));
    while(!pq.empty())
    {
        Edge edge = pq.top();pq.pop();
        int u = edge.t;
        if(flag[u])
            continue;
        if(u == d)
            break;
        flag[u] = true;
        for(int i=0;i<adj[u].size();i++)
        {
            int v = adj[u][i].t;
            int wt = adj[u][i].v;
            if(!flag[v])
            {
                int temp = D[u] + wt;
                if(D[v] > temp)
                {
                    D[v] = temp;
                    path[v] = u;
                    cnt[v] = cnt[u];
                    num[v] = num[u] + P[v];
                    pq.push(Edge(v, D[v]));
                }
                else if(D[v] == temp)
                {
                    cnt[v] += cnt[u];
                    if(num[u] + P[v] > num[v])
                    {
                        num[v] = num[u] + P[v];
                        path[v] = u;
                    }
                    pq.push(Edge(v, D[v]));
                }
            }
        }
    }

    cout << cnt[d] << " " << num[d] << endl;
    vector<int> ans;
    int t = d;
    int last = path[t];
    while(t != s)
    {
        ans.push_back(t);
        t = last;
        last = path[t];
    }
    ans.push_back(s);
    for(int i=ans.size()-1;i>=0;i--)
    {
        if(i!=ans.size()-1)
            cout << " ";
        cout << ans[i];
    }
    cout << endl;
}
int main()
{
    int n,m,s,d;
    cin >> n >> m >> s >> d;
    vector<vector<Edge>> adj(n);
    vector<int> P(n);
    for(int i=0;i<n;i++)
        cin >> P[i];
    int t1,t2,v1;
    for(int i=0;i<m;i++)
    {
        cin >> t1 >> t2 >> v1;
        adj[t1].push_back(Edge(t2,v1));
        adj[t2].push_back(Edge(t1,v1));
    }
    dijkstra(adj,P,n,s,d);
    return 0;
}

猜你喜欢

转载自blog.csdn.net/weixin_43264529/article/details/88811949