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 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 <vector>
 3
 4 using namespace std;
 5
 6 const int INF = 0x7fffffff;
 7 struct Node
 8 {
 9     bool isknown = false;
10     int minLength = INF;
11 };
12
13 int rescueTeams[500];
14 int cityMap[500][500];
15 int MaxTeams[500];
16 int minPath[500];
17 Node nodes[500];
18
19 int main()
20 {
21     int cityNum, roadNum, start, destination;
22
23     for (int i = 0; i<500; i++)    //initialize the city map
24         for (int j = 0; j<500; j++)
25             cityMap[i][j] = INF;
26
27     cin >> cityNum >> roadNum >> start >> destination;
28     for (int i = 0; i<cityNum; i++)
29         cin >> rescueTeams[i];
30
31     //construct the city map
32     for (int i = 0; i<roadNum; i++)
33     {
34         int c1, c2, length;
35         cin >> c1 >> c2 >> length;
36         if (cityMap[c1][c2] > length)
37             cityMap[c1][c2] = cityMap[c2][c1] = length;
38     }
39
40     //using dijkstra algorithms
41     nodes[start].isknown = true;    //set the start konwn
42     nodes[start].minLength = 0;
43     MaxTeams[start] = rescueTeams[start];
44     minPath[start] = 1;
45     int minNode = start;
46     int cnt = 1;
47     while (cnt < cityNum)
48     {
49         //update the node connnected to the minimum node
50         for (int j = 0; j<cityNum; j++)
51         {
52             if (cityMap[minNode][j] < INF)
53             {
54                 //with a equal length and record the route
55                 if (nodes[minNode].minLength + cityMap[minNode][j] == nodes[j].minLength)
56                 {
57                     if (MaxTeams[j] < MaxTeams[minNode] + rescueTeams[j])
58                         MaxTeams[j] = MaxTeams[minNode] + rescueTeams[j];
59                     minPath[j] += minPath[minNode];
60                 }
61                 //with a smaller length
62                 else if (nodes[minNode].minLength + cityMap[minNode][j] < nodes[j].minLength)
63                 {
64                     nodes[j].minLength = nodes[minNode].minLength + cityMap[minNode][j];
65                     MaxTeams[j] = rescueTeams[j] + MaxTeams[minNode];
66                     minPath[j] = minPath[minNode];
67                 }
68             }
69         }
70
71         //find the unkonwn node with the minimum length
72         int minLength = INF;
73         for (int i = 0; i<cityNum; i++)
74         {
75             if (!nodes[i].isknown && minLength > nodes[i].minLength)
76             {
77                 minLength = nodes[i].minLength;
78                 minNode = i;
79             }
80         }
81         nodes[minNode].isknown = true;
82         cnt++;
83     }
84
85     cout << minPath[destination] << " " << MaxTeams[destination];
86 }
时间: 2024-10-23 07:58:04

PAT 1003. Emergency (25)的相关文章

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

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

PAT:1003. Emergency (25) AC

#include<stdio.h> #include<string.h> #include<algorithm> using namespace std; const int MAXV=510; const int INF=0x3fffffff; int n,m,c1,c2; bool vis[MAXV]; int G[MAXV][MAXV]; //城市间距离 int weight[MAXV]; //每个城市的救援人数 int d[MAXV]; //最短距离 int w

PAT (Advanced Level) 1003. Emergency (25)

最短路+dfs 先找出可能在最短路上的边,这些边会构成一个DAG,然后在这个DAG上dfs一次就可以得到两个答案了. 也可以对DAG进行拓扑排序,然后DP求解. #include<iostream> #include<cstring> #include<cmath> #include<algorithm> #include<cstdio> #include<queue> #include<vector> using nam

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 e

PAT 1003 Sharing (25)

题目描述 To store English words, one method is to use linked lists and store a word letter by letter. To save some space, we may let the words share the same sublist if they share the same suffix. For example, "loading" and "being" are sto