1003. Emergency (25)
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
import java.util.Scanner;
public class Main {
static int n, m, s, e;
static int[] dis = new int[501];
static int[] path_max=new int[501];
static int[] teams_max=new int[501];
static int[] teams = new int[501];
static int[][] map1 = new int[501][501];
static boolean[] vis=new boolean[501];
public static void Djst(int start) {
for(int i=0;i<n;i++) {
dis[i]=map1[start][i];
vis[i]=false;
teams_max[i]=0;
path_max[i]=0;
}
//System.out.println(dis[2]);
dis[start]=0;
//vis[start]=true;//这里不可以标记,一旦标记最后道路的条数就是0条
teams_max[start]=teams[start];
path_max[start]=1;
for(int i=0;i<n-1;i++) {
int min1=0x3f3f3f3f,index =-1;
for(int j=0;j<n;j++) {
if(!vis[j]&&dis[j]<min1) {
min1=dis[j];
index=j;
}
}
vis[index]=true;
for(int j=0;j<n;j++) {
if(!vis[j]&&dis[j]>dis[index]+map1[index][j]) {
dis[j]=dis[index]+map1[index][j];
teams_max[j]=teams_max[index]+teams[j];
path_max[j]=path_max[index];
}
else if(!vis[j]&&dis[j]==dis[index]+map1[index][j]) {
path_max[j]+=path_max[index];
//System.out.println(j+":"+path_max[j]);
if(teams_max[j]<teams_max[index]+teams[j]) {
teams_max[j]=teams_max[index]+teams[j];
}
}
}
}
System.out.println(path_max[e]+" "+teams_max[e]);
}
public static void main(String[] args) {
// TODO Auto-generated method stub
int from, to, v;
Scanner sc = new Scanner(System.in);
n = sc.nextInt();
m = sc.nextInt();
s = sc.nextInt();
e = sc.nextInt();
for(int i=0;i<n;i++) {
//dis[i]=0x3f3f3f3f;
for(int j=0;j<n;j++) {
map1[i][j]=0x3f3f3f3f;//最短路径首先必须要将城市之间的初始距离设置为无穷大
}
}
for (int i = 0; i < n; i++) {
teams[i] = sc.nextInt();
}
for (int i = 0; i < m; i++) {
from = sc.nextInt();
to = sc.nextInt();
v = sc.nextInt();
map1[from][to] = map1[to][from] = v;
}
Djst(s);//千万要小心别写成0了,有时不一定是从0开始,
}
}