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
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.
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 1Sample Output
2 4
分析: 这道题就是应用dijkstra求c1到c2的最短路径. 有点变化的是, 要额外创建数组记录每个点(city)的总最短路径条数, 以及在各个最短路径中能聚集最大的team数量(最大权重). 另外要注意判断c1,c2是同一个城市时候的特殊情况.
渣代码如下:
#include <iostream>
#include <cstring>
using namespace std;
#define MAX 16843009
#define max(x,y) ((x)>(y)?(x):(y))
int main(void)
{
int N,M,c1,c2,i,j,k,l;
cin>>N>>M>>c1>>c2;
int team[N];
int distance[N][N];
int dist[N];
int visited[N];
int path_number[N];
int total_team[N];
memset(distance,1,sizeof(distance)); //let each element be 16843009
for(i = 0; i < N; i++)
cin>>team[i];
for(i = 0; i < M; i++)
{
cin>>j>>k>>l;
distance[j][k]=l;
distance[k][j]=l;
}
for(i = 0; i < N; i++)
{
visited[i]=0;
dist[i]=distance[c1][i];
total_team[i]=team[i];
path_number[i] = 1;
}
visited[c1]=1;
dist[c1]=0;
if(c2==c1){
cout<<1<<" "<<team[c1];
return 0;
}
while(visited[c2]==0)
{
l = MAX;
for(j = 0; j < N; j++)
{
if(visited[j]==0 && dist[j]<l)
{
l = dist[j];
k = j;
}
}
visited[k]=1;
for(j = 0; j < N; j++)
{
if(visited[j]==0 && distance[j][k]!=MAX)
{
if(dist[j] > dist[k]+distance[j][k])
{
dist[j] = dist[k]+distance[j][k];
path_number[j] = path_number[k];
total_team[j] = total_team[k]+team[j];
}
else if(dist[j]==dist[k]+distance[j][k])
{
path_number[j]+=path_number[k];
total_team[j] = max(total_team[j],total_team[k]+team[j]);
}
}
}
}
total_team[c2]+=team[c1];
cout<<path_number[c2]<<" "<<total_team[c2];
return 0;
}