PAT 甲级1003. Emergency (25) DIJKSTRA

来源:互联网 发布:淘宝网购物女装冬装 编辑:程序博客网 时间:2024/05/17 22:21

1003. Emergency (25)

时间限制
400 ms
内存限制
65536 kB
代码长度限制
16000 B
判题程序
Standard
作者
CHEN, Yue

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 21 2 1 5 30 1 10 2 20 3 11 2 12 4 13 4 1
Sample Output
2 4
DIJKSTRA模板,只是在其中加上记录到改点最短路的个数以及到改点队伍最多数。
#include<cstdio>#include<cstring>#include<algorithm>#include<queue>using namespace std;const int size=500;const int maxint=99999999;int dis[size]; //最短路 int cnt[size];//最短路个数int sum[size];//队伍最多数 int num[size];//每个城市人数 int map[size][size];//记录图 int n,m,c1,c2;void init(){for (int i=0;i<n;i++){dis[i]=maxint;cnt[i]=1;sum[i]=0;for (int j=0;j<n;j++)map[i][j]=maxint;}}void dijkstra(){int i,j,themin,pos;dis[c1]=0;int vis[size]={0};sum[c1]=num[c1];for (i=0;i<n;i++){int themin=maxint;for (j=0;j<n;j++)if (!vis[j]&&dis[j]<themin){pos=j;themin=dis[j];}vis[pos]=1;for (j=0;j<n;j++)if (!vis[j]&&map[pos][j]!=maxint){if (dis[pos]+map[pos][j]<dis[j])    {dis[j]=dis[pos]+map[pos][j];cnt[j]=cnt[pos];//最短路数等于上一节点最短路数 sum[j]=sum[pos]+num[j];}else if (dis[pos]+map[pos][j]==dis[j]){cnt[j]=cnt[j]+cnt[pos];//当前最短数加上新的路带来的路数 sum[j]=max(sum[j],sum[pos]+num[j]);}}}}int main(){int i,j,k,t,x,y,v;scanf("%d%d%d%d",&n,&m,&c1,&c2);init();for (i=0;i<n;i++)scanf("%d",&num[i]);for (i=0;i<m;i++){scanf("%d%d%d",&x,&y,&v);map[x][y]=map[y][x]=v;}dijkstra();printf("%d %d\n",cnt[c2],sum[c2]);return 0;}