#Keywords:Dijkstra 最短路径的条数
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, C - 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 c 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 C
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
Note
- 题意:
- 审题: 注意是输出一条最短路上能收集到的最多救援队数目,不是所有救援队数目总和,理解出现问题。
- 特殊情况: 自己到自己的dijkstra, count自己就会出现问题,因此需要从counts[start] = 1开始
Code
#include<iostream>
#include<vector>
#include<fstream>
#include<queue>
using namespace std;
const int MAX = 1000;
int collected[MAX] = {0}, dis[MAX] = {0}, rescue[MAX] = {0};// 每个节点本身有的救援队个数
int Counts[MAX] = {0}, sum[MAX] = {0};// Counts为从起点的i的救援队个数总和
int a[MAX][MAX];
int num;
void Dfs(int start, int des){
collected[start];
while(1){
int min = MAX, ptr;
for(int i = 0; i < num; i++){
if(!collected[i] && dis[i] < min){
min = dis[i];
ptr = i;
}
}
if(min == MAX){
break;
}
collected[ptr] = 1;
for(int i = 0; i < num; i++){
if(!collected[i] && dis[ptr] + a[ptr][i] < dis[i] && a[ptr][i] < MAX){
dis[i] = dis[ptr] + a[ptr][i];
Counts[i] = Counts[ptr];
sum[i] = rescue[i] + sum[ptr];
}
else if(!collected[i] && dis[ptr] + a[ptr][i] == dis[i] && a[ptr][i] < MAX){
Counts[i] += Counts[ptr];
if(sum[i] < rescue[i] + sum[ptr])
sum[i] = rescue[i] + sum[ptr];
}
}
}
}
int main(){
#ifdef _DEBUG
ifstream cin("data.txt");
#endif
int arcnum, start, dest;
cin >> num >> arcnum >> start >> dest;
for(int i = 0; i < num; i++){ //初始化
for(int j =0; j < num; j++){
a[i][j] = MAX;
}
dis[i] = MAX;
}
for(int i = 0; i < num; i++){
cin >> rescue[i];
}
for(int i = 0; i < arcnum; i++){
int tempa, tempb, length;
cin >> tempa >> tempb >> length;
a[tempa][tempb] = length;
a[tempb][tempa] = length;
}
for(int i = 0; i < num; i++){
if(i != start){
dis[i] = a[start][i];
if(a[start][i] < MAX){
// Counts[i] = 1;
}
}
}
dis[start] = 0;
Counts[start] = 1;
sum[start] = rescue[start];
Dfs(start, dest);
cout << Counts[dest] << " " << sum[dest];
#ifdef _DEBUG
cin.close();
#endif
return 0;
}