最短路_1003 Emergency (25 分)

1003 Emergency (25 分)

As an emergency rescue team leader of a city, you are given a special map of your country. The map shows several scattered cities connected by some roads. Amount of rescue teams in each city and the length of each road between any pair of cities are marked on the map. When there is an emergency call to you from some other city, your job is to lead your men to the place as quickly as possible, and at the mean time, call up as many hands on the way as possible.

Input Specification:

Each input file contains one test case. For each test case, the first line contains 4 positive integers: N (≤500) - the number of cities (and the cities are numbered from 0 to N−1), M - the number of roads, C​1​​ and C​2​​ - the cities that you are currently in and that you must save, respectively. The next line contains N integers, where the i-th integer is the number of rescue teams in the i-th city. Then M lines follow, each describes a road with three integers c​1​​, c​2​​ and L, which are the pair of cities connected by a road and the length of that road, respectively. It is guaranteed that there exists at least one path from C​1​​ to C​2​​.

Output Specification:

For each test case, print in one line two numbers: the number of different shortest paths between C​1​​ and C​2​​, and the maximum amount of rescue teams you can possibly gather. All the numbers in a line must be separated by exactly one space, and there is no extra space allowed at the end of a line.

Sample Input:

5 6 0 2
1 2 1 5 3
0 1 1
0 2 2
0 3 1
1 2 1
2 4 1
3 4 1

Sample Output:

2 4

题目中要求求出最短路的条数,以及所有最短路中,所经过节点权值最打的一条路的权值,使用per数组保存路径,当长度相等时,更新per数组,以及最短路条数即可

剩余的即为最短路算法

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
#include <queue>
#include <cmath>
#include <set>
#define INF 0x3f3f3f3f
#define ll long long
using namespace std;
const int maxn = 550;
const int maxm = 320000;
int N,M,S,T,team[maxn],k;
int head[maxn],next1[maxm],u[maxm],v[maxm],w[maxm],dis[maxn],per[maxn],count1[maxn];
bool used[maxn];
struct Node{
    int pos,cost;
    bool operator < (const Node &a)const{
        return cost > a.cost;
    }
};

void Add_edge(int x,int y,int z)
{
    u[k] = x;v[k] = y;w[k] = z;next1[k] = head[x];head[x] = k++;
    u[k] = y;v[k] = x;w[k] = z;next1[k] = head[y];head[y] = k++;
}

int get_ans(int pos)
{
    int ans = 0;
    while(pos != per[pos]){
        ans += team[pos];pos = per[pos];
    }

    return ans + team[pos];
}
void Dijkstar()
{
    memset(dis,INF,sizeof(dis));
    for(int i = 0;i < N;i ++) per[i] = i;
    priority_queue<Node> que;
    Node p,temp;p.cost = 0;p.pos = S;count1[S] = 1;
    que.push(p);
    dis[S] = 0;
    while(!que.empty())
    {
        p = que.top();que.pop();
        if(used[p.pos])
            continue;
        used[p.pos] = true;
        int root = head[p.pos];
        while(root != -1)
        {
            if(dis[v[root]] == dis[p.pos] + w[root])
            {
                count1[v[root]] += count1[p.pos];
                if(get_ans(v[root]) < get_ans(p.pos) + team[v[root]])
                    per[v[root]] = p.pos;
            }
            else if(dis[v[root]] > dis[p.pos] + w[root])
            {
                dis[v[root]] = dis[p.pos]  + w[root];
                per[v[root]] = p.pos;
                if(!used[v[root]])
                {
                    temp.cost = dis[v[root]];
                    count1[v[root]] = count1[p.pos];
                    temp.pos = v[root];
                    que.push(temp);
                }
            }
            root = next1[root];
        }
    }
}
int main()
{
    memset(head,-1,sizeof(head));
    scanf("%d%d%d%d",&N,&M,&S,&T);
    for(int i = 0;i < N;i ++)   scanf("%d",team+i);

    int x,y,z;
    for(int i = 0;i < M;i ++)
    {
        scanf("%d%d%d",&x,&y,&z);
        Add_edge(x,y,z);
    }
    Dijkstar();
    int ans = 0;
    printf("%d %d\n",count1[T],get_ans(T));
}

猜你喜欢

转载自blog.csdn.net/li1615882553/article/details/84637939