PAT 1003 Emergency(Dijstra算法解决)

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 (≤) - the number of cities (and the cities are numbered from 0 to N1), M - the number of roads, C1​​ and C2​​ - 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 c1​​, c2​​ 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 C1​​ to C2​​.

Output Specification:

For each test case, print in one line two numbers: the number of different shortest paths between C1​​ and C2​​, 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

使用Dijstra算法解决即可。找出最短路的数量以及计算所有最短路中能找到的最多队伍数目。注意本题是无向图,输入时要记得将处理,以及如果在同一通路上,则下一点的最短路数量应为前一点的最短路数量,
如果有另外一条最短路,则下一点的最短路数量应加上前一点的最短路数量。
参考代码如下:
#include<cstdio>
#include<string>
#include<cstring>
#include<sstream>
#include<iostream>
#include<cmath>
#include<queue>
#define INF 0xfffffff;
const int maxn=505;
using namespace std;
struct Edge{
    int from, to, dist;///起点,终点,边距离
    Edge(int u, int v, int d) : from(u), to(v), dist(d){}
};

struct HeapNode{///堆结点,保存每个边的距离以及进行比较
    int d,u;
    bool operator < (const HeapNode& rhs) const{
        return d>rhs.d;
    }
};

struct Dijstra{
    int n,m;
    vector <Edge> edges;///边数组
    vector <int> G[maxn];///保存每个起点所对应的边
    bool done[maxn];///标记数组
    int d[maxn];///到每个点的距离
    int p[maxn];///每个点的前一个点
    int num[maxn];///最短路的数量
    int value[maxn];///每条边的权值
    int w[maxn];///起点到每个结点的最大权值
    void init(int n){
        this ->n =n;
        for(int i=0;i<n;i++) G[i].clear();
        edges.clear();
    }

    void AddEdge(int from, int to, int dist){
        edges.push_back(Edge(from,to,dist));
        m = edges.size();
        G[from].push_back(m-1);///将起点所对应的边放进图中
    }

    void dijstra(int s){
        priority_queue <HeapNode> Q;
        for(int i=0;i<n;i++) d[i]=INF;
        memset(w,0,sizeof(w));
        memset(num,0,sizeof(num));
        d[s] = 0;///起点距离设为0
        w[s] = value[s];///初始权重即为起点权重
        num[s] = 1;///初始最短路径数目为一
        memset(done, 0, sizeof(done));
        Q.push((HeapNode){0,s});
        while(!Q.empty()){
            HeapNode x= Q.top();Q.pop();
            int u=x.u;
            if(done[u]) continue;
            done[u] = true;
            for(int i=0; i<G[u].size();i++){
                Edge& e = edges[G[u][i]];
                if(d[e.to]>d[u]+e.dist){///如果到下一点的距离小于当前距离
                    d[e.to] = d[u]+e.dist;
                    p[e.to] = G[u][i];///将前一个节点存储
                    Q.push((HeapNode){d[e.to],e.to});///下一个节点进队列
                    w[e.to] = w[u]+value[e.to];
                    num[e.to] = num[u];///如果在同一通路上,则下一点的最短路数量应为前一点的最短路数量
                }
                else if(d[e.to]==d[u]+e.dist){///如果找到距离相同的路
                    if(w[e.to]<w[u]+value[e.to]) {///找出最大权值
                        w[e.to] = w[u]+value[e.to];
                    }
                    p[e.to] = G[u][i];///将前一个节点存储
                    Q.push((HeapNode){d[e.to],e.to});///下一个节点进队列
                    num[e.to] += num[u];///如果有另外一条最短路,则下一点的最短路数量应加上前一点的最短路数量
                }
            }
        }
    }
};

int main(){
    int n,m,beg,des,u,v,d;
    Dijstra dij;
    while(~scanf("%d%d%d%d",&n,&m,&beg,&des)){
        dij.init(n);
        for(int i=0;i<n;i++){
            scanf("%d",&dij.value[i]);
        }
        for(int i=0;i<m;i++){
            scanf("%d%d%d",&u,&v,&d);
            dij.AddEdge(u,v,d);
            dij.AddEdge(v,u,d);
        }
        dij.dijstra(beg);
        printf("%d %d",dij.num[des],dij.w[des]);
    }
    return 0;
}

  

猜你喜欢

转载自www.cnblogs.com/sapphirebitter/p/11446642.html