HDU 2066 一个人的旅行

一个人的旅行

Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 15141    Accepted Submission(s): 5148


Problem Description
虽然草儿是个路痴(就是在杭电待了一年多,居然还会在校园里迷路的人,汗~),但是草儿仍然很喜欢旅行,因为在旅途中 会遇见很多人(白马王子,^0^),很多事,还能丰富自己的阅历,还可以看美丽的风景……草儿想去很多地方,她想要去东京铁塔看夜景,去威尼斯看电影,去阳明山上看海芋,去纽约纯粹看雪景,去巴黎喝咖啡写信,去北京探望孟姜女……眼看寒假就快到了,这么一大段时间,可不能浪费啊,一定要给自己好好的放个假,可是也不能荒废了训练啊,所以草儿决定在要在最短的时间去一个自己想去的地方!因为草儿的家在一个小镇上,没有火车经过,所以她只能去邻近的城市坐火车(好可怜啊~)。
 
Input
输入数据有多组,每组的第一行是三个整数T,S和D,表示有T条路,和草儿家相邻的城市的有S个,草儿想去的地方有D个;
接着有T行,每行有三个整数a,b,time,表示a,b城市之间的车程是time小时;(1=<(a,b)<=1000;a,b 之间可能有多条路)
接着的第T+1行有S个数,表示和草儿家相连的城市;
接着的第T+2行有D个数,表示草儿想去地方。
 
Output
输出草儿能去某个喜欢的城市的最短时间。
 
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
 
Author
Grass
 
Source
 
Recommend
lcy   |   We have carefully selected several similar problems for you:  1874 2112 1217 1142 1385 
 
思路:开始使用Floyd超时,后来使用Dijstra过了,把所有的起点指向一点,距离设为0,所有终点指向一点,
距离设为0
 
代码:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
using namespace std;
const int MAXN = 100000000;
int map[1100][1100];
int the_max;
int the_last_flag;
int dist[1100];
int t,s,d;
int a,b,tim;
int begin,end;
void Dijkstra(int n,int dist[1100],int map[1100][1100],int s)
{
    int minn;
    bool p[1100];
    for(int i = 0;i <= n;i ++)
    {
        if(i != s)
        {
            dist[i] = map[s][i];
            p[i] = false;
        }
    }
    dist[s] = 0;
    p[s] = true;
    for(int i = 0;i <= n - 1;i ++)
    {
        minn = MAXN;
        the_last_flag = 0;
        for(int j = 0;j <= n;j ++)
        {
            if(!p[j] && dist[j] < minn)
            {
                the_last_flag = j;
                minn = dist[j];
            }
        }
        if(the_last_flag == 0)
            break ;
        if(the_last_flag == n)
            break ;
        p[the_last_flag] = true;
        for(int j = 0;j <= n;j ++)
        {
            if(!p[j] && map[the_last_flag][j] != MAXN && dist[j] > dist[the_last_flag]
               + map[the_last_flag][j])
               {
                   dist[j] = dist[the_last_flag] + map[the_last_flag][j];
               }
        }
    }
}
int main()
{
    while(~scanf("%d%d%d",&t,&s,&d))
    {
        for(int i = 0;i <= 1001;i ++)
            for(int j = 0;j <= 1001;j ++)
            {
                if(i == j)
                   map[i][j] = 0;
                else
                   map[i][j] = MAXN;
            }
        the_max = 0;
        while(t --)
        {
            scanf("%d%d%d",&a,&b,&tim);
            if(map[a][b] > tim)
            {
                map[a][b] = map[b][a] = tim;
            }
            if(a > the_max)
                the_max = a;
            if(b > the_max)
                the_max = b;
        }
        the_max ++;
        while(s --)
        {
            scanf("%d",&begin);
            map[0][begin] = map[begin][0] = 0;
        }
        while(d --)
        {
            scanf("%d",&end);
            map[the_max][end] = map[end][the_max] = 0;
        }
        Dijkstra(the_max,dist,map,0);
        printf("%d
",dist[the_max]);
    }
    return 0;
}
原文地址:https://www.cnblogs.com/GODLIKEING/p/3406896.html