47. 参加科学大会(第六期模拟笔试)(最短路)

题目:

样例:

|---------------------------------|
| 4 5 0 2 7 3 1 2 1 3 2 3 2 4 3 4 |
[输入]

|---|
| 5 |
[输出]

思路:

由题意,很明显这是一道最短路径问题,但是不同的是,这里没有给出边的长度,而是以结点权值的形式,变相的作为边长,这一我们应该注意的是,这里的意思为

从 a 点到 b点所花时间为 b 即 g[a][b] = b ,从 b 点到 a 点所花时间为 a 即 g[b][a] = a

所以我们根据题意,更改一下Dijkstra模板函数即可。

代码详解如下:

cpp 复制代码
#include <iostream>
#include <cstring>
#include <algorithm>
#include <queue>
#include <unordered_map>
#define endl '\n'
#define int long long
#define YES puts("YES")
#define NO puts("NO")
#define umap unordered_map
#define INF 0x3f3f3f3f
#define All(x) (x).begin(),(x).end()
#pragma GCC optimize(3,"Ofast","inline")
#define ___G std::ios::sync_with_stdio(false),cin.tie(0), cout.tie(0)
using namespace std;
const int N = 1500 + 10;
int n,m;

int g[N][N];	// 所到结点所花的时间,即路径长度

umap<int,int>dist;	// 记录最短路径

umap<int,int>d;	// 记录结点权值,即所花时间

umap<int,bool>st;

inline int Dijkstra()
{
	// 初始化起点最短距离
	dist[1] = d[1];
	// 初始化 dist ,走一遍起点到每一个点的最短距离
	for(int i = 2;i <= n;++i)
	{
		dist[i] = g[1][i];
	}
	// 从起点走完一遍后,标记起点我们检查过了
	st[1] = true;

	// 这里 i = 2 是因为我们刚开始已经走过一遍起点了
	for(int i = 2;i < n;++i)
	{
		// t 探头探索哪一个点所花时间最少,即最短距离
		int t = -1;
		for(int j = 1;j <= n;++j)
		{
			if(!st[j] && (t == -1 || dist[j] < dist[t])) t = j;
		}
		
		// 标记并走向该结点
		st[t] = true;
		
		// 更新所有结点最短距离
		for(int j = 1;j <= n;++j)
		{
			dist[j] = min(dist[j],dist[t] + g[t][j]);
		}
	}
	
	// 这里返回结果最后加上 d[1] 是因为可能起点也有需要花费的时间
	return dist[n] + d[1];
}


inline void solve()
{
	memset(g,INF,sizeof g);
	cin >> n >> m;	
	
	// 输入结点权值
	for(int i = 1;i <= n;++i)
	{
		cin >> d[i];
	}
	
	while(m--)
	{
		int a,b;
		cin >> a >> b;
		
		// 记录 a 点 到 b 点 的距离
		g[a][b] = d[b];
		
		// 记录 b 点 到 a 点 的距离
		g[b][a] = d[a];
	}
	
	int ans = Dijkstra();
	
	cout << ans << endl;
	
}


signed main()
{
//	freopen("a.txt", "r", stdin);
	___G;
	int _t = 1;
//	cin >> _t;
	while (_t--)
	{
		solve();
	}

	return 0;
}

最后提交:

相关推荐
星释6 小时前
Rust 练习册 :Pythagorean Triplet与数学算法
开发语言·算法·rust
星释6 小时前
Rust 练习册 :Nth Prime与素数算法
开发语言·算法·rust
多喝开水少熬夜6 小时前
Trie树相关算法题java实现
java·开发语言·算法
WBluuue7 小时前
数据结构与算法:树上倍增与LCA
数据结构·c++·算法
bruk_spp7 小时前
牛客网华为在线编程题
算法
黑屋里的马9 小时前
java的设计模式之桥接模式(Bridge)
java·算法·桥接模式
sin_hielo9 小时前
leetcode 1611
算法·leetcode
李小白杂货铺9 小时前
识别和破除信息茧房
算法·信息茧房·识别信息茧房·破除信息茧房·算法推荐型茧房·观点过滤型茧房·茧房
来荔枝一大筐10 小时前
C++ LeetCode 力扣刷题 541. 反转字符串 II
c++·算法·leetcode
暴风鱼划水10 小时前
算法题(Python)数组篇 | 6.区间和
python·算法·数组·区间和