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
Code
#include <iostream>
#include <cstring>
#include <vector>
#define Max 560
#define INT_MAX 1000000000
using namespace std;
int G[Max][Max];//邻接矩阵
int d[Max];//起点到各个顶点的最短距离
bool vis[Max] = {false};
int n, m;//n : 点的个数 m : 边的个数
int p[Max];//存储到某个结点所有的最大点权和
int per[560];//存储该节点点权
int num[510]; //存储到v结点的最短路径个数
void djistra(int s)
{
/**
初始化d(最短路径)、num(最短路径的条数)、p(点权和数组)、以及源点s这些数组的初始值
然后找到从s出发的未访问结点的最短距离
更新中介到其他顶点的距离
**/
memset(num, 0, sizeof(num));//初始化从u到v 的最短路径条数
// fill(num, num + Max, 0);不要用fill初始化0,会出错
fill(d, d + Max, INT_MAX);//从u到v的最短路径
// fill(p, p + Max, 0);//不要用fill初始化0,会出错
memset(p, 0, sizeof(p));//从u到v的最大点权之和值
d[s] = 0;//到自己的距离为0
p[s] = per[s];//自己到自己的最大权值和为自己的权值大小,后面的点会用到源点的点权
num[s] = 1;
for(int i = 0; i < n; i++) {//找到d中未访问的节点中从u出发的最短路径
int u = -1;
int min = INT_MAX;
for(int j = 0; j < n; j++) {
if(vis[j] == false && d[j] < min) {
u = j;
min = d[j];//更新当前s未访问过的所能到的最近的节点作为中介
}
}
if(u == -1)
return;//没有结点与其连通且是未访问的
vis[u] = true;//标记中介已访问
//遍历此中介结点所能到达的其他结点,选择未访问的节点,更新最小路劲值
for(int v = 0; v < n; v++) {
if(vis[v] == false && G[u][v] != INT_MAX){
if(d[u] + G[u][v] < d[v]){
d[v] = d[u] + G[u][v];
p[v] = p[u] + per[v];
num[v] = num[u];//到v的最短路径等于到u的最短路径,因外u是v的前驱节点
}
else if(d[u] + G[u][v] == d[v]){
if(p[u] + per[v] > p[v])
p[v] = p[u] + per[v];
num[v] += num[u];//到v的最短路径多了以u为前驱的所有情况。
}
}
}
}
}
int main()
{
int s1, s2;
cin>>n>>m>>s1>>s2;
for(int i = 0; i < n; i++)
cin>>per[i];//输入点权
fill(G[0], G[0] + Max * Max, INT_MAX);//初始化每个点之间的距离为最大
for(int i = 0; i < m; i++) {
int c1, c2, l;
cin>>c1>>c2;
cin>>l;
G[c1][c2] = G[c2][c1] = l;
} //领接矩阵初始化
djistra(s1);
cout<<num[s2]<<" "<<p[s2];
}
Summary
这道题是经典的Djistra算法,针对这种题本质是一样的,只不过多了其他优化条件时要在更新u到v的最短路径时要同时更新优化条件
步骤如下:
- 录入点权
- 初始化领接矩阵为最大值(领接表不需要,因为它可以直接取出对应的边,但是要用结构体存储边的值和顶点编号)
- 录入领接矩阵
- djistra(源点):
- 初始化d数组、w数组(点权和最大)、num(最短路径条数)
- 初始化源点的d、w、num中的值
- 进入n次循环
- 找到从u出发的最小的未访问的d[v]节点,使其为中介
- 更新从此中介出发的所有权值数组合
- 如果以u为前驱的路径长度等于v目前的最短路径长度: 增加最短路径条数,如果在此基础上它,到u为止的点权和加上v的点权比目前到v为止的点权和要大,更新w[v] = w[u] + weight[v]