Saturday, May 28, 2011

423 - MPI Maelstrom


#include<iostream>
#include<cstdio>
#include<queue>
#include<cstring>
#define M 105
#define MAXINT 2147483647
using namespace std;

int List[M][M],adjNo[M],W[M][M];
int DjKastra(int);

int main()
{
    register int n,i,j,z;
    char ch[4];
    //freopen("in.txt","r",stdin);

    while(scanf("%d",&n)==1)
    {
        for(i=2;i<=n;i++)
            for(j=1;j<i;j++)
                if(scanf("%d",&z)==1)
                    W[i][j] = W[j][i] = z;
                else
                    scanf("%s",ch);

        z = DjKastra(n);
        printf("%d\n",z);
    }
    return 0;
}

int DjKastra(int n)
{
    priority_queue<pair<int,int> >Q;
    pair <int,int> tmp;
    int h,i,d[M],max=0,w;
    bool Visited[M];
   
    memset(Visited,false,sizeof(Visited));
    for(i=0;i<M;i++)d[i]=MAXINT;

    d[1] = 0;
    Q.push(pair <int,int>(d[1],1));
   
    while(!Q.empty())
    {
        tmp = Q.top();Q.pop();
        h = tmp.second;
        Visited[i] = true;
        if(d[h]>max)max = d[h];

        for (i = 1; i<=n; i++)
        {
            if(W[h][i])
            {
                w = d[h] + W[h][i];

                if(!Visited[i] && d[i] > w)
                {
                    d[i] = w;
                    Q.push(pair <int,int>(-d[i], i));
                }
            }
        }
    }
    return max;
}

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.