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: 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 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
感想:Dijkstra求的不是起点与终点的最短距离,而是起点到每个点的最短距离。
为什么整理这个题:因为第一次得了23分,第二个点没过,原因很简单,没有注意到起点和终点可能是同一个点,在这里整理一下,以后做图的问题记住。
#include<cstdio>
#include<iostream>
#include<cstring>
#include<algorithm>
using namespace std;
#define MAXN 515
#define INF 0x3f3f3f
int n,m,st,ed;
int person[MAXN],dist[MAXN],group[MAXN][MAXN],num[MAXN],w[MAXN];
bool vis[MAXN];
void DJ()
{
//初始化
for(int i = 0; i < n; i++)
{
dist[i] = group[st][i];
if(dist[i] < INF)
{
num[i] = 1;
person[i] = w[st] + w[i];
}
}
vis[st] = true;
//Dijkstra算法不是在找st到ed的最短路径,而是再找每一条路到起点的距离
for(int i = 1; i < n; i++)
{
int MIN = INF,MINID;
for(int i = 0; i < n; i++)
{
if(dist[i] < MIN && !vis[i])
{
MIN = dist[i];
MINID = i;
}
}
vis[MINID] = true;
for(int i = 0; i < n; i++)
{
if(!vis[i] && dist[i] > dist[MINID] + group[MINID][i])
{
//printf("dist[%d] = %d dist[%d] = %d group[%d][%d] = %d\n",i,dist[i],MINID,dist[MINID],MINID,i,group[MINID][i]);
dist[i] = dist[MINID] + group[MINID][i];
num[i] = num[MINID];
person[i] = person[MINID] + w[i];
//printf("起始点:%d 中间点:%d 目的地: %d \n dist[%d] = %d num[%d] = %d person[%d] = %d\n",st,MINID,i,i,dist[i],i,num[i],i,person[i]);
}
else if(!vis[i] && dist[i] == dist[MINID] + group[MINID][i])
{
if(person[i] < person[MINID] + w[i]) person[i] = person[MINID] + w[i];
num[i] += num[MINID];
}
}
}
}
int main()
{
int l,r,d;
scanf("%d %d %d %d",&n,&m,&st,&ed);
for(int i = 0; i < n; i++) scanf("%d",&w[i]);//每个城市救援队的数量
for(int i = 0; i < n; i++)
{
for(int j = 0; j < n; j++)
{
group[i][j] = INF;//全部设置为不可到达
}
}
for(int i = 0; i < m; i++)
{
scanf("%d %d %d",&l,&r,&d);
group[l][r] = d;//无向图
group[r][l] = d;
}
if(st == ed)
{
cout<<1<<" "<<w[ed]<<endl;
return 0;
}
else DJ();
cout<<num[ed]<<" "<<person[ed]<<endl;
return 0;
}