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 <algorithm>
#include <climits>
using namespace std;
const int maxn = 505;
int g[maxn][maxn] = {0};
int teamInCity[maxn]; // 每个城市里的救援队的数量
bool vis[maxn] = {false}; // dijkstra算法中标记结点是否已访问
int dis[maxn]; // dis[i]从源点到i的最小距离
int pathNum[maxn] = {0}; // pathNum[i]表示从源点到i的路径条数
int teamNum[maxn] = {0}; // pathNum[i]表示从源源点到i在走最短路径时可以带的最大救援队数量
int n, m, c1, c2; // n:城市数量 m:边的数量 c1:起始结点 c2:目标结点
void Dijkstra(int s, int t)
{
fill(dis, dis + maxn, INT_MAX);
dis[s] = 0; // 源点距离置为0
teamNum[s] = teamInCity[s]; // 源点到源点可以携带的救援队的数量
pathNum[s] = 1; // 源点到源点的路径数量置1
for(int i = 0; i < n; i++)
{
int u = -1, minL = INT_MAX;
for(int j = 0; j < n; j++)
{
if(!vis[j] && dis[j] < minL)
{
u = j;
minL = dis[j];
}
}
if(u == -1) return; // 说明没有和源点连通的结点了
if(u == t) return; // 说明目标城市已经完成了计算,可以退出
vis[u] = true; // 找到了当前最小距离的城市,标记为已访问
for(int j = 0; j < n; j++)
{
if(!vis[j] && g[u][j] != 0)
{
if(dis[u] + g[u][j] < dis[j])
{
dis[j] = dis[u] + g[u][j];
pathNum[j] = pathNum[u];
teamNum[j] = teamNum[u] + teamInCity[j];
}
else if(dis[u] + g[u][j] == dis[j])
{
pathNum[j] += pathNum[u];
if(teamNum[j] < teamNum[u] + teamInCity[j])
teamNum[j] = teamNum[u] + teamInCity[j];
}
}
}
}
}
int main()
{
scanf("%d%d%d%d", &n, &m, &c1, &c2);
for(int i = 0; i < n; i++)
scanf("%d", teamInCity + i);
for(int i = 0; i < m; i++)
{
int t1, t2, l;
scanf("%d%d%d", &t1, &t2, &l);
g[t1][t2] = l;
g[t2][t1] = l;
}
Dijkstra(c1, c2);
printf("%d %d\n", pathNum[c2], teamNum[c2]);
return 0;
}