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

题目:

样例:

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

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

思路:

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

从 a 点到 b点所花时间为 b 即 gab = b ,从 b 点到 a 点所花时间为 a 即 gba = 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;
}

最后提交:

相关推荐
郝学胜-神的一滴4 小时前
《C++11 工程级应用01:告别冗长类型,开启简洁高效编码新时代》深度解读
开发语言·c++·算法·软件开发·系统设计
学习中.........5 小时前
Transformer 训练资源估算:以 CS336 GPT-2 XL 配置为例
人工智能·python·算法·机器学习·自然语言处理
Navigator_Z10 小时前
LeetCode //C - 1206. Design Skiplist
c语言·算法·leetcode
码行山野赴时序归途10 小时前
三道经典数组题:从暴力到最优的算法思维
c语言·开发语言·数据结构·算法·leetcode
徐小夕11 小时前
3分钟从想法到Agent上线:我们开源了一款AI可视化工作流“IDE”
前端·算法·github
sel_912 小时前
【强化学习】Hands-on Modern RL项目实践|OPD 算法完整解析
人工智能·深度学习·算法·机器学习·语言模型
Navigator_Z13 小时前
LeetCode //C - 1209. Remove All Adjacent Duplicates in String II
c语言·算法·leetcode
ShineWinsu15 小时前
对于 C++:C++14中从变量模板、泛型 Lambda 到并发与字面量的解析
c++·算法
土司大王15 小时前
LeetCode hot100——合并两个有序链表
算法·leetcode·链表