【题意】
n个城市,m条双向路
n个城市中有k个城市中有仓库
现在要在一个没有仓库的城市开一家面包店,使得该面包店能够至少到达一个仓库,且到达该仓库的距离尽可能小
问最小距离为多少,若没有城市适合开设面包店,则输出"-1"
看懂题意就能做出来了,一条变的两端必须一端是没有面粉仓库的城市,一边是有面粉仓库的城市,然后比较满足这个条件的边的大小,选出最小的边,把值打印出来就行了,就是简单的贪心
AC代码:
#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cstring>
using namespace std;
const int maxm = 100002;
const int maxn = 100002;
const int inf = 0x3f3f3f3f;
int n, m, k;
struct node {
int x, y, w;
};
node edge[maxm*2];
int book[maxn];
int main()
{
while(~scanf("%d%d%d", &n, &m, &k)) {
memset(book, 0, sizeof(book));
int i;
for(i=0; i<m; ++i)
scanf("%d%d%d", &edge[i].x, &edge[i].y, &edge[i].w);
if(!k) {
printf("-1\n");
continue;
}
int a;
for(i=0; i<k; ++i) {
scanf("%d", &a);
book[a] = 1;
}
int ans = inf;
for(i=0; i<m; ++i) {
if(book[edge[i].x] + book[edge[i].y] == 1) {
ans = min(ans, edge[i].w);
}
}
if(ans == inf)
printf("-1\n");
else printf("%d\n", ans);
}
}