#include

PAT 1003 Emergency (25)(25 分)

1003 Emergency (25)(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

题目翻译:

1003.突发事件(25)

作为一个城市紧急援救队的指挥者,你得到了一个国家的特殊地图。地图上分散着几座城市,城市间用道路连接着。每个城市援救队的数量以及两座城市之间每条道路的长度已经在地图上标出。当某些城市发生了突发事件,需要你的帮助时,你的工作是带领你的队伍尽快的赶到事发现场,与此同时,召集尽可能多的在路上的队伍。

 

输入

每个输入文件包含一个测试实例。每个实例的第一行有四个正整数:N(<= 500)是城市的个数(城市的编号从0到N-1),M是道路的个数,C1和C2分别是你现在所在的城市以及你必须去救援的城市。下一行有N个整数,第i个整数是第i个城市中救援队的数量。然后下面有M行,每行表示一条道路。每一行有三个整数c1,c2和L,分别表示道路连接的两个城市以及道路的长度。保证C1到C2之间存在至少一条路径。

 

输出

对于每个测试实例,在一行中输出两个数字:C1和C2之间不同的最短路径的个数,你能聚集起来的最多的救援队数量。

一行中的所有数字必须被一个空格分隔开,在每行的结尾不允许出现空格。



// Dijkstra 算法变形 

#include <iostream>
#include <algorithm>

using namespace std; 

#define maxn 1000
#define INF 99999999

int map_dis[maxn][maxn]; 
int Num[maxn];  // Num[i] : 从 C1 到 i 有多少条路径  

int Team[maxn]; // Team[i] : 从 C1 到 i 每一条路径中,最大的一条路径。
int locateTeam[maxn]; // locateTeaam[i] 表示 :  位置 i 上已经有救援队的数量 

int dis[maxn];  // 从 C1 到 i 的最短路径距离 

int N, M, C1, C2; 
int Start, End, Weight; 

bool visit[maxn]; 

// 将 C1 到 C1 的距离 设置成 0, C1 不可以一步到达的点 设置成INF , 由于这里面有一部分点不能再 Dijkstra 松弛部分松弛,
// 则这些点可能出现 Num[i] 和 Team[i] 没有设置成正确形式的情况 , 所以提前设置好 
void init_1() {
    for (int i = 0; i < N; i++) {
        dis[i] = map_dis[C1][i]; // 初始化 C1 到 i 的距离 

        if (dis[i] < INF) { // C1 可以 一步到达 i (不能够再)
            Num[i] = 1;
            Team[i] = locateTeam[C1] + locateTeam[i]; //  
        }
        else {
            Num[i] = 0;
            Team[i] = locateTeam[i];
        }
        visit[i] = false;
    }
    Num[C1] = 1; Team[C1] = locateTeam[C1];
    dis[C1] = 0;  visit[C1] = true;
}

// 除了 C1 之外 , 所有位置距离C1的距离 都是 INF ,把点的信息 放到松弛的地方处理
void init_2() {
    for (int i = 0; i < N; i++) {
        dis[i] = INF; //  map_dis[C1][i]; // 初始化 C1 到 i 的距离 

        visit[i] = false;
    }
    Num[C1] = 1; Team[C1] = locateTeam[C1];
    dis[C1] = 0;
}

void Dijkstra() {
    // 两种初始化 方式都可以 
    //init_1(); 
    init_2(); 

    for (int times = 0; times < N; times++) {
        int pos = 0, max_dis = INF; 

        for (int i = 0; i < N; i++) {
            if (!visit[i] && max_dis > dis[i]) {
                pos = i; // 找到当前距离 C1 最近的 新点 
                max_dis = dis[i]; 
            }
        }
        visit[pos] = true; 

        if (pos == C2) {
            break; 
        }

        // cout << Num[C2] << " " << Team[C2] << endl; 

        for (int i = 0; i < N; i++) {
            if (!visit[i] && dis[i] > dis[pos] + map_dis[pos][i]) {
                // 更新距离
                dis[i] = dis[pos] + map_dis[pos][i]; 

                Num[i] = Num[pos]; 
                Team[i] = Team[pos] + locateTeam[i]; //Team[i]; 
            }
            else if(!visit[i] && dis[i] == dis[pos] + map_dis[pos][i]){

                Num[i] += Num[pos]; 
                
                if (Team[i] < Team[pos] + locateTeam[i]) {
                    Team[i] = Team[pos] + locateTeam[i]; 
                }

            }
        }
    }

    cout << Num[C2] << " " << Team[C2] << endl; 
}

int main() {

    while (cin >> N >> M >> C1 >> C2) {

        for (int i = 0; i < N; i++) {
            cin >> locateTeam[i]; // 本地城市 具有的 队员数量 
        }

        // 初始化地图 , 不相邻的  距离为 INF
        for (int i = 0; i < N; i++) {
            for (int j = 0; j < N; j++) {
                map_dis[i][j] = INF;
                if (i == j) {
                    map_dis[i][j] = 0; // 自己到自己的距离 为 0 
                }
            }
        }
        // 输入 M 条边 双向边
        for (int i = 0; i < M; i++) {
            cin >> Start >> End >> Weight;

            map_dis[Start][End] = Weight; 
            map_dis[End][Start] = Weight; 

        }

        Dijkstra(); 
    }

    return 0; 
}

 

 
posted @ 2018-08-09 19:44  0一叶0知秋0  阅读(529)  评论(0编辑  收藏  举报