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
题意:给定一张图,包含点权值和边权值,求某两点间最短路径数和点权值和的最大值
思路:利用dijkstra算法,最好设数组:dis[ ],num[ ],weight[ ]。下面斜线粗体重点
来自柳婼:当判定dis[u] + e[u][v] < dis[v]的时候,不仅仅要更新dis[v],还要更新num[v] = num[u], w[v] = weight[v] + w[u]; 如果dis[u] + e[u][v] == dis[v],还要更新num[v] += num[u],而且判断一下是否权重w[v]更小,如果更小了就更新w[v] = weight[v] + w[u]
#include <iostream>
#include <climits>
using namespace std;
int n,m,src,des;
int e[500][500];
int team[500];
int dis[500];
int maxt[500];
int num[500];
bool visit[500];
int main()
{
int c1,c2,l;
fill(e[0],e[0]+500*500,INT_MAX);
fill(dis,dis+500,INT_MAX);
fill(visit,visit+500,false);
cin >> n >> m >> src >> des;
for(int i=0;i<n;i++)
{
cin >> team[i];
}
for(int i=0;i<m;i++)
{
cin >> c1 >> c2 >> l;
e[c1][c2] = e[c2][c1] = l;
}
dis[src] = 0;
num[src] = 1;
maxt[src] = team[src];
while(true)
{
int mindis=INT_MAX;
int v=-1;
for(int i=0;i<n;i++)
{
if(visit[i] == false && mindis > dis[i])
{
v = i;
mindis = dis[i];
}
}
if(v==-1)
break;
visit[v] = true;
for(int i=0;i<n;i++)
{
if(visit[i] == false && e[v][i] != INT_MAX)
{
if(dis[i] > dis[v] + e[v][i])
{
dis[i] = dis[v] + e[v][i];
num[i] = num[v];
maxt[i] = maxt[v] + team[i];
}
else if(dis[i] == dis[v] + e[v][i])
{
num[i] += num[v];
if(maxt[i] < maxt[v] + team[i])
maxt[i] = maxt[v] + team[i];
}
}
}
}
cout << num[des] << " " << maxt[des];
return 0;
}