题目链接:点击打开链接
题意:n个城市,其中k个城市开了面粉店,某人想在剩余的n - k个城市中找一个距离面粉店(任意面粉店)最近的城市开面包店,问最短路径为多少?
思路:思维题,面包店一定与面粉店直接相连,所以直接处理每条路径即可,每次贪心的选取一条最短路径,判断相连的两个城市是否只有一个开了面粉店。具体实现,我把边的信息存在优先队列了;或者直接存储每条边的信息,线性扫一遍。
// CodeForces 707B Bakery 运行/限制:77ms/2000ms
#include <cstdio>
#include <cstring>
#include <iostream>
#include <queue>
using namespace std;
#define INF 0x3f3f3f3f
struct node {
int u, v, l;
node(int a,int b,int c):u(a),v(b),l(c){}
bool operator<(const node &b) const{
return this->l > b.l;
}
};
int stor[100005];//标记storage
priority_queue<node> q;
int main(){
int n, m, k;
int a, b, c;
int ans;
while (scanf("%d%d%d", &n, &m, &k) != EOF) {
ans = INF;
memset(stor, 0, sizeof(stor));
for (int i = 0; i < m; i++) {
scanf("%d%d%d", &a, &b, &c);
q.push(node(a, b, c));
}
for (int i = 0; i < k; i++) {
scanf("%d", &a);
stor[a] = 1;
}
while (!q.empty()) {
node t = q.top();
q.pop();
if (stor[t.u] != stor[t.v]) {//0 1 or 1 0即非storage和storage
ans = t.l;
break;
}
}
if (ans != INF) printf("%d\n", ans);
else printf("-1\n");
while (!q.empty()) q.pop();
}
return 0;
}