pat1003. Emergency (25)

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 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

提交代码

 

dijstra做法:

 1 #include <cstdio>
 2 #include <cstring>
 3 #include <string>
 4 #include <queue>
 5 #include <stack>
 6 #include <iostream>
 7 using namespace std;
 8 #define size 505
 9 int city[size],map[size][size],team[size],roadnum[size],dis[size];
10 int n,m,s,e;
11 void dijstra(int s,int e){
12     int i,j,k;
13     dis[s]=0;//表示已经在集合中
14     roadnum[s]=1;
15     while(s!=e){
16         for(i=0;i<n;i++){
17             if((dis[i]==-1&&map[s][i]!=-1)||(dis[i]>0&&dis[s]>0&&map[s][i]>0&&dis[i]>dis[s]+map[s][i])){//更新
18                 dis[i]=dis[s]+map[s][i];
19                 team[i]=team[s]+city[i];
20                 roadnum[i]=roadnum[s];
21             }
22             else{
23                 if(dis[i]>0&&dis[s]>0&&map[s][i]>0&&dis[i]==dis[s]+map[s][i]){
24                     if(team[i]<team[s]+city[i])
25                     team[i]=team[s]+city[i];
26                     roadnum[i]+=roadnum[s];
27                 }
28             }
29         }
30         dis[s]=0;
31         int min=-1;
32         for(i=0;i<n;i++){
33             if(dis[i]>0&&(min==-1||dis[i]<min)){
34                 min=dis[i];
35                 s=i;
36             }
37         }
38     }
39 }
40 int main(){
41     //freopen("D:\\INPUT.txt","r",stdin);
42     scanf("%d %d %d %d",&n,&m,&s,&e);
43     int i,j,k;
44     for(i=0;i<n;i++){
45         scanf("%d",&city[i]);
46     }
47     int a,b,l;
48     for(i=0;i<n;i++){
49         dis[i]=-1;
50         team[i]=city[i];
51         for(j=0;j<n;j++){
52             map[i][j]=map[j][i]=-1;//初始化
53         }
54     }
55     for(i=0;i<m;i++){
56         scanf("%d %d %d",&a,&b,&l);
57         if(map[a][b]==-1||map[a][b]>l)//确保最小化
58         map[a][b]=map[b][a]=l;
59     }
60     dijstra(s,e);
61     printf("%d %d\n",roadnum[e],team[e]);
62     return 0;
63 }

 

DFS做法:

 

posted @ 2015-07-20 16:28  Deribs4  阅读(171)  评论(0编辑  收藏  举报