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.
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 1Sample Output
2 4
题意:给定n为城市的个数,m为路的数目,c1为起点,c2为终点,然后给定n个城市的救援队伍的个数,再给出m条路的起点和终点和两点之间的长度,让我们求起点到终点的最短路径和救援队伍的总数 ,可以把它理解为无向图,那么我们可以用dijkstar算法求出最小路径,再进行些许修改
#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <algorithm>
#include <cstdlib>
#include <stack>
#include <iomanip>
using namespace std;
#define maxx 999999
#define maxn 505
int n,m,dist[maxn],num[maxn],c[maxn][maxn],star,endd,path[maxn],wage[maxn],visit[maxn];
void dijkstra()
{
visit[star]=1;//先把起点标记
path[star]=1;//path记录路径数目
num[star]=wage[star];//起点的救援队伍
dist[star]=0;
int i,j,tmp,k;
for(i=0;i<n;i++)
{
if(c[star][i]<dist[i])//如果从起点到i的路径长度要小于i的最小路径
{
dist[i]=c[star][i];
path[i]=path[star];
num[i]=num[star]+wage[i];//到i的路径救援队伍等于star的救援队伍加上这个城市本身的救援队伍
}
}
for(j=1;j<n;j++)
{
tmp=maxx;
k=star;
for(i=0;i<n;i++)
{
if(!visit[i]&&dist[i]<tmp)
{
tmp=dist[i];
k=i;
}
}
visit[k]=1;//求没有使用的且最小的长度,那么就归入到visit数组中
for(i=0;i<n;i++)
{
if(!visit[i])
{
if(dist[k]+c[k][i]<dist[i])
{
dist[i]=dist[k]+c[k][i];
path[i]=path[k];
num[i]=num[k]+wage[i];
}else if(dist[k]+c[k][i]==dist[i])//如果最小路径相等
{
if(num[i]<num[k]+wage[i])
{
num[i]=num[k]+wage[i];//如果救援队伍不是最大,那么我们就求出最大
}
path[i]=path[i]+path[k];//多了一个最短的路径,那么路径数目肯定要再增加
}
}
}
}
}
int main()
{
memset(c,maxx,sizeof(c));//初始化任意两点的距离为无穷大
memset(dist,maxx,sizeof(dist));//初始化任意一点的最小距离为无穷大
memset(visit,0,sizeof(visit));
int i,p,q,len;
cin>>n>>m>>star>>endd;
for(i=0;i<n;i++)
{
cin>>wage[i];
}
for(i=0;i<m;i++)
{
cin>>p>>q>>len;
c[p][q]=len;//因为两点间是没有方向的
c[q][p]=len;
}
if(star==endd)
{
cout<<"1 "<<wage[star]<<endl;//如果起点和终点是同一个地方,那么路径只有一条,救援队伍肯定只有本身这个城市的救援队伍数
}else{
dijkstra();
cout<<path[endd]<<" "<<num[endd]<<endl;
}
return 0;
}