城市间紧急救援 Dijkstra
输出格式:
第一行输出最短路径的条数和能够召集的最多的救援队数量。第二行输出从S到D的路径中经过的城市编号。数字间以空格分隔,输出结尾不能有多余空格。
输入样例:
4 5 0 3
20 30 40 10
0 1 1
1 3 2
0 3 3
0 2 2
2 3 2
输出样例:
2 60
0 1 3
#include <bits/stdc++.h>
using namespace std;
const int maxn = ;
int n,m,s,d;
int cnt[maxn],vis[maxn],num[maxn],snum[maxn],pre[maxn];
//最短路径条数 是否经过 救援队数量 到达该城市救援队数量 前一个城市
int a[maxn][maxn]; void Dijkstra(){
vis[s] = ;cnt[s] = ;pre[s] = -;
for(int i = ; i < n; i++){
int min = 0x3f,f = -;
for(int j = ; j < n; j++){
if(!vis[j] && a[s][j] < min){
min = a[s][j];
f = j;
}
}
if(f == -) break;
vis[f] = ;
for(int j = ; j < n; j++){
if(!vis[j] && a[s][j] > a[s][f] + a[f][j]){
a[s][j] = a[s][f] + a[f][j];
pre[j] = f;
cnt[j] = cnt[f];
snum[j] = num[j] + snum[f];
}
else if(!vis[j] && a[s][j] == a[s][f] + a[f][j]){
cnt[j] += cnt[f];
if(snum[j] < snum[f] + num[j]){
pre[j] = f;
snum[j] = snum[f] + num[j];
}
}
}
}
}
int ans[maxn],tp;
void path(int x){
ans[tp++] = x;
while(pre[x] != -){
ans[tp++] = pre[x];
x = pre[x];
}
}
int main() {
//freopen("in","r",stdin);
ios::sync_with_stdio();
cin >> n >> m >> s >> d;
for(int i = ; i < n; i++){
cin >> num[i];
snum[i] = num[i];
cnt[i] = ;
}
memset(a,0x3f, sizeof(a));
for(int i = ; i < n; i++) a[i][i] = ;
while(m--){
int x,y,z;
cin >> x >> y >> z;
a[x][y] = z;a[y][x] = z;
}
Dijkstra();
path(d);
cout << cnt[d] << " " << snum[d] + num[s] << endl;
for(int i = tp - ; i >= ; i--)
if(i) cout << ans[i] << " ";
else cout << ans[i];
return ;
}
城市间紧急救援 Dijkstra的更多相关文章
- PTA-数据结构 Dijkstra 城市间紧急救援
城市间紧急救援(25 分) 作为一个城市的应急救援队伍的负责人,你有一张特殊的全国地图.在地图上显示有多个分散的城市和一些连接城市的快速道路.每个城市的救援队数量和每一条连接两个城市的快速道路长度都标 ...
- PTA数据结构与算法题目集(中文) 7-35 城市间紧急救援 (25 分)
PTA数据结构与算法题目集(中文) 7-35 城市间紧急救援 (25 分) 作为一个城市的应急救援队伍的负责人,你有一张特殊的全国地图.在地图上显示有多个分散的城市和一些连接城市的快速道路.每个城市 ...
- 城市间紧急救援(25 分)(dijstra变形)
城市间紧急救援(25 分) 作为一个城市的应急救援队伍的负责人,你有一张特殊的全国地图.在地图上显示有多个分散的城市和一些连接城市的快速道路.每个城市的救援队数量和每一条连接两个城市的快速道路长度都标 ...
- 5-5 城市间紧急救援 (25分)【最短路spfa】
5-5 城市间紧急救援 (25分) 作为一个城市的应急救援队伍的负责人,你有一张特殊的全国地图.在地图上显示有多个分散的城市和一些连接城市的快速道路.每个城市的救援队数量和每一条连接两个城市的快速 ...
- L2-001. 紧急救援---(Dijkstra,记录路径)
https://www.patest.cn/contests/gplt/L2-001 L2-001. 紧急救援 时间限制 200 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 ...
- L2-001. 紧急救援 (Dijkstra算法打印路径)
作为一个城市的应急救援队伍的负责人,你有一张特殊的全国地图.在地图上显示有多个分散的城市和一些连接城市的快速道路.每个城市的救援队数量和每一条连接两个城市的快速道路长度都标在地图上.当其他城市有紧急求 ...
- pta—紧急救援 (dijkstra)
题目连接:https://pintia.cn/problem-sets/994805046380707840/problems/994805073643683840 题面: 作为一个城市的应急救援队伍 ...
- 计算城市间的球面距离(C++实现)
#include<iostream> #include<string> #include<cmath> #include<iomanip> using ...
- pta l2-1紧急救援(Dijkstra)
题目链接:https://pintia.cn/problem-sets/994805046380707840/problems/994805073643683840 题意:给n个城市,m条边,每个城市 ...
随机推荐
- 错误:pip._vendor.urllib3.exceptions.ReadTimeoutError: HTTPSConnectionPool(host='files.pythonhosted.org', port=443): Read timed out.
pip._vendor.urllib3.exceptions.ReadTimeoutError: HTTPSConnectionPool(host='files.pythonhosted.org', ...
- layer iframe 设置关闭按钮 和刷新
layer.open({ type: 2, title: 'XXXX网吧历史更多数据', shade:0, // closeBtn:0, resize:false, move:false, shade ...
- Centos7 安装VNC实现远程桌面
1.设置root用户方法: 1.# 复制一个服务设置的模板,命令如下: cp /lib/systemd/system/vncserver@.service /etc/systemd/system/vn ...
- js json -> <-object
1.利用原生JSON对象,将对象转为字符串 var jsObj = {}; jsObj.testArray = [1,2,3,4,5]; jsObj.name = 'CSS3'; jsObj.date ...
- Visual detection of structural changes in time-varying graphs using persistent homology
PKU blog about this paper Basic knowledge: 1. what is time-varying graphs? time-varying graph VS st ...
- opencv3.3 基础:Mat类里setTo函数
Mat& setTo(InputArray value, InputArray mask=noArray()); 说明: 1.功能:把矩阵mask中元素不为0的点全部变为value值: 2.当 ...
- opencv学习之颜色空间转换cvtColor()
我们生活中大多数看到的彩色图片都是RGB类型,但是在进行图像处理时,需要用到灰度图.二值图.HSV.HSI等颜色制式,opencv提供了cvtColor()函数来实现这些功能.首先看一下cvtColo ...
- HTML的链接标签
网页的链接标签 文本超链接 图像超链接 格式:<a href="path" target="目标窗口位置" >链接文本或图像</a> 锚 ...
- pikaqiu练习平台(CSRF(跨站请求伪造) )
CSRF(跨站请求伪造) CSRF(跨站请求伪造)概述 Cross-site request forgery 简称为“CSRF”,在CSRF的攻击场景中攻击者会伪造一个请求(这个请求一般是一个链接 ...
- windows系统 安装 mysql.fx
windows系统 安装 mqtt.fx 软件官网:http://mqttfx.jfx4ee.org/ 软件下载:http://www.jensd.de/apps/mqttfx/1.1.0/