Dijkstra算法之三
#include <iostream> //poj 1502 MPI Maelstrom
#include<string>
using namespace std;
const int MaxWeight=INT_MAX; //MaxWeight表示无穷大
int Vertex,edge[102][102],distD[102],i,j;
void init()
{
cin>>Vertex;
for(i=1;i<=Vertex;i++)
for(j=1;j<=Vertex;j++)
{
if(i==j)
edge[i][j]=0;
else
edge[i][j]=MaxWeight;
}
string p;
for(int i=2;i<=Vertex;++i)
for(int j=1;j<i;++j)
{
cin>>p;
if(p!="x")
{
int d=0;
for(int k=0;k<p.size();++k)
d=10*d+(p[k]-'0');
edge[j][i]=edge[i][j]=d;
}
}
}
void ShortestPath_Dijstra(int v) //从源点v到其余各顶点的最短路径长度
{
int i,min,u,j,S[102],pathD[102];
for(i=1;i<=Vertex;i++)
{
distD[i]=edge[v][i];S[i]=0;
if(i!=v&&distD[i]<MaxWeight)
pathD[i]=v;
else
pathD[i]=-1;
}
S[v]=1;
for(i=1;i<Vertex;i++)
{
min=MaxWeight;
for(j=1;j<=Vertex;j++)
if(!S[j]&&distD[j]<min)
{
u=j;min=distD[j];
}
S[u]=1;
for(j=1;j<=Vertex;j++)
if(!S[j]&&(long long)distD[u]+edge[u][j]<(long long)distD[j])
{
distD[j]=distD[u]+edge[u][j];pathD[j]=u;
}
}
}
int main()
{
init();
ShortestPath_Dijstra(1);
int mx=0;
for(i=1;i<=Vertex;i++)
mx=max(mx,distD[i]);
cout<<mx<<endl;
}