题目链接:http://icpc.njust.edu.cn/Problem/Hdu/2066/
SPFA可以高效过,代码如下:
1 #include<bits/stdc++.h> 2 using namespace std; 3 typedef unsigned int ui; 4 typedef long long ll; 5 typedef unsigned long long ull; 6 #define pf printf 7 #define mem(a,b) memset(a,b,sizeof(a)) 8 #define prime1 1e9+7 9 #define prime2 1e9+9 10 #define pi 3.14159265 11 #define lson l,mid,rt<<1 12 #define rson mid+1,r,rt<<1|1 13 #define scand(x) scanf("%llf",&x) 14 #define f(i,a,b) for(int i=a;i<=b;i++) 15 #define scan(a) scanf("%d",&a) 16 #define mp(a,b) make_pair((a),(b)) 17 #define P pair<int,int> 18 #define dbg(args) cout<<#args<<":"<<args<<endl; 19 #define inf 0x3f3f3f3f 20 const int maxn=1e4; 21 int n,m,t,e; 22 int head[maxn],in[maxn],nxt[maxn],d[maxn],a[maxn],ans,b; 23 bool vis[maxn]; 24 struct edge{ 25 int v,w; 26 }p[maxn]; 27 void init() 28 { 29 e=0; 30 mem(head,-1); 31 mem(nxt,-1); 32 mem(vis,0); 33 } 34 void addedge(int u,int v,int w) 35 { 36 p[e].v=v; 37 p[e].w=w; 38 nxt[e]=head[u]; 39 head[u]=e++; 40 } 41 void SPFA(int src) 42 { 43 mem(in,0); 44 d[src]=0; 45 queue<int>q; 46 q.push(src); 47 in[src]=1; 48 while(!q.empty()) 49 { 50 int now=q.front(); 51 q.pop(); 52 in[now]=0; 53 for(int i=head[now];~i;i=nxt[i]) 54 { 55 if(d[p[i].v]>d[now]+p[i].w) 56 { 57 d[p[i].v]=d[now]+p[i].w; 58 if(vis[p[i].v])ans=min(ans,d[p[i].v]); 59 if(!in[p[i].v]) 60 { 61 in[p[i].v]=1; 62 q.push(p[i].v); 63 } 64 } 65 } 66 } 67 } 68 int main() 69 { 70 //freopen("input.txt","r",stdin); 71 //freopen("output.txt","w",stdout); 72 std::ios::sync_with_stdio(false); 73 while(scanf("%d%d%d",&n,&m,&t)!=EOF) 74 { 75 init(); 76 ans=inf; 77 int u,v,w; 78 f(i,1,n) 79 { 80 scanf("%d%d%d",&u,&v,&w); 81 addedge(u,v,w); 82 addedge(v,u,w); 83 } 84 f(i,1,m)scan(a[i]); 85 f(i,1,t) 86 { 87 scan(b); 88 vis[b]=1; 89 } 90 f(i,1,m) 91 { 92 SPFA(a[i]); 93 } 94 pf("%d ",ans); 95 } 96 }