关闭

Codeforces Round #368 (Div. 2) 707B. Bakery

标签: codeforces
199人阅读 评论(0) 收藏 举报
分类:

B. Bakery
time limit per test2 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard output
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
Note

Image illustrates the first sample case. Cities with storage located in and the road representing the answer are darkened.

休息了几天,先做些水题。
枚举+二分

#include<stdio.h>
#include<iostream>
#include<algorithm>
#include<string>
#include<string.h>
using namespace std;
#define F(x,a,b) for (int x=a;x<=b;x++)
#define INF 1000000005
struct p{int a,b,v;friend bool operator < (const p &a,const p &b){return a.v<b.v;}}E[200005];
int f[200005],rec[200005];
int n,m,a,b,c,k;
bool _s(int x){int l=0,r=k;while (r-l>1){int mid=(l+r)>>1;if (rec[mid]>=x)r=mid;else l=mid;}if (rec[r]==x) return 1;else return 0;}
int main()
{
   scanf("%d%d%d",&n,&m,&k);
   F(i,1,m)
   {
       scanf("%d%d%d",&a,&b,&c);E[i].a=a;E[i].b=b;E[i].v=c;
   }
   if (k<=0) {printf("-1\n"); return 0;}
   F(i,1,k) scanf("%d",&rec[i]);
   sort(rec+1,rec+k+1);
   int minn=INF;
   F(i,1,m){if (_s(E[i].a)^_s(E[i].b))minn=E[i].v<minn?E[i].v:minn;}
   if (minn<INF)printf("%d",minn);else printf("-1");
}
0
0
查看评论

Codeforces Round #368 (Div. 2) D. Persistent Bookcase【可持久化线段树,区间取反

可持久化线段树,单点修改区间修改(取反)撤销操作,(伪)区间查询 反正……是个板儿 以前没有写过区间修改的,后来想起似乎pushdown的时候需要把儿子的儿子也接上去(反正就是儿子节点的信息要复制完整) 而且……在递归的时候(并没有写zkw的_(:зゝ∠)_),因为有pushdown存在所以不能无脑...
  • Flaze_
  • Flaze_
  • 2016-08-24 16:00
  • 221

Codeforces Round #258 (Div. 2)-(A,B,C,D,E)

A:Game With Sticks 水题。。。每次操作,都会拿走一个横行,一个竖行。 所以一共会操作min(横行,竖行)次。 #include #include #include #include #include #include #include #include #pragma commen...
  • rowanhaoa
  • rowanhaoa
  • 2014-07-25 14:39
  • 1486

【解题报告】Codeforces Round #401 (Div. 2)

简略的解题报告。
  • TRiddle
  • TRiddle
  • 2017-02-25 17:55
  • 417

Codeforces Round #460 (Div. 2) D. Substring(拓扑排序)

题目链接题意:有一个n个点m条边的有向图,每个节点有一个字母,路径的权值是路径上相同字母的最大个数。求最大的路径权值。思路:因为只有26个字母,所以直接假设其中一个字母为相同字母数最大的字母。枚举每一个字母,通过拓扑排序找到最大权值,取其中的最大值就是答案。当图中存在环时,答案就是无穷大,输出-1;...
  • cyf199775
  • cyf199775
  • 2018-02-01 13:55
  • 50

Codeforces Round #378 (Div. 2) E. Sleep in Class

链接:http://codeforces.com/contest/733/problem/E 题意:给一个只含'U'和'D'的字符串,U表示向右走,D表示向左走。每个格子踩一次之后翻转(U变D,D变U),求从每个格子出发走多少步才能走到位置小于1或大于n。 分析:...
  • Fsss_7
  • Fsss_7
  • 2016-11-15 13:48
  • 359

Codecraft-17 and Codeforces Round #391 (Div. 1 + Div. 2, combined)A+B

A. Gotta Catch Em’ All!time limit per test:1 secondmemory limit per test:256 megabytesinput:standard inputoutput:standard outputBash wants to become a...
  • qq_33183401
  • qq_33183401
  • 2017-01-16 17:12
  • 243

Codeforces Round #442 (Div. 2)

A. #include using namespace std; typedef long long ll; int main() { string s; cin >> s; vector v; v.push_back ("Danil&quo...
  • Egqawkq
  • Egqawkq
  • 2017-10-24 02:15
  • 242

Codeforces Round #428 (Div. 2):D. Winter is here(组合数公式)

D. Winter is here time limit per test 3 seconds memory limit per test 256 megabytes input standard input output standard output Winter is h...
  • Jaihk662
  • Jaihk662
  • 2017-08-14 14:52
  • 353

Codeforces Round #460 (Div. 2) D. Substring

题目链接:D. Substring 题目大意 一张有向图,每个结点上有一个字母,定义一条路径的权值为路径中出现次数最多的字母的出现次数,求图中权值最大的路径的权值,如果权值可以无限大,输出-1 思路 26次DFS,,每次只统计一个字母的权值,cnt[i]记录从i点开始的路径当前字母出...
  • litmxs
  • litmxs
  • 2018-01-31 23:25
  • 73

Codeforces Round #327 (div.2)(A B C D)

codeforces round #327 (div.2)
  • lincifer
  • lincifer
  • 2015-10-26 13:11
  • 1107
    个人资料
    • 访问:18712次
    • 积分:997
    • 等级:
    • 排名:千里之外
    • 原创:83篇
    • 转载:0篇
    • 译文:0篇
    • 评论:9条
    文章分类
    最新评论