博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
PAT甲级——A1003Emergency
阅读量:4541 次
发布时间:2019-06-08

本文共 3100 字,大约阅读时间需要 10 分钟。

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

Each input file contains one test case. For each test case, the first line contains 4 positive integers: N (≤) - the number of cities (and the cities are numbered from 0 to N1), 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 Specification:

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 21 2 1 5 30 1 10 2 20 3 11 2 12 4 13 4 1

Sample Output:

2 4 其实就是简单地Dijkstra算法,只不过要加上权重
1 #include 
2 #include
3 4 using namespace std; 5 6 #define INF 99999 7 int main() 8 { 9 int N, M, C1, C2;10 cin >> N >> M >> C1 >> C2;11 vector
>dis(N, vector
(N, INF));//-1表示路不通12 vector
v(N, 0);13 for (int i = 0; i < N; ++i)14 cin >> v[i];15 for (int i = 0; i < M; ++i)16 {17 int a, b, c;18 cin >> a >> b >> c;19 dis[a][b] = dis[b][a] = c;20 }21 vector
index(N, 1);//用来标记是否已经遍历过的点22 vector
w(N, 0);//用来更新权重23 vector
num(N, 0);//用来保存最优路径数量24 vector
D(N, INF);25 D[C1] = 0; // Start开始出发26 w[C1] = v[C1];27 num[C1] = 1;//最开始初始化有一条28 29 for (int i = 0; i < N; ++i)30 {31 //先找出发点去往下一个最近的点32 int p = -1;33 int minD = INF;34 for (int j = 0; j < N; ++j)35 {36 if (index[j] && minD > D[j])37 {38 p = j;39 minD = D[j];40 }41 }42 if (p == -1)43 break;//遍历完毕44 index[p] = false;//已经遍历过了45 //那么就遍历点p能去往的点46 for (int j = 0; j < N; ++j)47 {48 //更新点Start->j的距离49 if (index[j] && D[j] > D[p] + dis[p][j])50 {51 D[j] = D[p] + dis[p][j];52 num[j] = num[p];53 w[j] = w[p] + v[j];54 }55 else if (index[j] && D[j] == D[p] + dis[p][j])//出现相同路径56 {57 num[j] += num[p];//叠加最优路径数量58 w[j] = w[j] > (w[p] + v[j]) ? w[j] : (w[p] + v[j]);//新路的权重是否更大?59 }60 }61 }62 cout << num[C2] << " " << w[C2] << endl;63 64 return 0;65 }

 

 

转载于:https://www.cnblogs.com/zzw1024/p/11167248.html

你可能感兴趣的文章
[总结]数据结构(板子)
查看>>
C# 笔记
查看>>
[转]人人店短信插件开发
查看>>
[转]c# System.IO.Ports SerialPort Class
查看>>
14. 最长公共前缀
查看>>
Redis文档
查看>>
项目重构
查看>>
(笔试题)和一半的组合数
查看>>
leetcode--Algorithm--Array_Part 1 Easy- 566 Reshape the Matrix
查看>>
AC自动机算法详解 (转载)
查看>>
python3-day5(模块)
查看>>
Linux配置JDK
查看>>
qt 读取xml文件
查看>>
python3之正则表达式
查看>>
Visual Studio提示“无法启动IIS Express Web服务器”的解决方法
查看>>
Java 时间总结
查看>>
jQuery EasyUI 拖放 – 基本的拖动和放置
查看>>
这些年正Android - 母亲
查看>>
[工具] BurpSuite--XssValidator插件
查看>>
LPC1788系统时钟初始化
查看>>