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 }
时间: 2024-11-29 00:00:57

PAT 1003. Emergency的相关文章

PAT 1003. Emergency (25)

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 an

PAT 1003 Emergency (25)(25 分)

1003 Emergency (25)(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 betwe

PAT (Advanced Level) Practise 1003 Emergency(SPFA+DFS)

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

1003. Emergency (25)——PAT (Advanced Level) Practise

题目信息: 1003. Emergency (25) 时间限制 400 ms 内存限制 32000 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.

PAT 甲级 1003. 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

【PAT甲级】1003 Emergency (25分)

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 an

迪杰斯特拉算法——PAT 1003

本文主要是将我对于我对于迪杰斯特拉算法的理解写出来,同时通过例题来希望能够加深对于算法的理解,其中有错误的地方希望大家指正. 迪杰斯特拉算法 我将这个算法理解成一个局部到整体的算法,这个方法确实越研究就会发现越经典. 首先可以将整个图的节点看成两个集合:一个是S,一个是U-S.如果是求v0到图中各点的最短距离的话,那么S就是已经确认到v0距离最短的点,U-S则是对于整体的点集合U,还没有加入S集合的点. 这里提出一个算法总体的思想,将所有的点按照一定的原则加入到S集就是解集.而这个解法就是重点了

【PAT】Emergency(最短路条数-SPFA)

[PAT]Emergency(最短路条数-SPFA) 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 betw

1003 Emergency(25 分)C语言版本(提问求解答)

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 an