1003. Emergency (25)

来源:互联网 发布:nginx教程 编辑:程序博客网 时间:2024/04/29 21:03

1003. Emergency (25)

Question
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


把之前写的在修改一下,这样子更加直观一点儿。
思路很简单的“深度优先搜索”。

我的解法

#include <iostream>#include <climits> //不能是<limits>using namespace std;const int MAX = 501;int C_N, R_N, FROM, TO;int teamsInCity[MAX];int map[MAX][MAX];bool visited[MAX];int shortestLen = INT_MAX;int mostTeams = 0;int shortedNo = 0;int currentLen = 0;int currentTeams = 0;void depthSearch(int city_in) {    //搜索结束1:到达目的城市    if (city_in == TO) {        if (currentLen < shortestLen) {            shortedNo = 1;            mostTeams = currentTeams;            shortestLen = currentLen;        }else if (currentLen == shortestLen) {            shortedNo++;            if (currentTeams > mostTeams) {                mostTeams = currentTeams;            }        }        return;    }    for (int i = 0; i < C_N; ++i) {        if (map[city_in][i] != INT_MAX && !visited[i]) {            currentLen += map[city_in][i];            currentTeams += teamsInCity[i];            visited[i] = true;            depthSearch(i);            currentLen -= map[city_in][i];            currentTeams -= teamsInCity[i];            visited[i] = false;        }    }}int main(int argc, const char * argv[]) {    // 5 6 0 2    cin >> C_N >> R_N >> FROM >> TO;    //每一个城市中的team的数量:1 2 1 5 3    for (int i = 0; i < C_N; ++i) {        cin >> teamsInCity[i];    }    //初始化每个城市间的地图    for (int i = 0; i < C_N; ++i) {        for (int j = 0; j < C_N; ++j) {            map[i][j] = INT_MAX;        }    }    //绘制地图    int from, to, len;    for (int i = 0; i < R_N; ++i) {        cin >> from >> to >> len;        map[from][to] = map[to][from] = len;    }    //已经去过的城市    for (int i = 0; i < C_N; ++i) {        visited[i] = false;    }    visited[FROM] = true;    //搜索开始    currentTeams = teamsInCity[FROM];    depthSearch(FROM);    cout << shortedNo << " " << mostTeams;    return 0;}
0 0
原创粉丝点击