PAT 1003. Emergency (25) dij+增加点权数组和最短路径个数数组

2024-02-24 17:59

本文主要是介绍PAT 1003. Emergency (25) dij+增加点权数组和最短路径个数数组,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

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

仔细读题,最后需要输出的是最短路径中能够派出急救队数目和最大的数值

源代码:

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cmath>
 4 #include <string>
 5 #include <cstring>
 6 #include <vector>
 7 #include <algorithm>
 8 using namespace std;
 9 
10 const int maxn = 510;
11 const int INF = 0x3f3f3f3f;
12 int n,m,st,ed,G[maxn][maxn],weight[maxn];
13 int d[maxn],w[maxn],num[maxn];
14 bool vis[maxn]={false};
15 
16 void dij(int s) {
17     fill(d,d+maxn,INF);
18     memset(num,0,sizeof(num));
19     memset(w,0,sizeof(w));
20     d[s]=0;
21     w[s]=weight[s];
22     num[s]=1;
23     for(int i=0;i<n;++i) {
24         int u=-1,MIN=INF;
25         for(int j=0;j<n;++j) {
26             if(vis[j]==false&&d[j]<MIN) {
27                 u=j;
28                 MIN=d[j];
29             }
30         }
31         if(u==-1) return;
32         vis[u]=true;
33         for(int v=0;v<n;++v) {
34             if(vis[v]==false&&G[u][v]!=INF) {
35                 if(d[u]+G[u][v]<d[v]) {
36                     d[v]=d[u]+G[u][v];
37                     w[v]=w[u]+weight[v];
38                     num[v]=num[u];
39                 } else if(d[u]+G[u][v]==d[v]) {
40                     if(w[u]+weight[v]>w[v]) {
41                         w[v]=w[u]+weight[v];
42                     }
43                     num[v]+=num[u];
44                 }
45             }
46         }
47     }
48 }
49 int main() {
50     scanf("%d%d%d%d",&n,&m,&st,&ed);
51     for(int i=0;i<n;++i) {
52         scanf("%d",&weight[i]);
53     }
54     int u,v;
55     fill(G[0],G[0]+maxn*maxn,INF);
56     for(int i=0;i<m;++i) {
57         scanf("%d%d",&u,&v);
58         scanf("%d",&G[u][v]);
59         G[v][u]=G[u][v];
60     }
61     dij(st);
62     printf("%d %d\n",num[ed],w[ed]);
63     return 0;
64 }
View Code

 

转载于:https://www.cnblogs.com/lemonbiscuit/p/7775967.html

这篇关于PAT 1003. Emergency (25) dij+增加点权数组和最短路径个数数组的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/742900

相关文章

python获取指定名字的程序的文件路径的两种方法

《python获取指定名字的程序的文件路径的两种方法》本文主要介绍了python获取指定名字的程序的文件路径的两种方法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要... 最近在做项目,需要用到给定一个程序名字就可以自动获取到这个程序在Windows系统下的绝对路径,以下

JavaScript中比较两个数组是否有相同元素(交集)的三种常用方法

《JavaScript中比较两个数组是否有相同元素(交集)的三种常用方法》:本文主要介绍JavaScript中比较两个数组是否有相同元素(交集)的三种常用方法,每种方法结合实例代码给大家介绍的非常... 目录引言:为什么"相等"判断如此重要?方法1:使用some()+includes()(适合小数组)方法2

SpringBoot路径映射配置的实现步骤

《SpringBoot路径映射配置的实现步骤》本文介绍了如何在SpringBoot项目中配置路径映射,使得除static目录外的资源可被访问,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一... 目录SpringBoot路径映射补:springboot 配置虚拟路径映射 @RequestMapp

Java中数组与栈和堆之间的关系说明

《Java中数组与栈和堆之间的关系说明》文章讲解了Java数组的初始化方式、内存存储机制、引用传递特性及遍历、排序、拷贝技巧,强调引用数据类型方法调用时形参可能修改实参,但需注意引用指向单一对象的特性... 目录Java中数组与栈和堆的关系遍历数组接下来是一些编程小技巧总结Java中数组与栈和堆的关系关于

python设置环境变量路径实现过程

《python设置环境变量路径实现过程》本文介绍设置Python路径的多种方法:临时设置(Windows用`set`,Linux/macOS用`export`)、永久设置(系统属性或shell配置文件... 目录设置python路径的方法临时设置环境变量(适用于当前会话)永久设置环境变量(Windows系统

Spring Boot中的路径变量示例详解

《SpringBoot中的路径变量示例详解》SpringBoot中PathVariable通过@PathVariable注解实现URL参数与方法参数绑定,支持多参数接收、类型转换、可选参数、默认值及... 目录一. 基本用法与参数映射1.路径定义2.参数绑定&nhttp://www.chinasem.cnbs

Java中的数组与集合基本用法详解

《Java中的数组与集合基本用法详解》本文介绍了Java数组和集合框架的基础知识,数组部分涵盖了一维、二维及多维数组的声明、初始化、访问与遍历方法,以及Arrays类的常用操作,对Java数组与集合相... 目录一、Java数组基础1.1 数组结构概述1.2 一维数组1.2.1 声明与初始化1.2.2 访问

MySQL查询JSON数组字段包含特定字符串的方法

《MySQL查询JSON数组字段包含特定字符串的方法》在MySQL数据库中,当某个字段存储的是JSON数组,需要查询数组中包含特定字符串的记录时传统的LIKE语句无法直接使用,下面小编就为大家介绍两种... 目录问题背景解决方案对比1. 精确匹配方案(推荐)2. 模糊匹配方案参数化查询示例使用场景建议性能优

关于集合与数组转换实现方法

《关于集合与数组转换实现方法》:本文主要介绍关于集合与数组转换实现方法,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录1、Arrays.asList()1.1、方法作用1.2、内部实现1.3、修改元素的影响1.4、注意事项2、list.toArray()2.1、方

SpringBoot项目配置logback-spring.xml屏蔽特定路径的日志

《SpringBoot项目配置logback-spring.xml屏蔽特定路径的日志》在SpringBoot项目中,使用logback-spring.xml配置屏蔽特定路径的日志有两种常用方式,文中的... 目录方案一:基础配置(直接关闭目标路径日志)方案二:结合 Spring Profile 按环境屏蔽关