codeforces707B:Bakery
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.
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.
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.
5 4 2
1 2 5
1 2 3
2 3 4
1 4 10
1 5
3
3 1 1
1 2 3
3
-1
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的更多相关文章
- java web 开发三剑客 -------电子书
Internet,人们通常称为因特网,是当今世界上覆盖面最大和应用最广泛的网络.根据英语构词法,Internet是Inter + net,Inter-作为前缀在英语中表示“在一起,交互”,由此可知In ...
- 所有selenium相关的库
通过爬虫 获取 官方文档库 如果想获取 相应的库 修改对应配置即可 代码如下 from urllib.parse import urljoin import requests from lxml im ...
- 读书笔记2014第6本:《The Hunger Games》
以前从未读过一本完整的英文小说,所有就在今年的读书目标中增加了一本英文小说,但在头四个月内一直没有下定决定读哪一本.一次偶然从SUN的QQ空间中看到Mockingjay,说是不错的英文小说,好像已经是 ...
- Codeforeces 707B Bakery(BFS)
B. Bakery time limit per test 2 seconds memory limit per test 256 megabytes input standard input out ...
- Codeforces Round #368 (Div. 2) B. Bakery (模拟)
Bakery 题目链接: http://codeforces.com/contest/707/problem/B Description Masha wants to open her own bak ...
- 第6本:《The Hunger Games》
第6本:<The Hunger Games> 以前从未读过一本完整的英文小说,所有就在今年的读书目标中增加了一本英文小说,但在 头四个月内一直没有下定决定读哪一本.一次偶然从SUN的QQ空 ...
- Chris Richardson微服务翻译:微服务部署
Chris Richardson 微服务系列翻译全7篇链接: 微服务介绍 构建微服务之使用API网关 构建微服务之微服务架构的进程通讯 微服务架构中的服务发现 微服务之事件驱动的数据管理 微服务部署( ...
- 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 ...
- 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 ...
随机推荐
- ACM至大二省赛小结
大一进acm坑的,大一上就学了个c,下才学c++,不过 c 学完后学 c++ 感觉很简单,应该是大一下开学左右才开始刷题的,前面都在水???然后因为acm协会有各种月赛校赛什么的,班主任的提醒较多,所 ...
- VS2012如何显示行号
Tools-Options-Text Editor-All Languages –General – Display
- 阿里云服务器 进行zookeeper集群时候的肯!
首先我本地虚拟机 部署过N次没什么问题! 擦,上了云就启动后不能正常节点之间连接和发现! 这上面有手误 擦,吧编号的2 打成22 了,这个看下日志就解决了 重点是zoo.conf 中的IP进行配置时候 ...
- Python 通过字符串调用函数、接近属性
需求:传入的是函数名.属性名,想通过字符串调用函数,接近属性. 通过字符串接近.变动属性 变量:model_name, field_name # 获取 model model = AppConfig. ...
- TFS二次开发-基线文件管理器(4)-标签的创建
上一节已经完成了源码文件的读取,如果要将已经选择的文件保存为一个标签(Lable).在VS我们只能对一个目录做标签,非常的不方便.如果用下面的方法,将选择的文件路径保存为一个List在打标签,就非常的 ...
- 面向对象、接口编程的重要性 python 为什么引入接口interface
面向对象编程的实践 有个产品,其有10个子产品,现在要统计每日消费数据其中8个子产品的消费入账金额算法相同,2个不同; 拓展性差的糟糕的代码 def ConsumptionSum(product): ...
- 【python】-- Django ORM(基础)
Django ORM(基础) Django 框架十分强大,自带数据库操作功能.Django 跟 SQLAchemy 一样,也是通过ORM(Object Relational Mapping,关系对象映 ...
- Java输入输出重定向代码
try { BufferedInputStream in = new BufferedInputStream(new FileInputStream("input.txt")) ...
- 教你使用SQL数据库索引(1-15)
原文地址:http://www.sqlservercentral.com/stairway/72399/ 中文地址:https://www.cnblogs.com/tjy9999/category/4 ...
- 003-ARP地址解析协议
一.概念 地址解析协议,即ARP(Address Resolution Protocol),是根据IP地址获取物理地址的一个TCP/IP协议.主机发送信息时将包含目标IP地址的ARP请求广播到网络上的 ...