zoukankan      html  css  js  c++  java
  • hdu 2066 一个人的旅行

    题目连接

    http://acm.hdu.edu.cn/showproblem.php?pid=2066   

    一个人的旅行

    Description

    虽然草儿是个路痴(就是在杭电待了一年多,居然还会在校园里迷路的人,汗~),但是草儿仍然很喜欢旅行,因为在旅途中 会遇见很多人(白马王子,^0^),很多事,还能丰富自己的阅历,还可以看美丽的风景……草儿想去很多地方,她想要去东京铁塔看夜景,去威尼斯看电影,去阳明山上看海芋,去纽约纯粹看雪景,去巴黎喝咖啡写信,去北京探望孟姜女……眼看寒假就快到了,这么一大段时间,可不能浪费啊,一定要给自己好好的放个假,可是也不能荒废了训练啊,所以草儿决定在要在最短的时间去一个自己想去的地方!因为草儿的家在一个小镇上,没有火车经过,所以她只能去邻近的城市坐火车(好可怜啊~)。

    Input

    输入数据有多组,每组的第一行是三个整数$T,S$和$D$,表示有$T$条路,和草儿家相邻的城市的有$S$个,草儿想去的地方有$D$个;
    接着有$T$行,每行有三个整数$a,b,time$,表示$a, b$城市之间的车程是$time$4小时;$(1 leq (a,b) leq 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

    最短路。。

     1 #include<algorithm>
     2 #include<iostream>
     3 #include<cstdlib>
     4 #include<cstring>
     5 #include<cstdio>
     6 #include<vector>
     7 #include<queue>
     8 #include<map>
     9 using std::min;
    10 using std::cin;
    11 using std::cout;
    12 using std::endl;
    13 using std::find;
    14 using std::sort;
    15 using std::pair;
    16 using std::vector;
    17 using std::multimap;
    18 using std::priority_queue;
    19 #define pb(e) push_back(e)
    20 #define sz(c) (int)(c).size()
    21 #define mp(a, b) make_pair(a, b)
    22 #define all(c) (c).begin(), (c).end()
    23 #define iter(c) decltype((c).begin())
    24 #define cls(arr,val) memset(arr,val,sizeof(arr))
    25 #define cpresent(c, e) (find(all(c), (e)) != (c).end())
    26 #define rep(i, n) for (int i = 0; i < (int)(n); i++)
    27 #define tr(c, i) for (iter(c) i = (c).begin(); i != (c).end(); ++i)
    28 const int N = 10010;
    29 typedef unsigned long long ull;
    30 struct P {
    31     int w, v;
    32     P(int i = 0, int j = 0) :w(i), v(j) {}
    33     inline bool operator<(const P &a) const {
    34         return w > a.w;
    35     }
    36 };
    37 struct Node { int to, w, next; };
    38 struct Dijkstra {
    39     Node G[N];
    40     int tot, head[N], dist[N];
    41     inline void init() {
    42         tot = 0, cls(head, -1);
    43     }
    44     inline void add_edge(int u, int v, int w) {
    45         G[tot] = { v, w, head[u] }; head[u] = tot++;
    46     }
    47     inline void built(int n) {
    48         int u, v, w;
    49         rep(i, n) {
    50             scanf("%d %d %d", &u, &v, &w);
    51             add_edge(u, v, w), add_edge(v, u, w);
    52         }
    53     }
    54     inline void dijkstra(vector<int> &st) {
    55         cls(dist, 0x3f);
    56         priority_queue<P> q;
    57         rep(i, sz(st)) {
    58             q.push(P(0, st[i]));
    59             dist[st[i]] = 0;
    60         }
    61         while (!q.empty()) {
    62             P t = q.top(); q.pop();
    63             int u = t.v;
    64             if (dist[u] < t.w) continue;
    65             for (int i = head[u]; ~i; i = G[i].next) {
    66                 int &w = dist[G[i].to];
    67                 if (w > dist[u] + G[i].w) {
    68                     w = dist[u] + G[i].w;
    69                     q.push(P(w, G[i].to));
    70                 }
    71             }
    72         }
    73     }
    74     inline void work(int s, int d) {
    75         int v, ans = 0x3f3f3f3f;
    76         vector<int> A, B;
    77         while (s--) { scanf("%d", &v); A.push_back(v); }
    78         while (d--) { scanf("%d", &v); B.push_back(v); }
    79         dijkstra(A);
    80         rep(i, sz(B)) ans = min(ans, dist[B[i]]);
    81         printf("%d
    ", ans);
    82     }
    83 }go;
    84 int main() {
    85 #ifdef LOCAL
    86     freopen("in.txt", "r", stdin);
    87     freopen("out.txt", "w+", stdout);
    88 #endif
    89     int n, s, d;
    90     while (~scanf("%d %d %d", &n, &s, &d)) {
    91         go.init();
    92         go.built(n);
    93         go.work(s, d);
    94     }
    95     return 0;
    96 }
    View Code
    By: GadyPu 博客地址:http://www.cnblogs.com/GadyPu/ 转载请说明
  • 相关阅读:
    HDU 4069 Squiggly Sudoku
    SPOJ 1771 Yet Another NQueen Problem
    POJ 3469 Dual Core CPU
    CF 118E Bertown roads
    URAL 1664 Pipeline Transportation
    POJ 3076 Sudoku
    UVA 10330 Power Transmission
    HDU 1426 Sudoku Killer
    POJ 3074 Sudoku
    HDU 3315 My Brute
  • 原文地址:https://www.cnblogs.com/GadyPu/p/4652735.html
Copyright © 2011-2022 走看看