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
1、理解题意
给出n个城市,m条无向边。每个城市中都有一定数目的救援小组。所有边的边权已知。给出起点和终点。求起点到终点的最短路径条数和最大路径上救援小组数目之和。如果有多条最短路径,输出数目之和最大的。
2、解题思路
本题在求最短距离dis的同时,还需要求最短路径条数num和最短路径上最大救援小组数目w。由于是单元最短距离问题。可以直接使用Dijkstra算法解决问题。
#include<cstdio>
#include<algorithm>
using namespace std;
const int inf=0x3fffffff;
const int maxn=510;
int n,G[maxn][maxn];
bool vis[maxn]={false};
int dis[maxn];
int num[maxn];//路径个数
int w[maxn];//点重
int weight[maxn];
void Dijkstra(int s){
fill(dis,dis+maxn,inf);
dis[s]=0;
fill(num,num+maxn,0);
num[s]=1;
fill(w,w+maxn,0);
w[s]=weight[s];
for(int i=0;i<n;i++){
int u=-1,min=inf;
for(int j=0;j<n;j++){
if(vis[j]==false&&dis[j]<min){
u=j;
min=dis[j];
}
}
if(u==-1) return ;
vis[u]=true;
for(int v=0;v<n;v++){
if(vis[v]==false&&G[u][v]!=inf){
if(dis[u]+G[u][v]<dis[v]){
dis[v]=dis[u]+G[u][v];
w[v]=w[u]+weight[v];
num[v]=num[u];
}else if(dis[u]+G[u][v]==dis[v]){
if(w[u]+weight[v]>w[v]){
w[v]=w[u]+weight[v];
}
num[v]+=num[u];
}
}
}
}
}
int main(){
int m,s,t;
scanf("%d %d %d %d",&n,&m,&s,&t);
for(int i=0;i<n;i++){
scanf("%d",&weight[i]);
}
int u,v,d;
fill(G[0],G[0]+maxn*maxn,inf);
for(int i=0;i<m;i++){
scanf("%d %d %d",&u,&v,&d);
G[u][v]=d;
G[v][u]=d;
}
Dijkstra(s);
printf("%d ",num[t]);
printf("%d\n",w[t]);
return 0;
}
3、总结
(1)确定使用Dijkstra算法的条件:任意给出的图G,起点S,终点T,求S到T的最短路径。Dijkstra算法适合用来解决单源最短路径问题。
涉及Dijkstra算法的必须代码:
#include<cstdio>
#include<algorithm>
using namespace std;
const int inf=0x3fffffff;
const int maxn=1000;//最大顶点数
int n,G[maxn][maxn];//顶点个数在1000以下可以用邻接矩阵表示图
bool vis[maxn]={false};//用来记录已访问的顶点
int dis[maxn];//用来记录源点到各顶点的最短距离
void Dijkstra(int s,int t){
fill(dis,dis+maxn,inf);//开始时,每个顶点相互独立,之间距离很大
dis[s]=0;//源点到自身的距离为0
for(int i=0;i<n;i++){
int u=-1,min=inf;
for(int j=0;j<n;j++){
if(vis[j]==false&&dis[j]<min){
u=j;
min=dis[j];
}
}
if(u==-1) return ;
vis[u]=true;
for(int v=0;v<n;v++){
if(vis[v]==false&&G[u][v]!=inf&&dis[u]+G[u][v]<dis[v]){
dis[v]=dis[u]+G[u][v];
}
}
}
}
(2) 由于最短路径可能不止一条,题目中通常会给出第二尺标(第一尺标为距离),第二尺标常见的有:
1)给每条边再增加一个边权(比如说花费)
int cost[maxn][maxn];//边上的花费
int c[maxn];//起点到各点的最少花费
fill(c,c+maxn,inf);
c[s]=0;
for(int v=0;v<n;v++){
if(vis[v]==false&&G[u][v]!=inf){
if(dis[u]+G[u][v]<dis[v]){
dis[v]=dis[u]+G[u][v];
c[v]=c[u]+cost[u][v];
}else if(dis[u]+G[u][v]==dis[v]&&c[u]+cost[u][v]<c[v]){
c[v]=c[u]+cost[u][v];
}
}
}
2)给每个点增加一个点权(比如每个城市能收到的物资)
int weight[maxn];//每个城市的物资
int w[maxn];//从源点到各个城市共获得的物资
fill(w,w+maxn,0);
w[s]=weight[s];
for(int v=0;v<n;v++){
if(vis[v]==false&&G[u][v]!=inf){
if(dis[u]+G[u][v]<dis[v]){
dis[v]=dis[u]+G[u][v];
w[v]=w[u]+weight[v];
}else if(dis[u]+G[u][v]==dis[v]&&w[u]+weight[v]>w[v]){
w[v]=w[u]+weight[v];
}
}
}
3)直接问多少条最短路径
int num[maxn];
fill(num,num+maxn,0);
num[s]=1;
for(int v=0;v<n;v++){
if(vis[v]==false&&G[u][v]!=inf){
if(dis[u]+G[u][v]<dis[v]){
dis[v]=dis[u]+G[u][v];
num[v]=num[u];
}else if(dis[u]+G[u][v]==dis[v]){
num[v]+=num[u];//最短距离相同时累加;
}
}
}
(3) 在主函数中要对邻接矩阵进行初始化:
fill(G[0],G[0]+maxn*maxn,inf);
同时要注意题目中给出的图是无向图还是有向图。