第一次用dijkstr算法把每个起点找到最短的时间,然后进行比较,但是就是过不了。

网上看的先以0为起点然后到原先起点的那个点的时间设为0,再用dijkstr算法就能一次性找出最短时间

代码:

#include<iostream>
#include<string.h>
using namespace std;
#define Max 999999999
int map[1005][1005];
int
dis[1005],hash[1005];
int
star,en[1005];
int
t,s,d;
int
djs()
{

    int
i,j;
    int
index,minn,sc;
    memset( hash, 0, sizeof( hash ) );
    for
(i=1;i<1005;i++)
    {

      dis[i]=map[0][i];   
    }

    for
(j=1;j<1005;j++)
    {

        minn=Max;
        index=0;
        for
(i=1;i<1005;i++)
        {

            if
(!hash[i]&&dis[i]<minn)
            {

                minn=dis[i];
                index=i;
            }
        }

        hash[index]=1;
        for
(i=1;i<1005;i++)
        {

            if
(!hash[i]&&dis[i]>dis[index]+map[index][i])
                dis[i]=dis[index]+map[index][i];
        }    
    }

    sc=Max;
    for
(j=1;j<=d;j++)
    {

        if
(sc>dis[en[j]])
            sc=dis[en[j]];
    }

    return
sc;
}

int
main()
{

    int
x,y,st,jg;
    int
i,j;
    while
(cin>>t>>s>>d)
    {

        jg=0;
    /*    memset( map, Max, sizeof( map ) );*/

        for
(i=0;i<1005;i++)
        {

            for
( j=0;j<1005;j++)
                map[i][j]=i==j?0:Max;
        }

        for
( i=1;i<=t;i++)
        {

            cin>>x>>y>>st;
            if
(st<map[x][y])
               map[x][y]=map[y][x]=st;
        /*    map[x][y] = map[y][x] = min( st, map[x][y] );
                        jg = max( jg, max( x, y ) );*/

        }

        for
( j=1;j<=s;j++)
        {

            cin>>star;
            map[0][star] = map[star][0] = 0;
        }

        for
(int i=1;i<=d;i++)
          cin>>en[i];
        cout<<djs()<<endl;   
    }

    return
0;
}

 

posted on 2012-07-10 11:59  xinmenghuairi  阅读(364)  评论(0编辑  收藏  举报