1003. Emergency (25)

来源:互联网 发布:淘宝卖家怎么弄花呗 编辑:程序博客网 时间:2024/05/16 17:26

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
#include <iostream>#include <cstdio>#include <vector>#include <limits.h>#include <cstring>using namespace std;/* 图中两点最短路径,简单的题目可以只要求求出最短路径的长度,对于难的题目可能需要求最短路径的条数,存储所有的最短路径(如果题意明确规定只有一条路径满足最后要求,能此最短路径的球阀将变得相对简单) * */const int N = 500; //图中顶点数量struct Edge //边{int cost;int next;};struct Node //顶点{int value;vector<Edge> edge;};Node graph[N]; //图信息int Cost[N]; //路径int Rescue[N]; //int num[N];bool visit[N];int main(void){int n, m, start, end;  //n 为顶点,0 - n-1, m 为边, start起点,end终点scanf("%d%d%d%d", &n, &m, &start, &end);for (int i = 0; i <  n; ++i)scanf("%d", &graph[i].value);int s, e;Edge edge;for (int i = 0; i < m; ++i){scanf("%d%d%d", &s, &e, &edge.cost);edge.next = e;graph[s].edge.push_back(edge);edge.next = s;graph[e].edge.push_back(edge);}for (int i = 0; i < n; ++i)Cost[i] = INT_MAX;memset(Rescue, 0, n);memset(visit, false, n);memset(num, 0, n);Cost[start] = 0;Rescue[start] = graph[start].value;num[start] = 1;int minV, minI;while (true) {minV = INT_MAX;minI = -1;for (int i = 0; i < n; ++i){if (!visit[i]){if (Cost[i] < minV){minI = i;minV = Cost[i];}}}visit[minI] = true;if (minI == end)  //如果不确定start与end之间是否有有路径,可以判断是否minI为有效值break;for (vector<Edge>::iterator iter = graph[minI].edge.begin(); iter != graph[minI].edge.end(); ++iter){if (!visit[iter -> next]){if (Cost[minI] + iter->cost < Cost[iter->next]){Cost[iter->next] = Cost[minI] + iter->cost;Rescue[iter->next] = Rescue[minI] + graph[iter->next].value;num[iter->next] = num[minI];}else if (Cost[minI] + iter->cost == Cost[iter->next]){num[iter->next] += num[minI];if (Rescue[minI] + graph[iter->next].value > Rescue[iter->next])Rescue[iter->next] = Rescue[minI] + graph[iter->next].value;}}}}printf("%d %d\n", num[end], Rescue[end]);return 0;}


0 0