900字范文,内容丰富有趣,生活中的好帮手!
900字范文 > CodeForces 707B Bakery

CodeForces 707B Bakery

时间:2022-03-02 01:25:13

相关推荐

CodeForces 707B	 Bakery

问题描述:

Masha wants to open her own bakery and bake muffins in one of thencities numbered from1ton. There arembidirectional 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 onlykstorages, located in different cities numbereda1, 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 anothern - kcities, and, of course, flour delivery should be paid— for every kilometer of path between storage and bakery Masha should pay1ruble.

Formally, Masha will payxroubles, if she will open the bakery in some cityb(ai ≠ bfor every1 ≤ i ≤ k) and choose a storage in some citys(s = ajfor some1 ≤ j ≤ k) andbandsare connected by some path of roads of summary lengthx(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 ofkstorages 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 integersn,mandk(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.

Thenmlines follow. Each of them contains three integersu,vandl(1 ≤ u, v ≤ n,1 ≤ l ≤ 109,u ≠ v) meaning that there is a road between citiesuandvof length oflkilometers .

Ifk > 0, then the last line of the input containskdistinct integersa1, a2, ..., ak(1 ≤ ai ≤ n)— the number of cities having flour storage located in. Ifk = 0then this lineis 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 thencities, print - 1in 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.

思路分析:

我用hash写的。这个题直接贪心就可以吧。我应该做的麻烦了一些。

直接从储存面粉的地方开始找最短路。

ac代码:

#include<bits/stdc++.h>using namespace std;#define N 0x3f3f3f3fstruct Node{int u,v,l,nextt;}cnt[400005];int head[400005];bool vis[100005];int n,m,k,i,j,u,v,l,top=0;void add(int a,int b,int c){cnt[top].u=a;cnt[top].v=b;cnt[top].l=c;cnt[top].nextt=head[a];head[a]=top++;}int main(){ios::sync_with_stdio(false);memset(vis,0,sizeof(vis));memset(head,-1,sizeof(head));cin>>n>>m>>k;for(i=0;i<m;i++){cin>>u>>v>>l;add(u,v,l);add(v,u,l);}for(i=0;i<k;i++){cin>>j;vis[j]=1;}int minn=N;for(i=1;i<=n;i++){j=head[i];if(!vis[cnt[j].u])continue;for(j=head[i];j!=-1;j=cnt[j].nextt){if(vis[cnt[j].v])continue;//cout<<"U: "<<cnt[j].u<<" V: "<<cnt[j].v<<" L: "<<cnt[j].l<<endl;minn=min(minn,cnt[j].l);}}if(minn==N)cout<<-1<<endl;elsecout<<minn<<endl;}

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。
扩展阅读