https://pintia.cn/problem-sets/994805342720868352/problems/994805523835109376

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 N−1), M - the number of roads, C​1​​ and C​2​​ - 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 c​1​​, c​2​​ 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 C​1​​ to C​2​​.

Output Specification:

For each test case, print in one line two numbers: the number of different shortest paths between C​1​​ and C​2​​, 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

时间复杂度:$O(2 * N ^ 2)$

代码:

#include <bits/stdc++.h>
using namespace std; #define inf 0x3f3f3f3f
int N, M, C1, C2;
int cnt = 0;
int mp[550][550];
int team[550];
int vis[550], dis[550];
int see[550];
int amount[550]; void dijkstra(int act) {
memset(vis, 0, sizeof(vis));
memset(dis, inf, sizeof(dis)); dis[act] = 0;
int temp = act;
see[act] = 1;
amount[act] = team[act]; for(int i = 0; i < N; i ++) {
dis[i] = mp[act][i];
if(dis[i] != inf && i != act) {
amount[i] = amount[act] + team[i];
see[i] = 1;
}
} for(int i = 0; i < N - 1; i ++) {
int minn = inf;
for(int j = 0; j < N; j ++) {
if(dis[j] < minn && vis[j] == 0) {
minn = dis[j];
temp = j;
}
} vis[temp] = 1;
for(int k = 0; k < N; k ++) {
if(!vis[k] && dis[k] > dis[temp] + mp[temp][k]) {
dis[k] = dis[temp] + mp[temp][k];
amount[k] = amount[temp] + team[k];
see[k] = see[temp];
}
else if(!vis[k] && dis[k] == dis[temp] + mp[temp][k]) {
see[k] += see[temp];
if (amount[k] < amount[temp]+team[k])
amount[k] = amount[temp]+team[k];
}
}
}
return;
} int main() {
scanf("%d%d%d%d", &N, &M, &C1, &C2);
for(int i = 0; i < N; i ++)
scanf("%d", &team[i]); memset(mp, inf, sizeof(mp));
for(int i = 1; i <= M; i ++) {
int a, b, cost;
scanf("%d%d%d", &a, &b, &cost);
mp[a][b] = mp[b][a] = cost;
} dijkstra(C1);
printf("%d %d\n", see[C2], amount[C2]);
return 0;
}

  

PAT 甲级 1003 Emergency的更多相关文章

  1. PAT甲级1003. Emergency

    PAT甲级1003. Emergency 题意: 作为一个城市的紧急救援队长,你将得到一个你所在国家的特别地图.该地图显示了几条分散的城市,连接着一些道路.每个城市的救援队数量和任何一对城市之间的每条 ...

  2. 图论 - PAT甲级 1003 Emergency C++

    PAT甲级 1003 Emergency C++ As an emergency rescue team leader of a city, you are given a special map o ...

  3. PAT 甲级 1003. Emergency (25)

    1003. Emergency (25) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue As an emerg ...

  4. PAT 甲级1003 Emergency (25)(25 分)(Dikjstra,也可以自己到自己!)

    As an emergency rescue team leader of a city, you are given a special map of your country. The map s ...

  5. PAT Advanced 1003 Emergency 详解

    题目与翻译 1003 Emergency 紧急情况 (25分) As an emergency rescue team leader of a city, you are given a specia ...

  6. PAT Advanced 1003 Emergency (25) [Dijkstra算法]

    题目 As an emergency rescue team leader of a city, you are given a special map of your country. The ma ...

  7. PAT甲级1003题解——Dijkstra

    解题步骤: 1.初始化:设置mat[][]存放点之间的距离,vis[]存放点的选取情况,people[]存放初始时每个城市的人数,man[]存放到达每个城市的救援队的最多的人数,num[]存放到达每个 ...

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

    1003. Emergency (25) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue As an emerg ...

  9. PAT 解题报告 1003. Emergency (25)

    1003. Emergency (25) As an emergency rescue team leader of a city, you are given a special map of yo ...

随机推荐

  1. MySQL 避免行锁升级为表锁——使用高效的索引

    文章目录 普通索引 属性值重复率高 属性值重复率低 小结 众所周知,MySQL 的 InnoDB 存储引擎支持事务,支持行级锁(innodb的行锁是通过给索引项加锁实现的).得益于这些特性,数据库支持 ...

  2. 【TOJ 4475】The Coolest Sub-matrix(对角线前缀和)

    描述 Given an N*N matrix, find the coolest square sub-matrix.We define the cool value of the square ma ...

  3. 关于Navicat连接MySQL 报 Authentication plugin 'caching_sha2_password' cannot be loaded

    报错原因: 报这个错是因为MySQL8使用了 caching_sha2_password 加密方式而之前MySQL使用的是 mysql_native_password 加密方式,而你的Navicat不 ...

  4. grafana使用json数据源监控数据

    功能实现完后有部分数据一直在波动,就产生了想把这个数据波动集成到grafana形成可视化界面的监控,但grafana不支持mongo数据库又懒得去用其他工具转换,特意看了下grafana的databa ...

  5. json_decode结果为null的几种原因

    值只能是UTF-8编码,元素最后不能有逗号,元素不能使用单引号,元素值中间不能有空格和n.

  6. hive的desc命令

    desc命令 desc 命令是为了展示hive表格的内在属性.例如列名,data_type,存储位置等信息.这个命令常常用在我们对hive表格观察之时,我们想要知道这个hive各个列名(基于这些具体列 ...

  7. struts2学习

    struts2是一种基于mvc模式的轻量级web框架,它本质上相当于一个servlet,在mvc设计模式中,struts2作为控制器(Controller)来建立模型与视图的数据交互,struts2采 ...

  8. 第1章 MATLAB概述

    MATLAB系统由~开发环境.~语言.~数学函数库.~图形处理系统.~应用程序接口(API)5大部分组成. 界面 命令行中的语句格式 命令行的语句格式:>>变量=表达式(没有>> ...

  9. 转:python教程专题资源免费下载整理合集收藏

    python教程专题资源免费下载整理合集收藏 < Python学习手册(第4版)>(Learning Python, 4th Edition)[PDF] 94MB 简体中文 <Pyt ...

  10. C语言常用关键语法精华总结

    1.关于typedef的用法总结 2.typedef struct的用法 3.typedef函数指针用法 4.数组指针(数组类型的指针)与指针数组 5.真正明白c语言二级指针 6.C语言for循环(及 ...