92求助
查看原帖
92求助
225965
ztxtjz楼主2020/4/28 15:12
#include<bits/stdc++.h>
#define int long long
using namespace std;
const int MaxN=10010,MaxM=400010;
struct edge
{
    int to,next;
    double dis;
}e[MaxM],ee[MaxM];
int head[MaxN],headd[MaxN],cnt;
bool vis[MaxN];
int n,m,step;
double k,d[MaxN],dis[MaxN],ans;
int read()
{
	int x=0;char c=getchar();
	while(c>='0'&&c<='9') x=x*10+c-'0',c=getchar();
	return x;
}
inline void add(int u,int v,double d)
{
    e[++cnt].dis=d;
    e[cnt].to=v;
    e[cnt].next=head[u];
    head[u]=cnt;
}
inline void adde(int u,int v,double d)
{
    ee[cnt].dis=d;
    ee[cnt].to=v;
    ee[cnt].next=headd[u];
    headd[u]=cnt;
}
struct node
{
    double dis;
	int pos;
    bool operator <( const node &x )const
    {
        return x.dis<dis;
    }
};
priority_queue<node> q,p;
inline void a_star()
{
	p.push((node){dis[1],1});
	while(!p.empty())
	{
		node tmp=p.top();
		p.pop();
		int x=tmp.pos;
		d[x]=tmp.dis-dis[x];
		if(x==n)
		{
			if(ans+d[x]<=k) ans+=d[x],step++;
			else break;
		}
		if(x!=n)
		for(int i=head[x];i;i=e[i].next)
        {
            int y=e[i].to;
            p.push((node){dis[y]+d[x]+e[i].dis,y});
        }
	}
}
inline void dijkstra()
{
    dis[n]=0;
    q.push((node){0,n});
    while(!q.empty())
    {
        node tmp=q.top();
        q.pop();
        int x=tmp.pos;
        if(vis[x])
            continue;
        vis[x]=1;
        for(int i=headd[x];i;i=ee[i].next)
        {
            int y=ee[i].to;
            if(dis[y]>dis[x]+ee[i].dis)
            {
                dis[y]=dis[x]+ee[i].dis;
                if(!vis[y])
                {
                    q.push((node){dis[y],y});
                }
            }
        }
    }
}
signed main()
{
	//freopen("in.txt","r",stdin);
	scanf("%lld%lld%lf",&n,&m,&k);
	for(int i=1;i<=n;++i) dis[i]=1e9;
	for(register int i=1;i<=m;++i)
	{
	    int u,v;
	    double d;
	    scanf("%lld%lld%lf",&u,&v,&d);
		add(u,v,d);
		adde(v,u,d);
	}
	dijkstra();
	a_star();
	printf("%lld",step);
	return 0;
}
2020/4/28 15:12
加载中...