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;
}
}