Codeforeces 707B Bakery(BFS)
2 seconds
256 megabytes
standard input
standard 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 onlyk 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.
题目链接:CF 707B
多起点多终点求其中与终点相连的所有最短路中的最短长度,感觉跟优先队列有关,然后设一开始的所有面粉点即不能达到的d为0,其他都设为INF,把所有的面粉点压入队列进行BFS,最后选出1~n点中不为0的最短距离……一开始没考虑重边而且面粉店的d设的不对,WA两发……看了下其他人写的好像直接暴力比较(因为最短肯定是直接相连)而且速度跟BFS一样,我的做法比较麻烦……
代码:
#include<iostream>
#include<algorithm>
#include<cstdlib>
#include<sstream>
#include<cstring>
#include<bitset>
#include<cstdio>
#include<string>
#include<deque>
#include<stack>
#include<cmath>
#include<queue>
#include<set>
#include<map>
using namespace std;
#define INF 0x3f3f3f3f
#define CLR(x,y) memset(x,y,sizeof(x))
#define LC(x) (x<<1)
#define RC(x) ((x<<1)+1)
#define MID(x,y) ((x+y)>>1)
typedef pair<int,int> pii;
typedef long long LL;
const double PI=acos(-1.0);
const int N=100010;
struct info
{
int to;
int pre;
LL dx;
};
struct A
{
int cur;
LL dx;
bool operator<(const A &t)const
{
return dx>t.dx;
}
};
A pos[N];
info E[N<<1];
int head[N<<1],ne;
LL d[N];
priority_queue<A>Q;
void init()
{
CLR(head,-1);
ne=0;
CLR(d,INF);
while (!Q.empty())
Q.pop();
}
void add(int s,int t,LL dx)
{
E[ne].to=t;
E[ne].dx=dx;
E[ne].pre=head[s];
head[s]=ne++;
}
void bfs(int k)
{
int i;
for (i=0; i<k; ++i)
{
Q.push(pos[i]);
d[pos[i].cur]=0;
}
while (!Q.empty())
{
A now=Q.top();
Q.pop();
for (i=head[now.cur]; ~i; i=E[i].pre)
{
A v=now;
v.cur=E[i].to;
v.dx+=E[i].dx;
if(d[v.cur]>v.dx)
{
d[v.cur]=v.dx;
Q.push(v);
}
}
}
}
int main(void)
{
int n,m,k,i,j,a,b,temp;
LL dx;
while (~scanf("%d%d%d",&n,&m,&k))
{
init();
for (i=0; i<m; ++i)
{
scanf("%d%d%I64d",&a,&b,&dx);
add(a,b,dx);
add(b,a,dx);
}
for (i=0; i<k; ++i)
{
scanf("%d",&temp);
pos[i].cur=temp;
pos[i].dx=0;
}
bfs(k);
LL ans=d[0];
for (i=1; i<=n; ++i)
{
if(d[i]&&d[i]<ans)
ans=d[i];
}
printf("%I64d\n",ans!=d[0]?ans:-1LL);
}
return 0;
}
Codeforeces 707B Bakery(BFS)的更多相关文章
- 深搜(DFS)广搜(BFS)详解
图的深搜与广搜 一.介绍: p { margin-bottom: 0.25cm; direction: ltr; line-height: 120%; text-align: justify; orp ...
- 【算法导论】图的广度优先搜索遍历(BFS)
图的存储方法:邻接矩阵.邻接表 例如:有一个图如下所示(该图也作为程序的实例): 则上图用邻接矩阵可以表示为: 用邻接表可以表示如下: 邻接矩阵可以很容易的用二维数组表示,下面主要看看怎样构成邻接表: ...
- 深度优先搜索(DFS)与广度优先搜索(BFS)的Java实现
1.基础部分 在图中实现最基本的操作之一就是搜索从一个指定顶点可以到达哪些顶点,比如从武汉出发的高铁可以到达哪些城市,一些城市可以直达,一些城市不能直达.现在有一份全国高铁模拟图,要从某个城市(顶点) ...
- 【BZOJ5492】[HNOI2019]校园旅行(bfs)
[HNOI2019]校园旅行(bfs) 题面 洛谷 题解 首先考虑暴力做法怎么做. 把所有可行的二元组全部丢进队列里,每次两个点分别向两侧拓展一个同色点,然后更新可行的情况. 这样子的复杂度是\(O( ...
- 深度优先搜索(DFS)和广度优先搜索(BFS)
深度优先搜索(DFS) 广度优先搜索(BFS) 1.介绍 广度优先搜索(BFS)是图的另一种遍历方式,与DFS相对,是以广度优先进行搜索.简言之就是先访问图的顶点,然后广度优先访问其邻接点,然后再依次 ...
- 图的 储存 深度优先(DFS)广度优先(BFS)遍历
图遍历的概念: 从图中某顶点出发访遍图中每个顶点,且每个顶点仅访问一次,此过程称为图的遍历(Traversing Graph).图的遍历算法是求解图的连通性问题.拓扑排序和求关键路径等算法的基础.图的 ...
- 数据结构与算法之PHP用邻接表、邻接矩阵实现图的广度优先遍历(BFS)
一.基本思想 1)从图中的某个顶点V出发访问并记录: 2)依次访问V的所有邻接顶点: 3)分别从这些邻接点出发,依次访问它们的未被访问过的邻接点,直到图中所有已被访问过的顶点的邻接点都被访问到. 4) ...
- 层层递进——宽度优先搜索(BFS)
问题引入 我们接着上次“解救小哈”的问题继续探索,不过这次是用宽度优先搜索(BFS). 注:问题来源可以点击这里 http://www.cnblogs.com/OctoptusLian/p/74296 ...
- HDU.2612 Find a way (BFS)
HDU.2612 Find a way (BFS) 题意分析 圣诞节要到了,坤神和瑞瑞这对基佬想一起去召唤师大峡谷开开车.百度地图一下,发现周围的召唤师大峡谷还不少,这对基佬纠结着,该去哪一个...坤 ...
随机推荐
- Java性能优化权威指南-读书笔记(四)-JVM性能调优-延迟
延迟指服务器处理一个请求所花费的时间,单位一般是ms.s. 本文主要讲降低延迟可以做的服务器端JVM优化. JVM延迟优化 新生代 新生代大小决定了应用平均延迟 如果平均Minor GC持续时间大于应 ...
- SQLite常用网址
英文版SQLite官网: http://www.sqlite.org/rescode.html中文版SQLite官网:http://www.helplib.net/s/sqlite/9/167.sht ...
- 什么才是程序员的核心竞争力?zz
原文出处: 知乎 姚冬的观点 学习能力,尤其是自学能力,你啥时看到那些有名的程序高手在论坛上问“学习 XX 该看什么书,如何快速学习 XXX,学习 XXX 有什么代码推荐”之类的问题,他们想学什么很快 ...
- 【转】reduce端缓存数据过多出现FGC,导致reduce生成的数据无法写到hdfs
转自 http://blog.csdn.net/bigdatahappy/article/details/41726389 转这个目的,是因为该贴子中调优思路不错,值得学习 搜索推荐有一个job,1 ...
- [转载]“浅拷贝”与“深拷贝”
对于普通类型的对象来说,它们之间的复制是很简单的,例如: int a=88; int b=a; 而类对象与普通对象不同,类对象内部结构一般较为复杂,存在各种成员变量. C++中对象的复制就如同&quo ...
- python 类访问控制
访问限制 我们可以给一个实例绑定很多属性,如果有些属性不希望被外部访问到怎么办? Python对属性权限的控制是通过属性名来实现的,如果一个属性由双下划线开头(__),该属性就无法被外部访问.看例子: ...
- 深入理解DIP、IoC、DI以及IoC容器(转)
深入理解DIP.IoC.DI以及IoC容器 摘要 面向对象设计(OOD)有助于我们开发出高性能.易扩展以及易复用的程序.其中,OOD有一个重要的思想那就是依赖倒置原则(DIP),并由此引申出IoC.D ...
- node相关--socket.io
使用Socket.IO可以避免webSocket产生的问题: 传输: Socket.IO中消息的传递是基于传输的,而非全部依靠WebSocket; 它总会尝试选择对用户来说速度最快.对服务器性能来说最 ...
- 1076 K尾相等数
时间限制:500MS 内存限制:65536K提交次数:251 通过次数:80 题型: 编程题 语言: C++;C Description 从键盘输入一个自然数K(99999999>K> ...
- DataMember IsRequired属性
1.简介 在数据契约中,如果需要序列化时,则需要传入指定IsRequired属性: 摘要: 获取或设置一个值,该值用于指示序列化引擎在读取或反序列化时成员必须存在. public bo ...