CodeForces 707B Bakery(建图 + 枚举)

本文介绍了一个关于烘焙店选址的问题,通过寻找连接特殊点(面粉仓库所在城市)与非特殊点(可开设烘焙店的城市)之间的最短路径来决定最佳开店位置,并附带了解题思路及代码实现。

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

B. Bakery

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 nm and k (1 ≤ n, m ≤ 1050 ≤ 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 uv and l (1 ≤ u, v ≤ n1 ≤ l ≤ 109u ≠ 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
题目大意:给出一个无向图,图上有k个特殊的点,问是否有一条最短的边连接特殊的点与不特殊的点,若有,输出最短边的长度;否则输出-1。

解题思路:建图以后枚举每一条连接特殊点与非特殊点的边,然后不断更新最小值。(比赛时脑残的用了Dijkstra,好蠢啊,还被hack了,狂掉一波分。。菜鸡羞得无地自容)

代码如下:

#include <bits/stdc++.h>
#define INF INT_MAX
const int maxn = 1000005;
const int maxm = 1000005;
typedef long long ll;
typedef std::pair<ll,int> P;
int first[maxn],e;
int next[maxm],v[maxm],w[maxm];
int st[maxn];
ll d[maxn];
bool vis[maxn];
int n,m,k;
void init()
{
    memset(first,-1,sizeof(first));
    e = 1;
    memset(vis,0,sizeof(vis));
}

void add_edge(int from,int to,int cost)
{
    v[e] = to;
    next[e] = first[from];
    w[e] = cost;
    first[from] = e++;
}

int main()
{
    int a,b,c;
    while(scanf("%d %d %d",&n,&m,&k) != EOF){
        init();
        for(int i = 0;i < m;i++){
            scanf("%d %d %d",&a,&b,&c);
            add_edge(a,b,c);
            add_edge(b,a,c);
        }
        for(int i = 0;i < k;i++){
            scanf("%d",&st[i]);
            vis[st[i]] = 1;
        }
        ll ans = INF;
        for(int i = 0;i < k;i++){
            for(int j = first[st[i]];j != -1;j = next[j]){
                if(!vis[v[j]])
                    ans = std::min(ans,(ll)w[j]);
            }
        }
         printf("%I64d\n",ans == INF ?  -1 : ans);
    }
    return 0;
}


### 关于 Codeforces 1853B 的题解与实现 尽管当前未提供关于 Codeforces 1853B 的具体引用内容,但可以根据常见的竞赛编程问题模式以及相关算法知识来推测可能的解决方案。 #### 题目概述 通常情况下,Codeforces B 类题目涉及基础数据结构或简单算法的应用。假设该题目要求处理某种数组操作或者字符串匹配,则可以采用如下方法解决: #### 解决方案分析 如果题目涉及到数组查询或修改操作,一种常见的方式是利用前缀和技巧优化时间复杂度[^3]。例如,对于区间求和问题,可以通过预计算前缀和数组快速得到任意区间的总和。 以下是基于上述假设的一个 Python 实现示例: ```python def solve_1853B(): import sys input = sys.stdin.read data = input().split() n, q = map(int, data[0].split()) # 数组长度和询问次数 array = list(map(int, data[1].split())) # 初始数组 prefix_sum = [0] * (n + 1) for i in range(1, n + 1): prefix_sum[i] = prefix_sum[i - 1] + array[i - 1] results = [] for _ in range(q): l, r = map(int, data[2:].pop(0).split()) current_sum = prefix_sum[r] - prefix_sum[l - 1] results.append(current_sum % (10**9 + 7)) return results print(*solve_1853B(), sep='\n') ``` 此代码片段展示了如何通过构 `prefix_sum` 来高效响应多次区间求和请求,并对结果取模 \(10^9+7\) 输出[^4]。 #### 进一步扩展思考 当面对更复杂的约束条件时,动态规划或其他高级技术可能会被引入到解答之中。然而,在没有确切了解本题细节之前,以上仅作为通用策略分享给用户参考。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值