Poj1502 MPI Maelstrom (最短路)

题意:

N个处理器要进行信息传递,处理器i传递信息给自己不需要时间,处理器i与处理器j之间相互传递信息的时间是一样的,不同处理器之间传递信息所需要的时间由一个矩阵的下三角给出。若矩阵对应位置为x,则说明相应的两个处理器之间无法传递信息。求从第一个处理器传递信息到其他所有处理器最少需要多少时间。
题意复制自:https://blog.csdn.net/alongela/article/details/8272339

思路:

其实就是求最短路中最长的那一步。

#include <cstdio>
#include <cstring>
#include <queue>
#include <algorithm>
typedef long long LL;
const int INF = 0x3f3f3f3f;
const int maxn = 100+5;
using namespace std;

int n;
int G[maxn][maxn], dis[maxn];
bool vis[maxn];

struct Node{
	int u,d;
	Node(int a, int b):u(a),d(b){}
	bool operator < (const Node& rhs) const{
		return d > rhs.d;
	}
};
void Dijkstra(int s){
	for(int i = 1; i <= n; ++i) { dis[i] = INF; vis[i] = 0; }
	dis[s] = 0;
	priority_queue<Node> Q;
	Q.push(Node(s, 0));
	while(!Q.empty()){
		Node t = Q.top(); Q.pop();
		int u = t.u;
		if(vis[u]) continue;
		vis[u] = 1;
		for(int v = 1; v <= n; ++v){
			if(dis[v] > dis[u] + G[u][v]){
				dis[v] = dis[u] + G[u][v];
				Q.push(Node(v, dis[v]));
			}
		}
	}
}

int main()
{
    //freopen("in.txt","r",stdin);
    
	while(scanf("%d",&n) == 1){
		for(int i = 1; i <= n; ++i) G[i][i] = 0;
		char s[30];
		for(int i = 2; i <= n; ++i){
			for(int j = 1; j < i; ++j){
				scanf("%s",s);
				if(s[0] == 'x') G[i][j] = G[j][i] = INF;
				else{
					int x; sscanf(s, "%d", &x);
					G[i][j] = G[j][i] = x;
				}
			}
		}
		
		Dijkstra(1);
		
		int ans = 0;
		for(int i = 1; i <= n; ++i) ans = max(ans, dis[i]);
		printf("%d\n",ans);
		
	}
    fclose(stdin);
	return 0;
}


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值