[A* k短路] BZOJ 1975 [Sdoi2010]魔法猪学院

就是求前k短路的权值和<=E 

据说卡STL的priority_queue 那么我用STL的heap好了


#include<cstdio>
#include<cstdlib>
#include<algorithm>
#include<cstring>
#define cl(x) memset(x,0,sizeof(x))
#define V G[p].v
using namespace std;

const int M=200005;
const int N=5005;

struct edge{
	int u,v; double w;
	int next;
};

edge G[M<<1];
int head[N],inum;

inline void add(int u,int v,double w,int p){
	G[p].u=u; G[p].v=v; G[p].w=w; G[p].next=head[u]; head[u]=p;
}

const int ND=1000005;
int Q[ND],l,r;
int ins[N];
double dis[N];

int n,m,ans; double E;
int iu[M],iv[M]; double iw[M];

inline void SPFA(int S){
	int u;
	for (int i=1;i<=n;i++) dis[i]=1e130,ins[i]=0;
	l=r=-1;
	dis[S]=0; Q[(++r)%ND]=S; ins[S]=1;
	while (l!=r){
		u=Q[(++l)%ND]; ins[u]=0;
		for (int p=head[u];p;p=G[p].next)
			if (dis[V]>dis[u]+G[p].w)
			{
				dis[V]=dis[u]+G[p].w;
				if (!ins[V])
					Q[(++r)%ND]=V,ins[V]=1;
			}
	}
}

struct abcd{
	int u;
	double g,f;
	abcd(int u=0,double g=0,double f=0):u(u),g(g),f(f) { }
	bool operator < (const abcd &B) const{
		return f>B.f;
	}
}H[2000005];
int end;

inline void push(abcd x){
	H[++end]=x; push_heap(H+1,H+end+1);
}

inline abcd pop(){
	pop_heap(H+1,H+end+1);
	return H[end--];
}

inline void A_star(){
	abcd now;
	push(abcd(1,0,dis[1]));
	while (end){
		now=pop();
		for (int p=head[now.u];p;p=G[p].next)
			push(abcd(V,now.g+G[p].w,now.g+G[p].w+dis[V]));
		if (now.u==n){
			E-=now.f;
			if (E<0) return;
			ans++;
		}
	}
}

int main()
{
	freopen("t.in","r",stdin);
	freopen("t.out","w",stdout);
	scanf("%d%d%lf",&n,&m,&E);
	for (int i=1;i<=m;i++)
		scanf("%d%d%lf",iu+i,iv+i,iw+i),add(iv[i],iu[i],iw[i],++inum);
	SPFA(n);
	inum=0; cl(head); cl(G);
	for (int i=1;i<=m;i++) add(iu[i],iv[i],iw[i],++inum);
	A_star();
	printf("%d\n",ans);
	return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值