PAT 1003. Emergency

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了PAT 1003. Emergency相关的知识,希望对你有一定的参考价值。

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.

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

题目大意:给出一张图,每个节点和每条边各有权值,求从起始点出发到达目标点的最短路径条数,并在这些最短路径中找出所经过点的最大权值和。
解题思路:两次遍历图,第一次找出最短路径的大小,第二次遍历找出最短路径的数目。
 1 #include <iostream> 
 2 #include <cstring>
 3 #define N 505
 4 using namespace std;
 5 int n,m;                //点、边的数目 
 6 int G[N][N];            //
 7 int teams[N];            //存放点的权值 
 8 bool book[N];            //访问标记数组 
 9 int start,aim;            //起始、结束点 
10 int numofpath=0;        //最短路径条数 
11 int minpath=-1;            //最短路径长度 
12 int maxteam=0;            //最短路径长度中的最大权值和 
13 /*
14 *node :访问到的节点
15 *len :当前所经过的路径长度
16 *val :所经过点的权值和 
17 */
18 void dfs1(int node,int len,int val){
19     if(node==aim){
20         if(minpath==-1) {
21             minpath=len;
22             maxteam=val;
23         }
24         else if(minpath>len) {
25             minpath=len;
26             maxteam=val;
27         }else if(minpath==len&&maxteam<val)
28             maxteam=val;
29         return;
30     }
31     for(int i=0;i<n;i++){
32         if(G[node][i]!=-1&&book[i]==false){
33             book[i]=true;
34             dfs1(i,len+G[node][i],val+teams[i]);
35             book[i]=false;
36         }
37     }
38 }
39 void dfs2(int node,int len){
40     if(node==aim&&len==minpath){
41         numofpath++;
42         return;
43     }
44     for(int i=0;i<n;i++){
45         if(G[node][i]!=-1&&book[i]==false){
46             book[i]=true;
47             dfs2(i,len+G[node][i]);
48             book[i]=false;
49         }
50     }
51 }
52 int main(){
53     memset(G,-1,sizeof(G));
54     memset(book,false,sizeof(book));
55     cin>>n>>m>>start>>aim;
56     for(int i=0;i<n;i++)
57         cin>>teams[i];
58     int x,y,l;
59     for(int i=0;i<m;i++){
60         cin>>x>>y>>l;
61         G[x][y]=G[y][x]=l;
62     }
63     book[start]=true;
64     dfs1(start,0,teams[start]);
65     dfs2(start,0);
66     cout<<numofpath<<" "<<maxteam<<endl;
67     return 0;
68 }

 

以上是关于PAT 1003. Emergency的主要内容,如果未能解决你的问题,请参考以下文章

PAT-1003 Emergency

PAT 1003. Emergency

PAT甲级1003 Emergency (25分)

PAT 1003 Emergency (25)(25 分)

菜鸟记录PAT甲级1003--Emergency

PAT Advanced 1003 Emergency (25分)