bzoj1681[Usaco2005 Mar]Checking an Alibi 不在场的证明*

bzoj1681[Usaco2005 Mar]Checking an Alibi 不在场的证明

题意:

给个点集,求无向有权图中点集里的哪些点到点1的距离小于等于M。点集内点数≤100,图中点数≤500,边数≤1000。

题解:

spfa。

代码:

 1 #include <cstdio>
 2 #include <cstring>
 3 #include <algorithm>
 4 #include <queue>
 5 #define inc(i,j,k) for(int i=j;i<=k;i++)
 6 #define maxn 510
 7 #define INF 0x3ffffff
 8 using namespace std;
 9 
10 inline int read(){
11     char ch=getchar(); int f=1,x=0;
12     while(ch<'0'||ch>'9'){if(ch=='-')f=-1; ch=getchar();}
13     while(ch>='0'&&ch<='9')x=x*10+ch-'0',ch=getchar();
14     return f*x;
15 }
16 struct e{int t,w,n;}; e es[maxn*4]; int g[maxn],ess;
17 void pe(int f,int t,int w){
18     es[++ess]=(e){t,w,g[f]}; g[f]=ess; es[++ess]=(e){f,w,g[t]}; g[t]=ess;
19 }
20 int d[maxn],f,p,c,m,cow[maxn],tot; bool inq[maxn];
21 queue <int> q;
22 void spfa(){
23     while(!q.empty())q.pop(); memset(inq,0,sizeof(inq)); inc(i,1,f)d[i]=INF;
24     q.push(1); inq[1]=1; d[1]=0;
25     while(!q.empty()){
26         int x=q.front(); q.pop(); inq[x]=0;
27         for(int i=g[x];i;i=es[i].n)if(d[x]+es[i].w<d[es[i].t]){
28             d[es[i].t]=d[x]+es[i].w;
29             if(!inq[es[i].t])inq[es[i].t]=1,q.push(es[i].t);
30         }
31     }
32 }
33 int main(){
34     f=read(); p=read(); c=read(); m=read(); inc(i,1,p){int x=read(),y=read(),z=read(); pe(x,y,z);}
35     inc(i,1,c)cow[i]=read(); spfa(); inc(i,1,c)if(d[cow[i]]<=m)tot++; printf("%d
",tot);
36     inc(i,1,c)if(d[cow[i]]<=m)printf("%d
",i); return 0;
37 }

20160803

原文地址:https://www.cnblogs.com/YuanZiming/p/5743967.html