【CF707B】Bakery(想法题)

题意:

有N个城市,M条无向边,其中有K个城市是仓库

现在要在非仓库的城市中选择一家开面包店,使得其最少与一个仓库联通,且到所有仓库距离的最小值最小

(1 ≤ n, m ≤ 10^5, 0 ≤ k ≤ n)

分析:

数据范围决定了只能使用O(N)或O(n log n)的解法

思考后可以发现面包店一定选在与某仓库直接相连的城市中

否则就要通过另外间接相连的城市走到,多走了若干条路

 1 var a,b,c,flag:array[1..100000]of longint;
 2     n,m,k,i,ans,x:longint;
 3 
 4 begin
 5  readln(n,m,k);
 6  for i:=1 to m do readln(a[i],b[i],c[i]);
 7  for i:=1 to k do
 8  begin
 9   read(x);
10   flag[x]:=1;
11  end;
12  ans:=maxlongint;
13  for i:=1 to m do
14   if flag[a[i]]+flag[b[i]]=1 then
15    if c[i]<ans then ans:=c[i];
16  if ans<maxlongint then writeln(ans)
17   else writeln(-1);
18 end.
原文地址:https://www.cnblogs.com/myx12345/p/5865861.html