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, C1and 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
Code
套用Dijkstra模板:
int dijkstra() {
//距离初始化为无穷大
memset(dist, 0x3f, sizeof dist);
//起点距离自己的距离为零
dist[1] = 0;
//迭代n次,每次可以确定一个点到起点的最短路
for (int i = 0; i < n; ++i)
{
int t = -1;
for (int j = 1; j <= n; ++j)
if (!st[j] && (t == -1 || dist[j] < dist[t]))
t = j;
//加入到s集合中
st[t] = true;
//! 基本每次都有变化:
//找到了距离最小的点t,并用最小的点t去更新其他的点到起点的距离
for (int j = 1; j <= n; ++j)
dist[j] = min(dist[j], dist[t] + g[t][j]);
}
// 如果起点到达不了n号节点,则返回-1
if (dist[n] == 0x3f3f3f3f) return -1;
// 返回起点距离n号节点的最短距离
return dist[n];
}
#include<bits/stdc++.h>
using namespace std;
const int N = 610;
int n,m,c1,c2;
int g[N][N] , dist[N] , num[N] , cnt[N] , maxn[N];
bool st[N];
int dijkstra()
{
memset(dist , 0x3f , sizeof dist);
dist[c1] = 0;
cnt[c1] = 1;
maxn[c1] = num[c1];
for(int i=0 ; i<n ; i++)
{
int t = -1;
for(int j=0 ; j<n ; j++)
if(!st[j] && ( (t == -1) || (dist[t] > dist[j]) ))
t = j;
st[t] = true;
for(int j=0 ; j<n ; j++)
{
if(dist[j] > (dist[t]+g[t][j]))
{
dist[j] = dist[t]+g[t][j];
cnt[j] = cnt[t];
maxn[j] = maxn[t] + num[j];
}
else if(dist[j] == dist[t] + g[t][j])
{
cnt[j] += cnt[t];
if(maxn[j] < maxn[t] + num[j])
maxn[j]= maxn[t] + num[j];
}
}
}
}
int main()
{
cin>>n>>m>>c1>>c2;
memset(g , 0x3f , sizeof g);
for(int i=0;i<n;i++)
cin>>num[i]; //number of rescue team.
for(int i=0;i<m;i++)
{
int a,b,c;
cin>>a>>b>>c;
g[a][b] = g[b][a] = c; //road is two-way.
}
dijkstra();
cout<<cnt[c2]<<" "<<maxn[c2];
}