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

相关文章

MySQL9.0默认路径安装下重置root密码

《MySQL9.0默认路径安装下重置root密码》本文主要介绍了MySQL9.0默认路径安装下重置root密码,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们... 目录问题描述环境描述解决方法正常模式下修改密码报错原因问题描述mysqlChina编程采用默认安装路径,

C++一个数组赋值给另一个数组方式

《C++一个数组赋值给另一个数组方式》文章介绍了三种在C++中将一个数组赋值给另一个数组的方法:使用循环逐个元素赋值、使用标准库函数std::copy或std::memcpy以及使用标准库容器,每种方... 目录C++一个数组赋值给另一个数组循环遍历赋值使用标准库中的函数 std::copy 或 std::

C++初始化数组的几种常见方法(简单易懂)

《C++初始化数组的几种常见方法(简单易懂)》本文介绍了C++中数组的初始化方法,包括一维数组和二维数组的初始化,以及用new动态初始化数组,在C++11及以上版本中,还提供了使用std::array... 目录1、初始化一维数组1.1、使用列表初始化(推荐方式)1.2、初始化部分列表1.3、使用std::

C++ Primer 多维数组的使用

《C++Primer多维数组的使用》本文主要介绍了多维数组在C++语言中的定义、初始化、下标引用以及使用范围for语句处理多维数组的方法,具有一定的参考价值,感兴趣的可以了解一下... 目录多维数组多维数组的初始化多维数组的下标引用使用范围for语句处理多维数组指针和多维数组多维数组严格来说,C++语言没

Java 字符数组转字符串的常用方法

《Java字符数组转字符串的常用方法》文章总结了在Java中将字符数组转换为字符串的几种常用方法,包括使用String构造函数、String.valueOf()方法、StringBuilder以及A... 目录1. 使用String构造函数1.1 基本转换方法1.2 注意事项2. 使用String.valu

五大特性引领创新! 深度操作系统 deepin 25 Preview预览版发布

《五大特性引领创新!深度操作系统deepin25Preview预览版发布》今日,深度操作系统正式推出deepin25Preview版本,该版本集成了五大核心特性:磐石系统、全新DDE、Tr... 深度操作系统今日发布了 deepin 25 Preview,新版本囊括五大特性:磐石系统、全新 DDE、Tree

JAVA中整型数组、字符串数组、整型数和字符串 的创建与转换的方法

《JAVA中整型数组、字符串数组、整型数和字符串的创建与转换的方法》本文介绍了Java中字符串、字符数组和整型数组的创建方法,以及它们之间的转换方法,还详细讲解了字符串中的一些常用方法,如index... 目录一、字符串、字符数组和整型数组的创建1、字符串的创建方法1.1 通过引用字符数组来创建字符串1.2

python获取当前文件和目录路径的方法详解

《python获取当前文件和目录路径的方法详解》:本文主要介绍Python中获取当前文件路径和目录的方法,包括使用__file__关键字、os.path.abspath、os.path.realp... 目录1、获取当前文件路径2、获取当前文件所在目录3、os.path.abspath和os.path.re

vue如何监听对象或者数组某个属性的变化详解

《vue如何监听对象或者数组某个属性的变化详解》这篇文章主要给大家介绍了关于vue如何监听对象或者数组某个属性的变化,在Vue.js中可以通过watch监听属性变化并动态修改其他属性的值,watch通... 目录前言用watch监听深度监听使用计算属性watch和计算属性的区别在vue 3中使用watchE

hdu2241(二分+合并数组)

题意:判断是否存在a+b+c = x,a,b,c分别属于集合A,B,C 如果用暴力会超时,所以这里用到了数组合并,将b,c数组合并成d,d数组存的是b,c数组元素的和,然后对d数组进行二分就可以了 代码如下(附注释): #include<iostream>#include<algorithm>#include<cstring>#include<stack>#include<que