码迷,mamicode.com
首页 > 其他好文 > 详细

hdu2066——一个人的旅行

时间:2014-11-21 14:30:52      阅读:178      评论:0      收藏:0      [点我收藏+]

标签:最短路

一个人的旅行

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


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

最短路,加一个超级源点就行

#include <map>
#include <set>
#include <list>
#include <queue>
#include <stack>
#include <vector>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>

using namespace std;

const int N = 1010;
const int inf = 0x3f3f3f3f;

int dist[N];
int head[N];
int want[N];
int tot, n, m;
typedef pair<int, int> PLL;

struct node
{
    int weight;
    int next;
    int to;
}edge[N * N];

void addedge(int from, int to, int weight)
{
    edge[tot].weight = weight;
    edge[tot].to = to;
    edge[tot].next = head[from];
    head[from] = tot++;
}

void dijkstra(int v0)
{
    memset (dist, inf, sizeof(dist));
    dist[v0] = 0;
    priority_queue < PLL, vector<PLL>, greater<PLL> > qu;
    while (!qu.empty())
    {
        qu.pop();
    }
    dist[v0] = 0;
    qu.push(make_pair(dist[v0], v0));
    while (!qu.empty())
    {
        PLL tmp = qu.top();
        qu.pop();
        int u = tmp.second;
        int d = tmp.first;
        for (int i = head[u]; ~i; i = edge[i].next)
        {
            int v = edge[i].to;
            if (dist[v] > d + edge[i].weight)
            {
                dist[v] = d + edge[i].weight;
                qu.push(make_pair(dist[v], v));
            }
        }
    }
}

int main()
{
    int t, s, d, u, v, w;
    while (~scanf("%d%d%d", &t, &s, &d))
    {
        memset (head, -1, sizeof(head));
        tot = 0;
        for (int i = 0; i < t; ++i)
        {
            scanf("%d%d%d", &u, &v, &w);
            addedge(u, v, w);
            addedge(v, u, w);
        }
        for (int i = 0; i < s; ++i)
        {
            scanf("%d", &u);
            addedge(0, u, 0);
            addedge(u, 0, 0);
        }
        for (int i = 0; i < d; ++i)
        {
            scanf("%d", &want[i]);
        }
        dijkstra(0);
        int ans = inf;
        for (int i = 0; i < d; ++i)
        {
            ans = min(ans, dist[want[i]]);
        }
        printf("%d\n", ans);
    }
    return 0;
}


hdu2066——一个人的旅行

标签:最短路

原文地址:http://blog.csdn.net/guard_mine/article/details/41346359

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!