PAT(甲级)1003.Emergency(25)

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/ASJBFJSB/article/details/89189615

PAT 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.

输入格式:
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.

输出格式:
For each test case, print in one line two numbers: the number of different shortest paths between C1 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.

输入样例:

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

输出样例:

Case #1:
2 4

题目分析:求解单源最短路径的条数,以及在某条路径上的最大点权之和。本题的图是无向图,如果代码里写成了有向图会出现测试点错误。代码参考晴神的模板。

AC代码:

#include <cstdio>
#include <string>
#include <algorithm>
#include <cstring>
#include <iostream>
#include <vector>
using namespace std;


const int maxv = 510;
const int inf = 0x3fffffff;
int weight[maxv];
int d[maxv],num[maxv],w[maxv];
bool visited[maxv] = {false};
int n, m;

struct Node{
    int v, dis;
    Node(int v, int dis){
        this->v = v;
        this->dis = dis;
    }
};
vector<Node> G[maxv];

void dijkstra(int s){
    for(int i=0; i<maxv; ++i){
        d[i] = inf;
        w[i] = 0;
        num[i] = 0;
    }

    d[s] = 0;
    w[s] = weight[s];
    num[s] = 1; 
    for(int i=0; i<n; ++i){
        int u=-1,MIN=inf;
        for(int j=0; j<n; ++j){
            if(visited[j]==false && d[j]<MIN){
                MIN = d[j];
                u = j;
            }
        }
        if(u==-1)return;
        visited[u] = true;

        for(int j=0; j<G[u].size(); ++j){
            int v = G[u][j].v;
            if(visited[v]==false && d[u] + G[u][j].dis < d[v]){
                d[v] = d[u]+G[u][j].dis;
                w[v] = w[u] + weight[v];
                num[v] = num[u];
            }
            else if(visited[v]==false && d[u]+G[u][j].dis==d[v]){
                if(w[u]+weight[v] > w[v])
                    w[v] = w[u] + weight[v];
                num[v] += num[u];
            }
        }
    }
}


int main(){
    int st,ed;//源点和汇点
    scanf("%d%d%d%d", &n, &m, &st, &ed);
    for(int i=0; i<n; ++i){
        scanf("%d", &weight[i]);
    }

    int u,v,we;
    for(int i=0; i<m; ++i){
        scanf("%d%d%d",&u,&v,&we);
        G[u].push_back(Node(v,we));
        G[v].push_back(Node(u,we));
    }
    dijkstra(st);
    printf("%d %d\n",num[ed], w[ed]);
    return 0;
}

猜你喜欢

转载自blog.csdn.net/ASJBFJSB/article/details/89189615
今日推荐