POJ - 1502MPI Maelstrom

BIT has recently taken delivery of their new supercomputer, a 32 processor Apollo Odyssey distributed shared memory machine with a hierarchical communication subsystem. Valentine McKee’s research advisor, Jack Swigert, has asked her to benchmark the new system.
求源点到所有点最短距离中的最长距离,spfa解决。

#include<iostream>
#include<queue>
#include<math.h>
#include<stdio.h>
#include<string>
using namespace std;
#define N 6000+5
#define LL long long int
#define pow(a) ((a)*(a))
#define INF 0x3f3f3f3f
#define mem(arr,a) memset(arr,a,sizeof(arr))
struct edge{
    int to;
    int cost;
};

int d[N];
int used[N];
int num[N];
int V, E = 0;
vector<edge> e[N];
bool spfa(int s){
    mem(d, INF);
    mem(num, 0);
    d[s] = 0;
    queue<int>q;
    q.push(s);
    while (!q.empty()){
        int x = q.front(); q.pop();
        for (int i = 0; i < e[x].size(); i++){
            edge temp = e[x][i];
            int t = temp.to;
            if (d[t]>d[x] + temp.cost){
                d[t] = d[x] + temp.cost;
                q.push(t);
                num[t]++;
                if (num[t] == V){
                    return false;
                }
            }
        }
    }
    return true;
}
int main(){
    cin >> V;
    for (int i = 2; i <= V; i++){
        for (int j = 1; j < i; j++){
            edge es;
            string cost; cin >> cost;
            if (cost[0] == 'x'){
                j += cost.size() - 1;
                continue;
            }
            else
            {
                es.to = j; es.cost = atoi(cost.c_str());
                e[i].push_back(es);
                es.to = i;
                e[j].push_back(es);
            }
        }
    }
    int MAX = 0;
    if (spfa(1)){
        for (int i = 1; i <= V; i++){
            MAX = max(MAX, d[i]);
        }
        cout << MAX << endl;
    }
    else{
        cout << "NO" << endl;
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值