1003 Emergency

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

#include<iostream>
#include<algorithm>
#include<iomanip>
#include <ctype.h>
#include<string.h>
#include<string>
#include<cstring>
#include<stdio.h>
#include<stack>
# define INF 0x3f3f3f3f //无穷大
using namespace std;

int N;//<=500,城市数量
int M;//道路数量
int C1, C2;//C1→C2
int teams[510];//每个城市救援队数量
int G[510][510];//邻接矩阵

int dis[510];//从出发点到各城市最短路径长度 
int vis[510]={0};//节点是否被访问

int road_num[510];//最短路径数量
int max_team_num[510];//最多救援队数量

void dij(int s) {
    //初始化
    dis[s] = 0;
    max_team_num[s] = teams[s];
    // vis[s] = 1;
    road_num[s] = 1;
    for (int i = 0; i < N; i++) { //寻找未被访问过且dis最短的点,第一次循环即是出发点
        //外层循环n次,找到从出发点到所有顶点的最短路径
        int u = -1, min = INF;//u记录dis最小点,min最小点对应的dis
        for (int j = 0; j < N; j++) {
            //找出最短dis的点,第一次为出发点
            if (vis[j] == 0 && dis[j] < min) {
                u = j;
                min = dis[j];
            }
        }
        if (u == -1)break;//没找到就跳出
        vis[u] = 1;//记录被访问过
        //通过已加入最短路径的点,更新通过这个点的路径到其他点的dis
        for (int k = 0; k < N; k++) {
            if (vis[k] == 0 && G[u][k] != INF) {//该点未被访问且经过已记录的点可以到别的点
                if (dis[u] + G[u][k] < dis[k]) {
                    dis[k] = dis[u] + G[u][k];//更新dis为经过记录点
                    road_num[k] = road_num[u];//路径条数
                    max_team_num[k] = max_team_num[u] + teams[k];//更新救援队数量
                }
                else if (dis[u] + G[u][k] == dis[k]) {//相等就表示最短路径多一条,就判断救援队哪一条路多
                    road_num[k] = road_num[k] + road_num[u];
                    if (max_team_num[u] + teams[k] > max_team_num[k]) {
                        max_team_num[k] = max_team_num[u] + teams[k];
                    }
                }
            }
        }
    }
}

//迪杰斯特拉求最短路径
int main() {
    cin >> N;
    cin >> M;
    cin >> C1 >> C2;
    for (int i = 0; i < N; i++) {
        cin >> teams[i];
    }
    for (int i = 0; i < 510; i++) {
        for (int j = 0; j < 510; j++) {
            G[i][j] = INF;
        }
    }
    int hang, lie;
    for (int i = 0; i < M; i++) {
        cin >> hang >> lie;
        cin >> G[hang][lie];
        G[lie][hang]=G[hang][lie];//无向图对称
    }
    for (int i = 0; i < 510; i++) {
        dis[i] = INF;
    }
    dij(C1);
    cout << road_num[C2] << ' ' << max_team_num[C2];
}