[solution] MPI Maelstrom POJ - 1502 ⭐ [Dijkstra naked question]

Posted by jpbellavance on Sun, 03 Nov 2019 22:31:52 +0100

MPI Maelstrom POJ - 1502

There are many computers in the laboratory. Because of the different performance of each person's computer, the speed of sending information between computers is different, so the time spent is different.
After messages are sent from the first computer to the second computer, these two computers can send messages to other computers, which is a kind of structure similar to binary tree.
Of course, it's not really a binary tree - our computer has some special features that we should take advantage of.
Our computer allows messages to be sent to any number of other computers connected to it at the same time.
However, messages do not necessarily arrive at the destination at the same time - this involves computer configuration.
In general, we need to consider the time cost of each computer in the topology and plan accordingly to minimize the total time required for message propagation.
Tu ye needs to send messages to other people frequently. She wants to know how long it will take for all the people to receive the notice if she sends a message.

Input

Enter an n in the first line to indicate how many computers there are (TU Ye is No. 1, of course).
Then n-1 line, in the form of adjacency matrix, gives the time needed for communication between computers (1~n).
Because the communication time between two computers is equal, and computers do not need to communicate with themselves, only the lower triangle of matrix is given.
ps: x means that some computers cannot communicate because of special magnetic field.
ps2: all data range of this question is 0-100.

Output

The output line indicates the waiting time of Tu Ye. .

Examples

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

Hint




Title:

Explanation:

Dijkstra naked question

Summary of experience:


#include <cstdio>
#include <iostream>
#include <algorithm>
#include <cstring>
#include <string>
#include <stdlib.h>
#include <vector>
#include <queue>
#include <cmath>
#include <stack>
#include <map>
#include <set>
using namespace std;
#define ms(x, n) memset(x,n,sizeof(x));
typedef  long long LL;
const int inf = 1<<30;
const LL maxn = 110;

int w[maxn][maxn], N;
int d[maxn], used[maxn];
typedef pair<int, int> P;
void Dijkstra(){
    priority_queue<P, vector<P>, greater<P> > q;
    for(int i = 1; i <= N; ++i) d[i] = inf;
    d[1] = 0;
    ms(used, 0);
    q.push(P(d[1], 1));
    while(!q.empty()){
        P cur = q.top();
        q.pop();
        int u = cur.second;
        if(used[u]) continue;
        used[u] = true;
        for(int v = 1; v <= N; ++v)
            if(d[u]+w[u][v] < d[v]){
                d[v] = d[u]+w[u][v];
                q.push(P(d[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, d[i]);
    cout << ans << endl;

	return 0;
}