A person's travel

A person's travel

Although Cao'er is a road idiot (that is, someone who has stayed in Hangdian for more than a year, but still gets lost in the campus, Khan~), but Cao'er still likes to travel, because she will meet many people during the journey (Prince Charming, ^0^), a lot of things, but also enriching her own experience and seeing beautiful scenery... Cao'er wants to go to many places, she wants to go to Tokyo Tower to see the night view, go to Venice to watch movies, and go to Yangmingshan to see the sea Taro, go to New York just to watch the snow scene, go to Paris to drink coffee and write a letter, and go to Beijing to visit Meng Jiangnu... As the winter vacation is coming soon, such a long time cannot be wasted, you must give yourself a good vacation, but also You can't waste training, so Cao'er decided to go to a place she wants to go in the shortest possible time! Because Cao'er's home is in a small town, there is no train passing by, so she can only go to the neighboring city to take the train (so pitiful~).

Input
There are multiple groups of input data, the first line of each group is three integers T, S and D, indicating that there are T roads, S in the city adjacent to Cao'er's house, and D in the place where Cao'er wants to go ;
Then there are T lines, each line has three integers a, b, time, indicating that the drive between cities a and b is time hours; (1=<(a,b)<=1000; there may be Multiple roads)
The next line T+1 has S numbers, indicating the city connected to Cao'er's house;
the next line T+2 has D numbers, indicating the place Cao'er wants to go.
Output
Output the shortest time that Cao'er can go to a favorite city.
Sample Input
6 2 3
1 3 5
1 4 7
2 8 12
3 8 4
4 9 12
9 10 2
1 2
8 9 10
Sample Output
9 
Use dijkstra algorithm
to pay attention to simplification, easy to time out
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <cstring>
#include <string>
#include <cmath>
#include <algorithm>
using namespace std;
const int MAXN=99999999;

int T,S,D,map[1010][1010],dis[1010],n;
bool mark[1010];

void Dijkstra(int x)
{
    int i,j,k,Min;
    for(i=1;i<=n;i++)
    {
        mark[i]=false;
        dis[i]=map[x][i];
    }
    mark[x]=true;
    do
    {
        Min = MAXN;
        k=-1;
        for(i=1;i<=n;i++)
            if((mark[i]==false)&&(dis[i]<Min))
            {
                Min = dis [i];
                k=i;
            }
        if(k>-1)
        {
            mark[k]=true;
            for(i=1;i<=n;i++)
                if(dis[i]>dis[k]+map[k][i]) dis[i]=dis[k]+map[k][i];
        }
    }while(k>-1);
}

intmain ()
{
    while(scanf("%d%d%d",&T,&S,&D)!=EOF)
    {
        n=0;
        for(int i=0;i<=1001;i++)
        {
            map[i][i]=0;
            for(int j=1;j<=1001;j++)
                map[i][j]=map[j][i]=MAXN;
        }
        int x,y,z;
        for(int i=1;i<=T;i++)
        {
            scanf("%d%d%d",&x,&y,&z);
            n=max(max(y,x),n);
            if(z<map[x][y]) map[x][y]=map[y][x]=z;
        }
        for(int i=1;i<=S;i++)
        {
            scanf("%d",&x);
            map[x][0]=map[0][x]=0;
        }
        //p();
        Dijkstra(0);
        int minn=MAXN;
        for(int i=1;i<=D;i++)
        {
            
            scanf("%d",&x);
            if(minn>dis[x]) minn=dis[x];
        }
        printf("%d\n",minn);
    }
    return 0;
}

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325344887&siteId=291194637