zoukankan      html  css  js  c++  java
  • Codeforces Round #368 (Div. 2) Bakery

    Bakery

    Masha wants to open her own bakery and bake muffins in one of the n cities numbered from 1 to n. There are m bidirectional roads, each of whose connects some pair of cities.

    To bake muffins in her bakery, Masha needs to establish flour supply from some storage. There are only k storages, located in different cities numbered a1, a2, ..., ak.

    Unforunately the law of the country Masha lives in prohibits opening bakery in any of the cities which has storage located in it. She can open it only in one of another n - k cities, and, of course, flour delivery should be paid — for every kilometer of path between storage and bakery Masha should pay 1 ruble.

    Formally, Masha will pay x roubles, if she will open the bakery in some city b (ai ≠ b for every 1 ≤ i ≤ k) and choose a storage in some city s (s = aj for some 1 ≤ j ≤ k) and b and s are connected by some path of roads of summary length x (if there are more than one path, Masha is able to choose which of them should be used).

    Masha is very thrifty and rational. She is interested in a city, where she can open her bakery (and choose one of k storages and one of the paths between city with bakery and city with storage) and pay minimum possible amount of rubles for flour delivery. Please help Masha find this amount.

    Input
     

    The first line of the input contains three integers n, m and k (1 ≤ n, m ≤ 105, 0 ≤ k ≤ n) — the number of cities in country Masha lives in, the number of roads between them and the number of flour storages respectively.

    Then m lines follow. Each of them contains three integers u, v and l (1 ≤ u, v ≤ n, 1 ≤ l ≤ 109, u ≠ v) meaning that there is a road between cities u and v of length of l kilometers .

    If k > 0, then the last line of the input contains k distinct integers a1, a2, ..., ak (1 ≤ ai ≤ n) — the number of cities having flour storage located in. If k = 0 then this line is not presented in the input.

    Output
     

    Print the minimum possible amount of rubles Masha should pay for flour delivery in the only line.

    If the bakery can not be opened (while satisfying conditions) in any of the n cities, print  - 1 in the only line.

    Examples
     
    Input
     
    5 4 2
    1 2 5
    1 2 3
    2 3 4
    1 4 10
    1 5

    Output
     
    3

    Input
     
    3 1 1
    1 2 3
    3
    Output
     
    -1
    Note

    Image illustrates the first sample case. Cities with storage located in and the road representing the answer are darkened.

    题意:

      给出n个城市,其中有k个城市提供原料,其他n-k个城市开商店,

    求商店与原料的最短路径

    思路:

      暴力解决,每条路都走一遍,ok

    AC代码
     1 # include <iostream>
     2 # include <vector>
     3 # include <cstring>
     4 using namespace std;
     5 const int MAX = 1e5 + 1;
     6 const int MAX2 = 1e9 + 1;
     7 struct node
     8 {
     9     int x;
    10     int y;
    11     int len;
    12 };
    13 bool vis[MAX];
    14 int main()
    15 {
    16     int n, m, k;
    17     cin >> n >> m >> k;
    18     vector <node> v;
    19     memset(vis, false, sizeof(vis));
    20     for(int i = 0; i < m; i++)
    21     {
    22         node n1;
    23         cin >> n1.x >> n1.y >> n1.len;
    24         v.push_back(n1);
    25     }
    26     for(int i = 0; i < k; i++)
    27     {
    28         int t;
    29         cin >> t;
    30         vis[t] = true;
    31     }
    32     int max = MAX2;
    33     for(int i = 0; i < v.size(); i++)
    34     {
    35         if(vis[v[i].x] && !vis[v[i].y])
    36         {
    37             if(max > v[i].len)
    38             {
    39                 max = v[i].len;
    40             }
    41         }
    42         else if(!vis[v[i].x] && vis[v[i].y])
    43         {
    44             if(max > v[i].len)
    45             {
    46                 max = v[i].len;
    47             }
    48         }
    49     }
    50     if(max == MAX2)
    51         cout << "-1" << endl;
    52     else
    53         cout << max << endl;
    54     
    55     return 0;
    56 }
    View Code
    
    
    




     
    生命不息,奋斗不止,这才叫青春,青春就是拥有热情相信未来。
  • 相关阅读:
    [GSEAPY] 在Python里进行基因集富集分析
    scRNAseq R包公共单细胞数据获取
    pybedtools:在Python中使用BEDTools
    pybedtools 提取序列
    将博客搬至CSDN
    【转】SELECT INTO 和 INSERT INTO SELECT 两种表复制语句
    sql长日期数据以短日期格式显示【转】
    [转]YouTube架构学习体会
    [转]让Nginx 支持 ASP ASP.NET配置方法
    [转]LINQ查询总结
  • 原文地址:https://www.cnblogs.com/lyf-acm/p/5791581.html
Copyright © 2011-2022 走看看