当前位置:网站首页>L2-001 emergency rescue (extension of shortest Dijkstra - number of shortest paths & maximum weight of paths)
L2-001 emergency rescue (extension of shortest Dijkstra - number of shortest paths & maximum weight of paths)
2022-04-22 07:26:00 【S atur】


Ideas : Vegetables only seek the shortest path , Will not expand /(ㄒoㄒ)/~~. Reference blog
Code implementation :
#include<bits/stdc++.h>
#define int long long
#define endl '\n'
using namespace std;
const int N = 510;
int n, m, s, d;
int ans, u, v, w;
int g[N][N], c[N]; // Adjacency matrix edge storage , c[i] It means the first one i Number of rescue teams in cities
int dis[N], path[N]; //dis[i] Express s->i Shortest path , path[i] Transfer to i Last status node of
int num[N], p[N], mx[N]; //num[i] Express s->i Number of shortest paths , p[i] Store the cities you pass , mx[i] Express s->i Maximum number of rescues
bool vis[N];
void dijkstra()
{
for(int i = 0; i < n; i ++){
int t = -1;
for(int j = 0; j < n; j ++){ // First find the nearest point that is not used as an intermediate node
if(!vis[j]&&(t==-1||dis[t]>dis[j])){
t = j;
}
}
vis[t] = 1; // Mark it as used
for(int j = 0; j < n; j ++){ // Use this intermediate node to handle other points
if(dis[t]+g[t][j]<=dis[j]){ // Update if the path is shorter
if(dis[t]+g[t][j]==dis[j]){ // situation 1: When the path length is the same ,dis The value is not updated
num[j] += num[t]; // Increased number of paths
if(mx[t]+c[j]>mx[j]){
mx[j] = mx[t]+c[j]; // Update the maximum number of rescues
path[j] = t;
}
}
else{ // The path length is smaller , Then all values are forced to update
num[j] = num[t];
mx[j] = mx[t]+c[j];
path[j] = t;
dis[j] = dis[t]+g[t][j];
}
}
}
}
int last = d; // Start at the end and go back to the starting point
while(last!=s){
p[ans++] = last; // Record an answer every time you pass through a city ans
last = path[last]; // Update the current end value
}
p[ans++] = last; // The starting point also needs to be included in the passing city
cout << num[d] << " " << mx[d] << endl;
for(int i = ans-1; ~i; i --){ // Just export all the cities you pass
cout << p[i] << " \n"[!i];
}
}
signed main()
{
cin >> n >> m >> s >> d;
memset(g, 0x3f, sizeof(g));
memset(dis, 0x3f, sizeof(dis));
for(int i = 0; i < n; i ++){
cin >> c[i];
mx[i] = c[i];
}
for(int i = 0; i < m; i ++){
cin >> u >> v >> w;
g[u][v] = g[v][u] = w;
}
num[s] = 1;
dis[s] = 0;
dijkstra();
return 0;
}
版权声明
本文为[S atur]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204220616059463.html
边栏推荐
- Anaconda安装与使用
- Binary tree chain structure operation leetcode + Niuke (detailed explanation)
- LeetCode - 8 - (三数之和、Z字形变换、两数之和<链表>、盛最多水的容器、电话号码的字母组合)
- (6) DCL and DML syntax of SQL Server
- 详解树状数组模板——理论和代码的实现
- 762 · 最长公共子序列 II
- SQL review, grammar notes, fresh out
- CodeTON Round 1 (Div. 1 + Div. 2, Rated, Prizes)
- 下面这段SQL能否用索引优化查询性能
- 带环链表详解
猜你喜欢

L2-005 set similarity (set judgment)

树+二叉树 详解 详解 +Top-k问题

为什么非主键索引的叶子结点存放的数据是主键值

Two algorithm questions for Microsoft intern interview -- 20220119

(5) Use Navicat to create database data table and set ID auto increment

Host cannot Ping virtual machine in bridging mode

链表难题记录一
![Error: [HSI 55-1545], unable to generate fsbl normally, unable to read in MSS file, failed to close system mss](/img/4e/34e2820ff8579007b20b33b27d8f1d.png)
Error: [HSI 55-1545], unable to generate fsbl normally, unable to read in MSS file, failed to close system mss

L2-001 紧急救援 (最短路Dijkstra的扩展 - 最短路径数&路径最大权值)

L2-002 链表去重(测试点1的坑)
随机推荐
CodeTON Round 1 (Div. 1 + Div. 2, Rated, Prizes)
Precautions for using feign to make remote calls
LaTex中添加作者照片和作者简介
[solution] Luogu p6186 [noi online 1 improvement group] bubble sorting: [bubble sorting] and [reverse order pair] problems
843 · 数字翻转
When latex uses the template, the caption title of the picture cannot be left aligned
What is socket programming?
Codeforces Round #610 (Div. 2)
synchronized锁优化的一些机制(锁升级)
Redis advanced
最强操作符学习之路(C语言详解)
SQL Server quick start
LeetCode - 7 - (二叉树的最近公共祖先、轮转数组、二叉树的直接、下一个排列、组合总和)
二叉树链式结构操作LeetCode+牛客(详解)
小题记录——
Detailed tree array template -- Theory and code implementation
Win10 modify command line default font
L2-002 链表去重(测试点1的坑)
296 · 数组去重
blog同步更新通知