MPI Maelstrom POJ 1502 ⭐⭐ 【Dijkstra裸题】

MPI Maelstrom POJ - 1502
实验室有很多台计算机,由于每个人计算机的性能不同,导致计算机之间发送信息的速度不同,所以花费时间不同。
消息从第一台电脑发送到第二台电脑后,这两台电脑能再向其他电脑发送消息,就是一种类似二叉树的结构。
当然并不是真正的二叉树——我们的计算机有一些特殊的特性,我们应该加以利用。
我们的计算机允许同时向连接到它的任意数量的其他计算机发送消息。
然而,消息不一定同时到达目的地——这涉及到计算机配置。
一般来说,我们需要考虑到拓扑结构中每个计算机的时间成本,并相应地计划将消息传播所需的总时间降到最低。
涂爷需要经常给其他人发消息,她想要知道如果她发出一个消息,至少要等多久全部的人才可以都收到通知。

Input
第一行输入一个n,表示有多少台计算机(涂爷当然是一号机啦~)。
随后n-1行,以邻接矩阵的形式给出计算机(1~n)之间通讯需要的时间。
由于两台计算机相互通信时间相等,且计算机自己和自己不需要通信,所以只给出了矩阵的下三角。

ps:x表示由于部分计算机之间存在特殊的磁场并不能通信。
ps2:该题所有数据范围0~100。

Output
输出一行表示涂爷需要等待的时间。.

Examples
Sample Input

5
50
30 5
100 20 50
10 x x 10
Sample Output
35

数据只有100,所以便利使用Floyd也是可以的

Floyd

#include<iostream>
#include<cstdio>
#include<cmath>
#include<algorithm>
#include<cstring>
#include<queue>
#include<vector>
using namespace std;

const int maxn = 110;
const int inf = 0x3f3f3f3f;
int e[maxn][maxn];
int n;

void floyd() {
	for (int k = 1; k <= n; ++k)
		for (int i = 1; i <= n; ++i)
			for (int j = 1; j <= n; ++j)
				if (e[i][j] > e[i][k] + e[k][j])
					e[i][j] = e[i][k] + e[k][j];
}

int main() {
	//freopen("in.txt","r",stdin);
	ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
	cin >> n; char c[20];
	for (int i = 1; i <= n; ++i)for (int j = 1; j <= n; ++j)e[i][j] = inf;
	for (int i = 1; i <= n; ++i)e[i][i] = 0;
	for(int i = 2;i<=n;++i)
		for (int j = 1; j <= i - 1; ++j) {
			cin >> c;
			if (c[0] == 'x')continue;
			 e[i][j] = e[j][i] = atoi(c);
		}
	floyd();
	int ans = 0;
	for (int i = 2; i <= n; i++) {
		if (e[1][i] != inf)
			ans = max(ans, e[1][i]);
	}
	cout << ans << endl;
}

堆优化的Dijkstra

#include<iostream>
#include<cstdio>
#include<cmath>
#include<algorithm>
#include<cstring>
#include<queue>
#include<vector>
using namespace std;
typedef pair<int, int> PII;
const int inf = 0x3f3f3f3f;
const int maxn = 110;
int w[maxn][maxn];
int dis[maxn];
bool book[maxn];
int n;

void  Dijkstra() {
    priority_queue<PII, vector<PII>, greater<PII> > q;
    memset(book, false, sizeof book);
    for (int i = 2; i <= n; ++i)dis[i] = inf;
    dis[1] = 0; book[0] = true;
    q.push({ 0,1 });
    while (q.size()) {
        PII cur = q.top(); q.pop();
        int u = cur.second;
        if (book[u])continue;
        book[u] = true;
        for (int v = 1; v <= n; ++v) {
            if (dis[u] + w[u][v] < dis[v]) {
                dis[v] = dis[u] + w[u][v];
                q.push({ dis[v], v });
            }
        }
    }
}

int Stoi(string s) {
    int ret = 0;
    for (int i = s.length() - 1, j = 1; i >= 0; --i, j *= 10)
        ret += (s[i] - '0') * j;
    return ret;
}

int main() {
    string input;
    fill(w[0], w[0] + maxn * maxn, inf);
    cin >> n;
    for (int i = 2; i <= n; ++i)
        for (int j = 1; j < i; ++j) {
            cin >> input;
            if (input != "x") w[i][j] = w[j][i] = Stoi(input);
        }
    Dijkstra();
    int ans = 0;
    for (int i = 2; i <= n; ++i)
        ans = max(ans, dis[i]);
    cout << ans << endl;
    return 0;
}
原文地址:https://www.cnblogs.com/RioTian/p/13379373.html