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

本文介绍了解决BZOJ1681[Usaco2005Mar]CheckinganAlibi问题的方法。该问题要求找出在无向有权图中,从起点1出发距离不超过M的所有特定集合中的点。使用SPFA算法进行最短路径计算,通过实例代码展示了算法的具体实现。

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\n",tot);
36     inc(i,1,c)if(d[cow[i]]<=m)printf("%d\n",i); return 0;
37 }

 

20160803

转载于:https://www.cnblogs.com/YuanZiming/p/5743967.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值