codeforces707Bdiv2Bakery+贪心
来源:互联网 发布:更改路由器端口 编辑:程序博客网 时间:2024/04/28 20:50
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
Image illustrates the first sample case. Cities with storage located in and the road representing the answer are darkened.
题意:有n个城市,窝有k个仓库,规定我点商店不能在仓库所在的城市,很多条路径a到b花费为c,每走一千米我要花费1卢布。问我开商店的最小花费。。
解法:直接找一个仓库周围的城市没有仓库的城市最为商店,找一个路径最小的即可。。
#include<cstdio>#include<cmath>#include<cstring>#include<iostream>#include<algorithm>#include<queue>#include<vector>#include<map>#include<stack>#pragma comment(linker,"/STACK:102400000,102400000")#define pi acos(-1.0)#define EPS 1e-6#define INF (1<<28)#define inf 0x3f3f3f3fusing namespace std;#define LL long longbool visit[100005];int l[100005];struct R{ int x,y,c;}rode[100005];int main(){ int n,m,k; scanf("%d %d %d",&n,&m,&k); for(int i=1;i<=m;i++) scanf("%d %d %d",&rode[i].x,&rode[i].y,&rode[i].c); for(int i=1;i<=k;i++){ scanf("%d",&l[i]); visit[l[i]]=true; } int ans=inf; for(int i=1;i<=m;i++){ if(visit[rode[i].x]==false&&visit[rode[i].y]==true) ans=min(ans,rode[i].c); if(visit[rode[i].y]==false&&visit[rode[i].x]==true) ans=min(ans,rode[i].c); } if(ans==inf) printf("-1\n"); else printf("%d\n",ans); return 0;}
- codeforces707Bdiv2Bakery+贪心
- 贪心!
- 贪心~
- 贪心
- 贪心
- 贪心
- 贪心
- 贪心
- 贪心
- 贪心
- 贪心
- 贪心
- 贪心
- 贪心
- 贪心
- 贪心
- 贪心
- 贪心
- codeforces707Cdiv2Pythagorean Triples+勾股数
- 使用LUA userdata手动绑定C++对象
- UVA457 - Linear Cellular Automata
- R programming week #4 assigment
- linux下各种*-dev或*-devel包的作用
- codeforces707Bdiv2Bakery+贪心
- 51nod 1126 求递推序列的第N项
- 13章广义线性模型
- codeforces707Adiv2Brain's Photos+水题
- 【bzoj 1798】[AHOI2009]维护序列
- Qt5.7.0 日志输出文件之debug重定向
- 1038. Recover the Smallest Number (30)-PAT甲级真题(贪心算法)
- python 多线程中的同步锁 Lock Rlock Semaphore Event Condition
- 深入TR室内定位算法