Online JudgeProblem SetAuthorsOnline ContestsUser
Web Board
Home Page
F.A.Qs
Statistical Charts
Problems
Submit Problem
Online Status
Prob.ID:
Register
Update your info
Authors ranklist
Current Contest
Past Contests
Scheduled Contests
Award Contest
User ID:
Password:
  Register

Dijistra过的~~~

Posted by renhuan0737 at 2011-03-03 21:41:21 on Problem 1502
#include <cstdlib>
#include <iostream>
#include <cstdio>
#include <cstring> 
using namespace std;
const int M=105,INF=1<<30;
int mp[M][M],n,dist[M],flag[M],D[M][M];
int dijstra()
{
     int i,j,u,min;
     for(i=1;i<=n;i++)
     {
         dist[i]=INF;
         flag[i]=0;   
     }
     dist[1]=0;
     for(i=1;i<=n;i++)
     {
          min=INF,u=0;
          for(j=1;j<=n;j++)
          {
                if(!flag[j]&&dist[j]<min)
                {
                     min=dist[j];
                     u=j;
                }
          }
          flag[u]=1;
          for(j=1;j<=n;j++)
             if(!flag[j]&&(dist[j]>dist[u]+mp[u][j]))
             dist[j]=dist[u]+mp[u][j];
             
     }
     return min;
}

int main(int argc, char *argv[])
{
    int i,j;
    char ch[100];
    while(scanf("%d",&n)!=EOF)
    {
        for(i=2;i<=n;i++)
        {
                for(j=1;j<=i-1;j++)
                {
                    scanf("%s",ch);
                    if(ch[0]=='x')
                    mp[i][j]=mp[j][i]=INF;
                    else
                    mp[i][j]=mp[j][i]=atoi(ch);               
                }
        }
        for(i=1;i<=n;i++)
        {
             mp[i][i]=0;
        }
        /*
        for(i=1;i<=n;i++)
        {
             for(j=1;j<=n;j++)
             printf("%d ",mp[i][j]);
             printf("\n");
        }
        */
        //ans=0;
       //dijstra();
        printf("%d\n",dijstra());
    }
    //system("PAUSE");
    return EXIT_SUCCESS;
}

Followed by:

Post your reply here:
User ID:
Password:
Title:

Content:

Home Page   Go Back  To top


All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator