Codeforces Problem 707B Bakery(思维题)

本文解析了一道Codeforces竞赛题,目标是在无仓库的城市中选择最佳位置开设面包店,并确保能以最短路径连接至任一仓库,提供了解题思路及代码实现。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

此文章可以使用目录功能哟↑(点击上方[+])

比赛链接→Codeforces Round #368 (Div. 2)

 Codeforces Problem 707B Bakery

Accept: 0    Submit: 0
Time Limit: 2 seconds    Memory Limit : 256 megabytes

 Problem 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 n, m and k (1 ≤ n, m ≤ 10^5, 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 ≤ 10^9, 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.

 Sample Input

5 4 2
1 2 5
1 2 3
2 3 4
1 4 10
1 5
3 1 1
1 2 3
3

 Sample Output

3
-1

 Hint


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

 Problem Idea

解题思路:

【题意】
n个城市,m条双向路

n个城市中有k个城市中有仓库

现在要在一个没有仓库的城市开一家面包店,使得该面包店能够至少到达一个仓库,且到达该仓库的距离尽可能小

问最小距离为多少,若没有城市适合开设面包店,则输出"-1"


【类型】
思维题

【分析】
该开始觉得有点难以下手,因为既不知道应该以哪个城市作为起点,又不知道应该以哪个城市作为终点,且又不能用floyd暴力求解两两城市之间的距离

但是,仔细思考一下,你会发现,我们只需要考虑和仓库直接相连的城市就可以了,因为那些可达但和仓库没有直接相连的城市必定是经过了某个与仓库直接相连的城市,这样距离势必会比直接相连来得长,如图


城市1和城市5有仓库,那我们只需要考虑城市2和城市4,之所以不考虑城市3是因为仓库1到城市3必须要经过城市2,显然城市2比城市3离仓库1更近

于是,我们只要把有仓库的城市标记,如果某条双向路一端连着有仓库的城市,另一端连着没有仓库的城市,那就把这条路的长度取来作比较,留下距离最短的路即可

【时间复杂度&&优化】
O(n)

题目链接→Codeforces Problem 707B Bakery

 Source Code

/*Sherlock and Watson and Adler*/
#pragma comment(linker, "/STACK:1024000000,1024000000")
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<queue>
#include<stack>
#include<math.h>
#include<vector>
#include<map>
#include<set>
#include<cmath>
#include<complex>
#include<string>
#include<algorithm>
#include<iostream>
#define eps 1e-9
#define LL long long
#define PI acos(-1.0)
#define bitnum(a) __builtin_popcount(a)
using namespace std;
const int N = 100005;
const int M = 40;
const int inf = 1000000007;
const int mod = 1000000007;
bool vis[N];
int u[N],v[N],l[N];
int main()
{
    int n,m,k,i,a,ans=inf;
    scanf("%d%d%d",&n,&m,&k);
    for(i=0;i<m;i++)
        scanf("%d%d%d",&u[i],&v[i],&l[i]);
    for(i=0;i<k;i++)
    {
        scanf("%d",&a);
        vis[a]=true;
    }
    for(i=0;i<m;i++)
        if(vis[u[i]]&&!vis[v[i]]||!vis[u[i]]&&vis[v[i]])
            ans=min(ans,l[i]);
    if(ans!=inf)
        printf("%d\n",ans);
    else
        puts("-1");
    return 0;
}

菜鸟成长记

### Codeforces 思维思路和技巧 #### 预处理的重要性 对于许多竞赛编程问而言,预处理能够显著提高效率并简化后续操作。通过提前计算某些固定的数据结构或模式匹配表,可以在实际求解过程中节省大量时间。例如,在字符串处理类目中预先构建哈希表来加速查找过程[^1]。 #### 算法优化策略 针对特定类型的输入数据设计高效的解决方案至关重要。当面对大规模测试案例时,简单的暴力破解往往无法满足时限要求;此时则需考虑更高级别的算法改进措施,比如动态规划、贪心算法或是图论中的最短路径算法等。此外,合理利用空间换取时间也是一种常见的优化手段[^2]。 #### STL库的应用价值 C++标准模板库提供了丰富的容器类型(vector, deque)、关联式容器(set,map)以及各种迭代器支持,极大地便利了程序开发工作。熟练掌握这些工具不仅有助于快速实现功能模块,还能有效减少代码量从而降低出错几率。特别是在涉及频繁插入删除场景下,优先选用双向队列deque而非单向链表list可获得更好的性能表现。 ```cpp #include <iostream> #include <deque> using namespace std; int main(){ deque<int> dq; // 向两端添加元素 dq.push_back(5); dq.push_front(3); cout << "Front element is: " << dq.front() << endl; cout << "Back element is : " << dq.back() << endl; return 0; } ``` #### 实际应用实例分析 以一道具体目为例:给定一系列查询指令,分别表示往左端/右端插入数值或者是询问某个指定位置到边界之间的最小距离。此目的关键在于如何高效地追踪最状态而无需重复更整个数组。采用双指针技术配合静态分配的一维数组即可轻松解决上述需求,同时保证O(n)级别的总运行成本[^4]。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值