1003. Emergency (25)

来源:互联网 发布:苏州买茶叶知乎 编辑:程序博客网 时间:2024/06/01 07: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 21 2 1 5 30 1 10 2 20 3 11 2 12 4 13 4 1
Sample Output
2 4
城市之间的最短距离,用dijkstra算法
dijkstra算法核心就是:
1、把起始点加入集合s
2、不断将距离集合s最短的点加入集合s
3、根据新加入的点,更新从起始点出发的 离起始点最短距离矩阵dist 和 记录某点之前的节点矩阵prev
然后这一题还要看能聚集到的人数最多的救援队数量,在更新最短距离矩阵的时候,如果距离相等,就要考虑救援队的数量
同时,还要输出c1与c2之间的最短路径的数量,采用path矩阵来记录每个节点到c1的最短路径数量
如果更新最短距离的时候,距离相等,那么最短路径的个数就会增加
#include<iostream>#include<string.h>#include<vector>using namespace std;#define INF 0x7fffffffint main(){int n, m, c1, c2;cin >> n >> m >> c1 >> c2;int *teamn = new int[n];     //当前城市救援队数目for (int i = 0; i < n; i++)cin >> teamn[i];int **dis = new int*[n];   //距离矩阵int *dist = new int[n];   //从c1出发的最短距离int *prev = new int[n];   //之前的节点bool *used = new bool[n];  //是否加入集合int *teama = new int[n];   //在最短距离下,从起点到当前点的救援队数量int *path = new int[n];    //最短路径的数量for (int i = 0; i < n; i++)   //初始化{dis[i] = new int[n];for (int j = 0; j < n; j++)dis[i][j] = INF;dist[i] = INF;prev[i] = -1;used[i] = 0;teama[i] = 0;path[i] = 0;}used[c1] = 1;path[c1] = 1;teama[c1] = teamn[c1];int node1, node2, ndis;for (int i = 0; i < m; i++){cin >> node1 >> node2 >> ndis;dis[node1][node2] = ndis;dis[node2][node1] = ndis;if (node1 == c1){dist[node2] = ndis;prev[node2] = c1;path[node2] = 1;}}for (int i = 0; i < n; i++){int tmp = INF;int pnt = c1;for (int j = 0; j < n; j++){if ((!used[j]) && dist[j] < tmp)   //find shortest dist and add it{pnt = j;tmp = dist[j];teama[pnt] = teama[prev[pnt]] + teamn[pnt];}}used[pnt] = 1;for (int j = 0; j < n; j++)         //update dist{if ((!used[j]) && dis[pnt][j] < INF){if (dist[pnt] + dis[pnt][j] < dist[j]){dist[j] = dist[pnt] + dis[pnt][j];prev[j] = pnt;path[j] = path[pnt];teama[j] = teama[pnt] + teamn[j];}else if (dist[pnt] + dis[pnt][j] == dist[j]){path[j] += path[pnt];if (teama[pnt] + teamn[j] > teama[j]){dist[j] = dist[pnt] + dis[pnt][j];prev[j] = pnt;teama[j] = teama[pnt] + teamn[j];}}}}}cout << path[c2] << " " << teama[c2];return 0;}



原创粉丝点击