# PAT甲级 1003.Emergency(25) 题目翻译与答案

1003. Emergency (25)

As an emergencyrescue team leader of a city, you are given a special map of your country. Themap shows several scattered cities connected by some roads. Amount of rescueteams in each city and the length of each road between any pair of cities aremarked 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 themean time, call up as many hands on the way as possible.

Input

Each input filecontains one test case. For each test case, the first line contains 4 positiveintegers: N (<= 500) - the number of cities (and the cities are numberedfrom 0 to N-1), M - the number of roads, C1 and C2 - the cities that you arecurrently in and that you must save, respectively. The next line contains Nintegers, where the i-th integer is the number of rescue teams in the i-thcity. Then M lines follow, each describes a road with three integers c1, c2 andL, which are the pair of cities connected by a road and the length of thatroad, respectively. It is guaranteed that there exists at least one path fromC1 to C2.

Output

For each test case,print in one line two numbers: the number of different shortest paths betweenC1 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 isno extra space allowed at the end of a line.

SampleInput

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

SampleOutput

2 4

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

1003.突发事件(25)

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

2 4

#include<cstdio>
#define MAXN 100000
//#define QWERTY
int main()
{
int N,M,C1,C2;
scanf("%d%d%d%d",&N,&M,&C1,&C2);
int SaveMen[N];
int arcs[N][N];
int Num[N];
int Team[N];
int i,i1,i2,d;
for(i1=0;i1<N;++i1)
{
for(i2=0;i2<N;i2++)
{
arcs[i1][i2]=MAXN;
if(i1==i2)
arcs[i1][i2]=0;
}
}
for(i=0;i<N;++i)
scanf("%d",&SaveMen[i]);
for(i=0;i<M;++i)
{
scanf("%d%d%d",&i1,&i2,&d);
arcs[i1][i2]=d;
arcs[i2][i1]=d;
}

bool S[N];
int dist[N],path[N];
for(i=0;i<N;++i)
{
S[i]=false;
dist[i]=arcs[C1][i];
//path[i]=C1;
if(i==C1)
{
Num[C1]=1;
Team[C1]=SaveMen[C1];
}else
{
Num[i]=0;
Team[i]=0;
}
}

int c=-1;
while(c!=C2)
{
d=MAXN;
for(i=0;i<N;++i)
{
if(S[i]==true)
continue;
if(dist[i]<d)
{
i1=i;
d=dist[i];
}
}
S[i1]=true;
c=i1;
#ifdef QWERTY
printf("@@@@@@@ begin \n");
for(i=0;i<N;++i)
{
printf("%d ",dist[i]);
}
putchar('\n');
for(i=0;i<N;++i)
{
printf("%d ",Team[i]);
}
putchar('\n');
printf("@@@@@@@ end \n");
#endif
for(i=0;i<N;++i)
{
if(S[i]==true)
continue;
if(dist[i]>dist[c]+arcs[c][i])
{
dist[i]=dist[c]+arcs[c][i];
//path[i]=c;
Team[i]=SaveMen[i]+Team[c];
Num[i]=Num[c];
}else if(dist[i]==dist[c]+arcs[c][i])
{
Num[i]=Num[i]+Num[c];
if(Team[i]<Team[c]+SaveMen[i])
Team[i]=Team[c]+SaveMen[i];
}
}
}
int sum=0;
//while(c!=C1)
//{
//printf("%d ",c);
//	sum+=SaveMen[c];
//	c=path[c];
//}
//sum+=SaveMen[C1];
printf("%d %d",Num[C2],Team[C2]);
return 0;
}