思路:显然是直接连着仓库的点是最优解,枚举即可
#include<bits/stdc++.h>
using namespace std;
#define LL long long
const int maxn = 1e5+7;
int a[maxn];
int vis[maxn];
vector<pair<int,LL> >e[maxn];
int main()
{
int n,m,k;
LL ans = 1e18;
scanf("%d%d%d",&n,&m,&k);
for(int i = 1;i<=m;i++)
{
int u,v;LL w;
scanf("%d%d%lld",&u,&v,&w);
e[u].push_back(make_pair(v,w));
e[v].push_back(make_pair(u,w));
}
if(!k)
{
puts("-1");
return 0;
}
else if(k==n)
{
puts("-1");
return 0;
}
for(int i = 1;i<=k;i++)
scanf("%d",&a[i]),vis[a[i]]=1;
for(int u = 1;u<=n;u++)
{
if(vis[u])continue;
for(int i = 0;i<e[u].size();i++)
{
int v = e[u][i].first;
LL w = e[u][i].second;
if(vis[v])
ans = min(ans,w);
}
}
if(ans==1e18)
puts("-1");
else
printf("%lld\n",ans);
}
B. Bakery
time limit per test
memory limit per test
input
output
n cities numbered from 1 to n. There are m
k storages, located in different cities numbered a1, a2, ..., ak.
n - k cities, and, of course, flour delivery should be paid — for every kilometer of path between storage and bakery Masha should pay 1
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
k
Input
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.
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
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.
n cities, print - 1
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.