天天看点

[PAT甲级]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

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, 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

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
           

题目大意:

  • 第一行给定n个城市,城市之间有m条路,给定起点c1,终点c2
  • 第二行给定每个城市有救援队数目(点权)
  • 接下来m行给出路径信息 每条路给定起点,终点,路径长度(边权)
  • 输出c1到c2的最短路径条数(可能有多条路径,长度相等),输出最短路径中能带去的最大救援队数量(即最短路径上的最大点权)
  • 其实就是求无向图中两节点间最短路径

思路:

  • 第一反应想到了前段时间算法分析课上学到的动态规划备忘录方法,但是,自己最终还是没有最做出来
  • 后来查查别人的方法,用的是dijkstra算法,其实就是动态规划。
  • 想了解算法的解析博客可以自行百度
  • 我做这道题参考了这个博客,点击链接,具体分析看代码注释

代码:

//求无向图两结点间的最短路径数目,并求出最短路径的最大点权
#include <iostream>
#include <cstdio>
#include <algorithm>
using namespace std;
//城市数目n 路径总数目m 起点c1  终点c2
//dis[i]表示起点到i点的最短路径长度
//weight[i]表示i城市救援队数目,     e[a][b] 表示从a到b路径长度
//num[i]表示从出发点到i城市拥有的最短路径的条数,w[i]表示最短路径上能够找到的救援队的最大数目
int n, m, c1, c2;//默认0
int dis[],weight[],e[][],num[],w[];//默认0
bool visit[];//默认false
const int inf = ;//常数
int main()
{
    //初始化
    scanf("%d%d%d%d",&n, &m, &c1, &c2);
    for(int i=; i<n; i++)
        scanf("%d", &weight[i]);
    fill(e[], e[]+*, inf);//赋值
    fill(dis, dis+, inf);
    for(int i=; i<m; i++){
        int a, b, c;
        scanf("%d%d%d", &a, &b, &c);
        e[a][b] = c;//记录路径长度
        e[b][a] = c;//无向图,所以记录双向路径长度
    }
    dis[c1] = ;
    w[c1] = weight[c1];
    num[c1] = ;
    for(int i=; i<n; i++){
        int u=-,minn = inf;
        for(int j=; j<n; j++){
            if(visit[j]==false && dis[j]<minn){
                u = j;
                minn = dis[j];
            }
        }
        if(u == -)
            break;
        visit[u] = true;
        for(int v=; v<n; v++){
            if(visit[v]==false && e[u][v]!=inf){//路径不包含v点 且u到v点有路径
                if(dis[u] + e[u][v] < dis[v]){//找到更短的路径
                    dis[v] = dis[u] + e[u][v];//最短路径长度更新
                    num[v] = num[u];//最短路径数量更新
                    w[v] = w[u] + weight[v];//最短路径点权更新(救援队数目)
                }else if(dis[u] + e[u][v] == dis[v]){//路径与最短路径长度相等
                    num[v] = num[v] + num[u];//最短路径数量更新
                    if(w[u] + weight[v] > w[v])
                        w[v] = w[u] + weight[v];
                }
            }
        }
    }
    printf("%d %d", num[c2], w[c2]);
    return ;
}
           

注意:

  • 在main方法里定义的变量一定要赋初值,否则系统会随机赋值的。
  • 另外推荐两个链接,人家的题解写的很棒,链接1, 链接2,DFS