/*c++ 62ms
*g++ 78ms
*spfa+暴力
*/
#include <bits/stdc++.h>
#define MEM(a,x) memset(a , x , sizeof(a))
using namespace std;
typedef int weight_t;
const int SIZE_E = 1000000;
const int SIZE_V = 1050;
struct edge_t{
int v;
weight_t w;
edge_t *next;
}Edge[SIZE_E];
edge_t *V[SIZE_V];
int n;
int ECnt = 0;
void mkEdge(int const &a,int const &b,weight_t const &w){
Edge[ECnt].v = b;
Edge[ECnt].w = w;
Edge[ECnt].next = V[a];
V[a] = Edge+ECnt++;
Edge[ECnt].v = a;
Edge[ECnt].w = w;
Edge[ECnt].next = V[b];
V[b] = Edge+ECnt++;
}
weight_t D[SIZE_V];
bool flag[SIZE_V];
void spfa(int s){
MEM(flag,0);
fill(D,D+SIZE_V,INT_MAX);
D[s] = 0;
flag[s] = true;
queue<int>q;
q.push(s);
while(!q.empty()){
int u = q.front();
q.pop();
flag[u] = false;
for(edge_t *p = V[u];p;p = p->next){
int v = p->v;
weight_t temp = D[u] + p->w;
if (temp < D[v]){
D[v] = temp;
if (!flag[v]){
flag[v] = true;
q.push(v);
}
}
}
}
}
void init(){
ECnt = 0;
memset(V,0,sizeof(V));
MEM(Edge,0);
}
int liveWith[SIZE_V];
int WantTo[SIZE_V];
int main(){
int T,S,d;
while(scanf("%d%d%d",&T,&S,&d)!=EOF){
init();
int a,b;
weight_t time;
for (int i = 0;i < T;++i){
scanf("%d%d%d",&a,&b,&time);
mkEdge(a,b,time);
}
for (int i = 0;i < S;++i)
scanf("%d",liveWith+i);
for (int i = 0;i < d;++i)
scanf("%d",WantTo+i);
int ans = INT_MAX;
//直接爆搜所有情况
for (int i = 0;i < S;++i){
for (int j = 0;j < d;++j){
spfa(liveWith[i]);
ans = min(ans,D[WantTo[j]]);
}
}
printf("%d\n",ans);
}
return 0;
}
spfa hdu 2066 一个人的旅行
最新推荐文章于 2021-04-18 18:48:29 发布