1003 Emergency (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 Specification:
Each input file contains one test case. For each test case, the first line contains 4 positive integers: () – the number of cities (and the cities are numbered from 0 to ), – the number of roads, and – the cities that you are currently in and that you must save, respectively. The next line contains integers, where the -th integer is the number of rescue teams in the -th city. Then lines follow, each describes a road with three integers , and , 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 to .
Output Specification:
For each test case, print in one line two numbers: the number of different shortest paths between and , 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
题目解读:你在A城市,B城市着火了,你要去救援,沿路的城市都有本地的消防队,输入城市个数、道路个数、A、B城市,及每个城市的消防队个数,输出最短路径的个数以及最多能聚集的消防队个数。
解题思路:Dijkstra遍历图,不断刷新dis、num、sum数组。这道题卡在了当路径相同时,如何更新通路的数量,num[i]应该等于num[i]+num[v]而不是num[v]+1,因为这里不是覆盖num[i]而是叠加。
AC代码
#include <iostream>
#include <vector>
#include <algorithm>
#include <string>
#define INF 999999
using namespace std;
int n,m,c1,c2;
int team[505];
bool visit[505];
int edge[505][505];
int num[505];
int sum[505];
int dis[505];
void Dijkstra(){
sum[c1] = team[c1];
num[c1] = 1;
dis[c1] = 0;
while( visit[c2] != true ){
int min = INF;
int v = -1;
for( int i = 0 ; i < n ; i ++ ){
if( !visit[i] && dis[i] < min ){
min = dis[i];
v = i;
}
}
if( v == -1 )return;
visit[v] = true;
for( int i = 0 ; i < n ; i ++ ){
if( !visit[i] && dis[i] > dis[v] + edge[v][i] ){
dis[i] = dis[v]+edge[v][i];
sum[i] = sum[v] + team[i];
num[i] = num[v];
}
else if( !visit[i] && dis[i] == dis[v] + edge[v][i] ){
if(sum[i] < team[i] + sum[v])
sum[i] = team[i]+ sum[v];
num[i] = num[v] + num[i];
}
}
}
}
int main(int argc, const char * argv[]) {
for( int i = 0 ; i < 505 ; i ++ ){
for( int j = 0 ; j < 505 ;j ++ ){
edge[i][j] = INF;
}
}
fill(visit,visit+505,false);
fill(dis,dis+505,INF);
fill(num,num+505,0);
fill(sum,sum+505,0);//initialize
scanf("%d %d %d %d ",&n,&m,&c1,&c2);
for( int i = 0 ; i < n ; i ++ ){
scanf("%d",&team[i]);
}
for( int i = 0 ; i < m ; i ++ ){
int temp1,temp2,temp3;
scanf("%d %d %d",&temp1,&temp2,&temp3);
edge[temp1][temp2] = temp3;
edge[temp2][temp1] = temp3;
}
Dijkstra();
printf("%d %d",num[c2],sum[c2]);
//std::cout << "Hello, World!\n";
return 0;
}
0 条评论