POJ - 1502 MPI Maelstrom Dijkstra

本文介绍了一种使用Dijkstra算法来解决图中从源点到其他各点最短路径最大值的问题,并提供了完整的AC代码实现。该算法适用于带权图,通过不断选择未访问节点中距离最短的一个进行松弛操作来逐步更新所有节点的距离。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

           思路:直接Dijkstra,求源点1到其他各点的最短距离的最大值。

AC代码

#include <cstdio>
#include <cmath>
#include <cctype>
#include <algorithm>
#include <cstring>
#include <utility>
#include <string>
#include <iostream>
#include <map>
#include <set>
#include <vector>
#include <queue>
#include <stack>
using namespace std;
#pragma comment(linker, "/STACK:1024000000,1024000000") 
#define eps 1e-10
#define inf 0x3f3f3f3f
#define PI pair<int, int> 
typedef long long LL;
const int maxn = 100 + 5;
int d[maxn], vis[maxn], w[maxn][maxn];
int n;
int dijkstra(int s) {
	memset(vis, 0, sizeof(vis));
	for(int i = 1; i <= n; ++i) d[i] = inf;
	d[s] = 0;
	for(int i = 0; i < n; ++i) {
		int x, m = inf;
		for(int y = 1; y <= n; ++y) if(!vis[y] && d[y] < m) m = d[x=y];
		vis[x] = 1;
		for(int y = 1; y <= n; ++y) if(w[x][y] < inf) d[y] = min(d[y], d[x] + w[x][y]);
	}
	int ans = 0;
	for(int i = 1; i <= n; ++i) {
		ans = max(ans, d[i]);
	}
	return ans;
}

int main() {
	while(scanf("%d", &n) == 1) {
		for(int i = 1; i <= n; ++i) 
			for(int j = 1; j < n; ++j) {
				w[i][j] = inf;
			}
		char s[50];
		for(int i = 1; i <= n; ++i) {
			for(int j = 1; j < i; ++j) {
				scanf("%s", s);
				if(s[0] != 'x') {
					int dis;
					sscanf(s, "%d", &dis);
					w[i][j] = w[j][i] = dis;
				}
			}
		}
		printf("%d\n", dijkstra(1));
	}
	return 0;
}

如有不当之处欢迎指出!
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值