Emergency
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
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
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
/*题目大意:n个城市m条路,每个城市有救援小组,
所有的边的边权已知。给定起点和终点,
求从起点到终点的最短路径条数
以及最短路径上的救援小组数目之和。
如果有多条就输出点权(城市救援小组数目)最大的那个*/
/*大红色标记的两条语句(Dijkstra模板),在此题中不能加上,因为还要更新路径数和人数,若加上,c1将不会执行松弛操作,结果将会少了c1的人数。*/
#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
int n,m,inf=0x3f3f3f3f,c1,c2;
int e[510][510],w[510],weight[510],num[510],book[510],dis[510];
//weight是每个城市救援人员数目,e是边,dis是下标城市距离c1的最近距离
//w是到达某城市及之前的救援人员总数 ,num路径总数
int main()
{
int i,j,t1,t2,t3,u,v,min;
scanf("%d%d%d%d",&n,&m,&c1,&c2);
for(i=0;i<n;i++)
scanf("%d",&weight[i]);
fill(dis,dis+510,inf);
fill(e[0],e[0]+510*510,inf);
//注意写法,fill函数是在 algorithm中的,类比memset,但也有区别
// memset(book,0,sizeof(book));
for(i=0;i<m;i++)
{
scanf("%d%d%d",&t1,&t2,&t3);
e[t1][t2]=t3;
e[t2][t1]=t3;
}
for(i=0;i<n;i++)
dis[i]=e[c1][i];
dis[c1]=0;
num[c1]=1;
w[c1]=weight[c1];//救援人员总数是在c1基础上的
// book[c1]=1;//c1已走过,标记
for(i=0;i<n;i++)
{
u=-1;
min=inf;
for(j=0;j<n;j++)//找到离c1最近的点
{
if(book[j]==0&&dis[j]<min)
{
min=dis[j];
u=j;
}
}
if(u==-1)
break;
book[u]=1;
for(v=0;v<n;v++)//松弛
{
if(book[v]==0&&e[u][v]<inf)//v没走过,并且uv之间连通,
{
if(dis[v]>dis[u]+e[u][v])//如果路径短就松弛
{
dis[v]=dis[u]+e[u][v];
num[v]=num[u];//路径条数没变
w[v]=weight[v]+w[u];//人数累加
}
else if(dis[v]==dis[u]+e[u][v])
//如果路径一样长,就存在多条最短路径
{
num[v]+=num[u];//路径增加
if(weight[v]+w[u]>w[v])//并且要选取人数最多的情况
w[v]=weight[v]+w[u];
}
}
}
}
printf("%d %d",num[c2],w[c2]);
return 0;
}
或者按照以下做法
#include<stdio.h>
#include<string.h>
#include<iostream>
#include<algorithm>
using namespace std;
int n,m,inf=0x3f3f3f3f,c1,c2;
int e[510][510],w[510],weight[510],num[510],book[510],dis[510];
int main()
{
int i,j,t1,t2,t3,u,v,min;
scanf("%d%d%d%d",&n,&m,&c1,&c2);
for(i=0;i<n;i++)
scanf("%d",&weight[i]);
fill(dis,dis+510,inf);
fill(e[0],e[0]+510*510,inf);
for(i=0;i<m;i++)
{
scanf("%d%d%d",&t1,&t2,&t3);
e[t1][t2]=t3;
e[t2][t1]=t3;
}
w[c1]=weight[c1];
num[c1]=1;
for(i=0;i<n;i++)
{
dis[i]=e[c1][i];
if(dis[i]!=inf)
{
w[i]=weight[i]+weight[c1];
num[i]=num[c1];
}
}
dis[c1]=0;
memset(book,0,sizeof(book));
book[c1]=1;
//救援人员总数是在c1基础上的
for(i=0;i<n;i++)
{
u=-1;
min=inf;
for(j=0;j<n;j++)//找到离c1最近的点
{
if(book[j]==0&&dis[j]<min)
{
min=dis[j];
u=j;
}
}
if(u==-1)
break;
book[u]=1;
for(v=0;v<n;v++)//松弛
{
if(e[u][v]<inf)//v没走过,并且uv之间连通,
{
if(dis[v]>dis[u]+e[u][v])//如果路径短就松弛
{
dis[v]=dis[u]+e[u][v];
num[v]=num[u];//路径条数没变
w[v]=weight[v]+w[u];//人数累加
}
else if(dis[v]==dis[u]+e[u][v])
//如果路径一样长,就存在多条最短路径
{
num[v]+=num[u];//路径增加
if(weight[v]+w[u]>w[v])//并且要选取人数最多的情况
w[v]=weight[v]+w[u];
}
}
}
}
printf("%d %d",num[c2],w[c2]);
return 0;
}