贪心+简单图——Bakery

来源:互联网 发布:mac os 正式版镜像 编辑:程序博客网 时间:2024/05/17 22:08
B. Bakery
time limit per test
 2 seconds
memory limit per test
 256 megabytes
input
 standard input
output
 standard output

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 nm and k (1 ≤ n, m ≤ 1050 ≤ 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 uv and l (1 ≤ u, v ≤ n1 ≤ l ≤ 109u ≠ 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 21 2 51 2 32 3 41 4 101 5
output
3
input
3 1 11 2 33
output
-1
Note

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



【题意】
n个城市,m条双向路

n个城市中有k个城市中有仓库

现在要在一个没有仓库的城市开一家面包店,使得该面包店能够至少到达一个仓库,且到达该仓库的距离尽可能小

问最小距离为多少,若没有城市适合开设面包店,则输出"-1"


看懂题意就能做出来了,一条变的两端必须一端是没有面粉仓库的城市,一边是有面粉仓库的城市,然后比较满足这个条件的边的大小,选出最小的边,把值打印出来就行了,就是简单的贪心


AC代码:

[cpp] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. #include<cstdio>  
  2. #include<iostream>  
  3. #include<algorithm>  
  4. #include<cstring>  
  5. using namespace std;  
  6.   
  7.   
  8. const int maxm = 100002;  
  9. const int maxn = 100002;  
  10. const int inf = 0x3f3f3f3f;  
  11.   
  12. int n, m, k;  
  13. struct node {  
  14.     int x, y, w;  
  15. };  
  16. node edge[maxm*2];  
  17. int book[maxn];  
  18. int main()  
  19. {  
  20.     while(~scanf("%d%d%d", &n, &m, &k)) {  
  21.         memset(book, 0, sizeof(book));  
  22.         int i;  
  23.         for(i=0; i<m; ++i)  
  24.             scanf("%d%d%d", &edge[i].x, &edge[i].y, &edge[i].w);  
  25.         if(!k) {  
  26.             printf("-1\n");  
  27.             continue;  
  28.         }  
  29.         int a;  
  30.         for(i=0; i<k; ++i) {  
  31.             scanf("%d", &a);  
  32.             book[a] = 1;  
  33.         }  
  34.         int ans = inf;  
  35.         for(i=0; i<m; ++i) {  
  36.             if(book[edge[i].x] + book[edge[i].y] == 1) {  
  37.                 ans = min(ans, edge[i].w);  
  38.             }  
  39.         }  
  40.         if(ans == inf)  
  41.             printf("-1\n");  
  42.         else  printf("%d\n", ans);  
  43.     }  
  44.       
  45. }  
0 0
原创粉丝点击