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算法求解即可。注意第一个输出的是最短路径条数,第二个是最大点权
参考代码:
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
const int maxv=520;
const int INF=1000000000;
int n,m,Start,End,G[maxv][maxv],weight[maxv];
int d[maxv],w[maxv],num[maxv];
//n为顶点数,m为边数,Start为起点,End为终点
//G为邻接矩阵,weight为点权
//d为最短距离,w记录最大点权之和,num记录最短路径条数
bool vis[maxv]={false};
void Dijkstra(int s) //s为起点
{
fill(d,d+maxv,INF); //初始化
memset(num,0,sizeof(num));
memset(w,0,sizeof(w));
d[s]=0;
w[s]=weight[s];
num[s]=1;
for(int i=0;i<n;++i)
{
int u=-1,min=INF; //u记录使d[u]最小的顶点编号,min记录最小d[u]
for(int j=0;j<n;++j)
{
if(vis[j]==false&&d[j]<min) //找到未访问的顶点中d[]最小的
{
u=j;
min=d[j];
}
}
//找不到最小的,说明剩下的顶点和起点不相通
if(u==-1)
return;
vis[u]=true;
for(int v=0;v<n;++v)
{
//如果v未访问且u可以到达v并且以u为中介点可以使d[v]更优
if(vis[v]==false&&G[u][v]!=INF)
{
if(d[u]+G[u][v]<d[v])
{
d[v]=d[u]+G[u][v]; //更新d[v]
w[v]=w[u]+weight[v]; //更新w[v]
num[v]=num[u]; //更新num
}
else if(d[u]+G[u][v]==d[v]) //找到一条长度相同的路径
{
if(w[u]+weight[v]>w[v])//以u为中介点时点权更大
w[v]=w[u]+weight[v]; //更新w[v],w[v]继承自w[u]
num[v]+=num[u];//增加最短路径条数
}
}
}
}
}
int main()
{
scanf("%d%d%d%d",&n,&m,&Start,&End);
for(int i=0;i<n;++i)
scanf("%d",&weight[i]);
int u,v;
fill(G[0],G[0]+maxv*maxv,INF); //初始化图G
for(int i=0;i<m;++i)
{
scanf("%d%d",&u,&v);
scanf("%d",&G[u][v]);//读入边权
G[v][u]=G[u][v];
}
Dijkstra(Start);
printf("%d %d\n",num[End],w[End]);
return 0;
}