Description

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 ≤ 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 uv 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.

正解:搜索

解题报告:

  直接搜索每个仓库有没有与非仓库直接相连,若相连则更新答案。显然可行。

 #include<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<cstdlib>
#include<algorithm>
using namespace std;
const int MAXN = ;
const int MAXM = ;
const int inf = (<<);
int n,m,k,ecnt,ans;
int first[MAXN],next[MAXM],to[MAXM],w[MAXM];
bool in[MAXN]; inline int getint(){
int w=,q=;char c=getchar();
while(c!='-' && (c<'' || c>'')) c=getchar();
if(c=='-') q=-,c=getchar();
while(c>='' && c<='') w=w*+c-'',c=getchar();
return w*q;
} int main()
{
n=getint(); m=getint(); k=getint();
int x,y,z;
for(int i=;i<=m;i++){
x=getint(); y=getint(); z=getint();
next[++ecnt]=first[x]; first[x]=ecnt; to[ecnt]=y; w[ecnt]=z;
next[++ecnt]=first[y]; first[y]=ecnt; to[ecnt]=x; w[ecnt]=z;
}
for(int o=;o<=k;o++){
x=getint(); in[x]=;
}
ans=inf;
for(int i=;i<=n;i++) {
if(in[i]) for(int j=first[i];j;j=next[j]) if(!in[to[j]]) ans=min(ans,w[j]);
}
if(ans==inf) printf("-1"); else printf("%d",ans);
return ;
}

codeforces707B:Bakery的更多相关文章

  1. java web 开发三剑客 -------电子书

    Internet,人们通常称为因特网,是当今世界上覆盖面最大和应用最广泛的网络.根据英语构词法,Internet是Inter + net,Inter-作为前缀在英语中表示“在一起,交互”,由此可知In ...

  2. 所有selenium相关的库

    通过爬虫 获取 官方文档库 如果想获取 相应的库 修改对应配置即可 代码如下 from urllib.parse import urljoin import requests from lxml im ...

  3. 读书笔记2014第6本:《The Hunger Games》

    以前从未读过一本完整的英文小说,所有就在今年的读书目标中增加了一本英文小说,但在头四个月内一直没有下定决定读哪一本.一次偶然从SUN的QQ空间中看到Mockingjay,说是不错的英文小说,好像已经是 ...

  4. Codeforeces 707B Bakery(BFS)

    B. Bakery time limit per test 2 seconds memory limit per test 256 megabytes input standard input out ...

  5. Codeforces Round #368 (Div. 2) B. Bakery (模拟)

    Bakery 题目链接: http://codeforces.com/contest/707/problem/B Description Masha wants to open her own bak ...

  6. 第6本:《The Hunger Games》

    第6本:<The Hunger Games> 以前从未读过一本完整的英文小说,所有就在今年的读书目标中增加了一本英文小说,但在 头四个月内一直没有下定决定读哪一本.一次偶然从SUN的QQ空 ...

  7. Chris Richardson微服务翻译:微服务部署

    Chris Richardson 微服务系列翻译全7篇链接: 微服务介绍 构建微服务之使用API网关 构建微服务之微服务架构的进程通讯 微服务架构中的服务发现 微服务之事件驱动的数据管理 微服务部署( ...

  8. Codeforces 834D The Bakery【dp+线段树维护+lazy】

    D. The Bakery time limit per test:2.5 seconds memory limit per test:256 megabytes input:standard inp ...

  9. Codeforces Round #426 (Div. 1) B The Bakery (线段树+dp)

    B. The Bakery time limit per test 2.5 seconds memory limit per test 256 megabytes input standard inp ...

随机推荐

  1. Linux 进程间通信(二)(网络IPC:套接字)

    socket描述符 套接字是通信端点的抽象,创建一个套接字使用如下函数: #include <sys/socket.h> int socket(int domain, int type, ...

  2. Objective-c 单例模式

    用GCD写Objective-c的单例模式和C#有比较大的区别 声明h文件 #import <Foundation/Foundation.h> @interface me : NSObje ...

  3. 客户端-服务器通信安全 sign key

    API接口签名校验,如何安全保存appsecret? - 知乎  https://www.zhihu.com/question/40855191 要保证一般的客户端-服务器通信安全,可以使用3个密钥. ...

  4. docker openvas

    https://hub.docker.com/r/mikesplain/openvas/ Requirements DockerPorts available: 443, 9390, 9391 Usa ...

  5. 如何定义 match 常量?

    namespace MathConstants { const double E = 2.71828182845904523536; // e const double LOG2E = 1.44269 ...

  6. excel同时冻结首行和首列怎么操作

    之前ytkah只知道excel可以冻结首行或首列,但还不清楚如何同时冻结excel首行和首列,后面看到小C的报表,问了他才明白怎么操作. 首先,我们先把选中B2单元格,点击导航菜单的“视图” - “冻 ...

  7. 017-Spring Boot AOP

    一.概述 面向切面编程,将业务代码与处理琐碎相关度少的代码隔离开.以便达到重用,解耦. 用途:日志记录.权限处理.性能统计.监控.事务处理.异常处理等 通知类型有:前置通知.后置最终通知.后置返回通知 ...

  8. 《Python机器学习》笔记(六)

    模型评估与参数调优实战 基于流水线的工作流 一个方便使用的工具:scikit-learn中的Pipline类.它使得我们可以拟合出包含任意多个处理步骤的模型,并将模型用于新数据的预测. 加载威斯康星乳 ...

  9. tfboys——tensorflow模块学习(二)

    tf.contrib模块 tf.contrib 模块是一个比较复杂的模块. contrib细节: tf.contrib.bayesflow.entropy  香农信息论 tf.contrib.baye ...

  10. vim tabs

    :tabnew 增加一个标签:tabc 关闭当前的tab:tabo 关闭所有其他的tab:tabs 查看所有打开的tab:tabp 或gT 前一个:tabn 或gt 后一个:tabfirst或:tab ...