Dijkstra算法应用
PTA Advanced level
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, C
1
and C
2
- 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 c
1
, c
2
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 C
1
to C
2
.
Output Specification:
For each test case, print in one line two numbers: the number of different shortest paths between C
1
and C
2
, 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模板,该题需要特殊处理每个城市自带的救援队(注意,该属性与边无关,为结点自带),同时要处理最短路径的条数。
特殊处理:
Val为单独每个城市的救援队数量
int Val[505]; //每个城市的救援队数量;
int val[505]; //救援队数量数组;
int t[505]; //最短路径条数;
初始化:
for(int i=0;i<n;i++) //初始化距离数组;
{
dis[i]=G[s][i].s;
val[i]=Val[s]+Val[i];
}
for(int i=0;i<n;i++) //最短路径条数;
t[i]=1;
更新条件:
若最短路更新,则一定更新:
更新方式,通过u到v,能得到s到v的最短路,则更新为val[u]+Val[v]
同时t[v]=t[u]
if(!vis[v]&&dis[u]+G[u][v].s<dis[v]){ //更新最短路一定同时更新花费;
dis[v]=dis[u]+G[u][v].s;
val[v]=val[u]+Val[v];
t[v]=t[u]; //特别注意,若通过u更新v,即通过u到v为s到v的最短路,则到v的最短路径个数为到u的最短路径个数。
}
若最短路长度相同,则更新如下:
else if(!vis[v]&&dis[u]+G[u][v].s==dis[v]) //若都为最短路,则再比较花费。
{
t[v]+=t[u]; //注意,若通过u更新v,则最短路径个数为v原本个数加上到u的最短路径个数.
val[v]=max(val[u]+Val[v],val[v]);
}
完整代码
#include<cstdio>
#include<iostream>
#include<cstring>
#include<algorithm>
using namespace std;
struct Graph{
int s;//距离;
//int v;//收费;
}G[505][505];
int n,m;//城市个数,高速公路条数;
int s,d;//出发城市编号,目的地城市编号;
int Val[505]; //每个城市的救援队数量;
void init()
{
cin>>n>>m;
cin>>s>>d;
for(int i=0;i<n;i++) //初始化权值;
{
for(int j=0;j<n;j++)
G[i][j].s=G[j][i].s=99999;
}
for(int i=0;i<n;i++)
cin>>Val[i];
int a,b;//坐标;
while(m--) //无向图;
{
cin>>a>>b;
cin>>G[a][b].s;
G[b][a].s=G[a][b].s;
}
}
bool vis[505]; //访问数组;
int dis[505]; //距离数组;
int val[505]; //费用数组;
int t[505]; //最短路径条数;
void dijkstra()
{
memset(vis,false,sizeof(vis)); //初始都未访问;
for(int i=0;i<n;i++) //初始化距离数组;
{
dis[i]=G[s][i].s;
val[i]=Val[s]+Val[i];
}
dis[s]=0,val[s]=Val[s],vis[s]=true; //自己到自己;
for(int i=0;i<n;i++) //最短路径条数;
t[i]=1;
for(int i=0;i<n-1;i++) //除出发点外另找n-1个点;
{
int u,tmin=99999;
for(int j=1;j<n;j++) //寻找当前距离数组中最小值;
{
if(!vis[j]&&dis[j]<tmin){
tmin=dis[j];
u=j;
}
}
vis[u]=true; //利用u结点进行距离更新;
for(int v=1;v<n;v++)
{
if(!vis[v]&&dis[u]+G[u][v].s<dis[v]){ //更新最短路一定同时更新花费;
dis[v]=dis[u]+G[u][v].s;
val[v]=val[u]+Val[v];
t[v]=t[u]; //特别注意,若通过u更新v,即通过u到v为s到v的最短路,则到v的最短路径个数为到u的最短路径个数。
}
else if(!vis[v]&&dis[u]+G[u][v].s==dis[v]) //若都为最短路,则再比较花费。
{
t[v]+=t[u]; //注意,若通过u更新v,则最短路径个数为v原本个数加上到u的最短路径个数.
val[v]=max(val[u]+Val[v],val[v]);
}
}
}
}
int main()
{
init();
dijkstra();
cout<<t[d]<<' '<<val[d]<<endl;
return 0;
}