POJ 1502 MPI Maelstrom

dijkstra  SSIP

#include <stdio.h>
#include
<string.h>
#include
<stdlib.h>
using namespace std;
const int MAXN = 101;
const int MAXINT = 32767;
int G[MAXN][MAXN];
bool used[MAXN];
int pre[MAXN];
int dist[MAXN];

void dijkstra(int n){
for(int i = 1; i <= n; i++){
dist[i]
= G[1][i];
used[i]
= false;
if( dist[i] != MAXINT )
pre[i]
= 1;
else
pre[i]
= 0;
}
dist[
1] = 0;
used[
1] = true;
for(int i = 1; i < n ; i++){
int min = MAXINT;
int minIndex = 1;
for(int j = 1; j <= n; j++){
if( !used[j] && dist[j] < min){
min
= dist[j];
minIndex
= j;
}
}
used[minIndex]
= true;
for(int j = 1; j <= n; j++){
if( !used[j] && dist[minIndex] + G[minIndex][j] < dist[j] ){
dist[j]
= G[minIndex][j] + dist[minIndex];
pre[j]
= minIndex;
}
}
}
}


int main(){
int n;
char c[20];
memset(G,
0,sizeof(G));
scanf(
"%d",&n);
for(int i = 2; i <= n; i++){
for(int j = 1; j < i; j++){
scanf(
"%s",c);
if( strcmp(c,"x")){
G[i][j]
= G[j][i] = atoi(c);
}
else {
G[i][j]
= G[j][i] = MAXINT;
}
}
}
dijkstra(n);
int max = dist[1];
for(int i = 2; i <= n; i++){
if(max < dist[i])
max
= dist[i];
}
printf(
"%d\n",max);
return 0;
}

posted @ 2011-04-21 15:26  L..  阅读(203)  评论(0编辑  收藏  举报