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

2023-07-11,,

Bakery

题目链接:

http://codeforces.com/contest/707/problem/B

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.

</big>

##Input
<big>

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.

</big>

##Output
<big>

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.

</big>

##Examples
<big>
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
</big> ##Source
<big>
Codeforces Round #368 (Div. 2)
</big> <br/>
##题意:
<big>
有n个城市,其中k个地方是仓库,现在要选一个没有仓库的城市建一个商店,使得其离任一商店最近.
</big> <br/>
##题解:
<big>
把所有直接连接仓库和其他城市的边拿出来比较一下即可.
</big> <br/>
##代码:
``` cpp
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <queue>
#include <map>
#include <set>
#include <stack>
#include <vector>
#include <list>
#define LL long long
#define eps 1e-8
#define maxn 101000
#define mod 100000007
#define inf 0x3f3f3f3f
#define mid(a,b) ((a+b)>>1)
#define IN freopen("in.txt","r",stdin);
using namespace std; int n,m,k;
typedef pair<int,int> pii;
vector<pii> g[maxn];
bool store[maxn]; int main(int argc, char const *argv[])
{
//IN; while(scanf("%d %d %d", &n,&m,&k) != EOF)
{
for(int i=1; i<=n; i++) g[i].clear();
memset(store, 0, sizeof(store)); while(m--) {
int u,v,w; scanf("%d %d %d", &u,&v,&w);
g[u].push_back(make_pair(v,w));
g[v].push_back(make_pair(u,w));
}
while(k--) {
int x; scanf("%d", &x);
store[x] = 1;
} int ans = inf;
for(int i=1; i<=n; i++) if(store[i]) {
int sz = g[i].size();
for(int j=0; j<sz; j++) if(!store[g[i][j].first]){
ans = min(ans, g[i][j].second);
}
} if(ans == inf) ans = -1;
printf("%d\n", ans);
} return 0;
}

Codeforces Round #368 (Div. 2) B. Bakery (模拟)的相关教程结束。

《Codeforces Round #368 (Div. 2) B. Bakery (模拟).doc》

下载本文的Word格式文档,以方便收藏与打印。

  • [考试反思]0801NOIP模拟测试11
    [考试反思]0801NOIP模拟测试11

    8月开门红。 放假回来果然像是神志不清一样。 但还是要接受这个事实。 嗯,说好听点,并列rank#7。 说难听点,垃圾rank#18。 都不用粘人名就知道我是哪一个吧。。。 因为图片不能太长,所以就不截图了。 你是...

    2023-07-30编程代码,,
  • 【2020.12.02提高组模拟】A组反思
    【2020.12.02提高组模拟】A组反思

    55,rk47 T1 赛时先想了\(trie\),想到不一定是前缀,然后就放弃转为打暴力 得分:\(RE22\) 正解是只用判断\(i\)与\(i+1\)的关系,那么只有两种情况,判断一下然后\(dp\)就可以了 T2 赛时直接暴力了,枚举当前时刻...

    2023-07-30编程代码,,
  • 2019.8.1 NOIP模拟测试11 反思总结
    2019.8.1 NOIP模拟测试11 反思总结

    延迟了一天来补一个反思总结 急匆匆赶回来考试,我们这边大家的状态都稍微有一点差,不过最后的成绩总体来看好像还不错XD 其实这次拿分的大都是暴力【?】,除了某些专注于某道题的人以及远程爆踩我们的某学车神...

    2023-07-30编程代码,,
  • [NOI.AC省选模拟赛3.23] 集合 [数学]
    [NOI.AC省选模拟赛3.23] 集合 [数学]

    题面 传送门 一句话题意: 给定$n\leq 1e9,k\leq 1e7,T\leq 1e9$ 设全集$U=\lbrace 1,2,3,...n\rbrace $,求$(min_{x\in S}\lbrace S\rbrace (S\subseteq U, \lvert S \rvert =k))^T$的期望 重要思想 注意,在遇...

    2023-07-29编程代码,,
  • [NOI.AC省选模拟赛3.23] 染色 [点分治+BFS序]
    [NOI.AC省选模拟赛3.23] 染色 [点分治+BFS序]

    题面 传送门 重要思想 真的是没想到,我很久以来一直以为总会有应用的$BFS$序,最终居然是以这种方式出现在题目中 笔记:$BFS$序可以用来处理限制点对距离的题目(综合点分树使用) 思路 本题中首先询问可以拆成...

    2023-07-29编程代码,,
  • [NOI.AC省选模拟赛3.30] Mas的童年 [二进制乱搞]
    [NOI.AC省选模拟赛3.30] Mas的童年 [二进制乱搞]

    题面 传送门 思路 这题其实蛮好想的......就是我考试的时候zz了,一直没有想到标记过的可以不再标记,总复杂度是$O(n)$ 首先我们求个前缀和,那么$ans_i=max(pre[j]+pre[i]$ $xor$ $pre[j])$ 考虑对于每个$pre[i]...

    2023-07-29编程代码,,
  • [CSP-S模拟测试]:Cover(单调栈++单调队列+DP)
    [CSP-S模拟测试]:Cover(单调栈++单调队列+DP)

    题目传送门(内部题126) 输入格式   第一行两个个整数$n,m$表示区间的长度与彩灯的数量。  接下来$m$行,每行三个整数$l_i,r_i,a_i$表示一条彩灯能够覆盖的区间以及它的美观程度。 输出格式   输出一行$...

    2023-07-29编程代码,,
  • CSP模拟赛游记
    CSP模拟赛游记

    时间:2019.10.5 考试时间:100分钟(连正式考试时间的一半还没有到)题目:由于某些原因不能公开。 由于第一次接触NOIinux系统所以连怎么建文件夹,调字体,如何编译都不知道,考试的前半小时全在摸索这些。 等...

    2023-07-29编程代码,,