频道栏目
首页 > 资讯 > 其他 > 正文

编程开发练习题_空间传送装置 spfa

17-09-23        来源:[db:作者]  
收藏   我要投稿

空间传送装置 spfa

太空中一共有n座星球,它们之间可以通过空间传送装置进行转移。空间传送装置分为m种,第i种装置可以用4个参数a_i,b_i,c_i,d_i来描述。因为时空抖动的问题,在非整数时刻禁止使用空间传送装置。如果在整数s时刻使用装置,那么需要花费((a_i*s+b_i) mod c_i)+d_i单位时间才能完成传送。现在是s时刻,小Q位于1号星球,请写一个程序计算从1号星球到每个星球最少需要的时间。
2<=n<=100000,1<=m<=50,1<=s<=2000,1<=e<=200000,分别表示星球的个数、空间传送装置的种类数、当前的时间以及空间传送装置的个数。
1<=a_i,b_i,c_i,d_i<=2000

分析

注意到即使有时空抖动,每个点仍然是越早到越好。那么我们预处理出模c意义下每种取值的最佳等待时间,然后直接跑spfa就好了。

代码

#include
#include
#include
#include
#include
#include
using namespace std;

typedef long long LL;

const int N=100005;
const LL inf=(LL)1e15;

int n,m,cnt,s,t,tot[55],last[N],trans[55][2005];
bool vis[N];
queue que;
struct edge{int to,next,op;}e[N*2];
struct data{int a,b,c,d;}tr[55];
LL dis[N];

int read()
{
    int x=0,f=1;char ch=getchar();
    while (ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
    while (ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
    return x*f;
}

void addedge(int u,int v,int op)
{
    e[++cnt].to=v;e[cnt].op=op;e[cnt].next=last[u];last[u]=cnt;
}

void spfa()
{
    for (int i=1;i<=n;i++) dis[i]=inf;
    dis[1]=s;que.push(1);vis[1]=1;
    while (!que.empty())
    {
        int u=que.front();que.pop();
        for (int i=last[u];i;i=e[i].next)
            if (dis[u]+trans[e[i].op][(dis[u]-1)%tot[e[i].op]+1]=1;j--)
        {
            int now=(pos+j-1)%tot[i]+1;
            trans[i][now]=min(trans[i][now%tot[i]+1]+1,(tr[i].a*now+tr[i].b)%tr[i].c+tr[i].d);
        }
    }
    spfa();
    for (int i=2;i<=n;i++)
        if (dis[i]==inf) puts("-1");
        else printf("%lld\n",dis[i]-s);
    return 0;
}
相关TAG标签
上一篇:vue项目优化之页面的按需加载
下一篇:CSS块级元素/内联元素、继承与相关属性
相关文章
图文推荐

关于我们 | 联系我们 | 广告服务 | 投资合作 | 版权申明 | 在线帮助 | 网站地图 | 作品发布 | Vip技术培训 | 举报中心

版权所有: 红黑联盟--致力于做实用的IT技术学习网站