【codeforce 141D】Take-off Ramps 最短路问题

7 篇文章 0 订阅

其实本人觉得这道题最大的考点是题意(理解了半小时)外加各种坑,因为太过复杂,解释题意我想会写好多字就不写了,谷歌翻译。思路很简单,把每一个跳板的起点和终点作为一个中转点,离散化以后建边,首先和前后建边(直接划过去,所以是双向的),花费是路德长度,然后跳板前后建单向边,边权 为缓冲区 路径加给定时间。

坑:1.cf日常卡spfa,T在30多组,必须用dijkstra

         2.中间过程 爆long long

         3.区间越过起点和终点的边应当直接忽略

         4.输出方案的时候顺序为走的过去时候的顺序

上代码:

#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<queue>
#define maxn 100020
#include<queue>
#define MK make_pair
#define LL long long
using namespace std;
LL n,L;
LL cur[maxn*20],tot,ttot,head[maxn*10],cnt;
struct node{
    LL a,b,t,id;
}nod[maxn*2];
struct edge{
    LL v,next,w,flag;
}e[maxn*50];
void adde(LL a,LL b,LL c,LL d){e[ttot].v=b,e[ttot].next=head[a],e[ttot].w=c,e[ttot].flag=d;head[a]=ttot++;}
LL Q(LL x){return lower_bound(cur+1,cur+1+tot,x)-cur;}
LL d[maxn*10],vis[maxn*10];
LL ppap[maxn*10],yes[maxn*10],ans2[maxn*10];
typedef pair<LL,LL>pii;
priority_queue<pii,vector<pii>,greater<pii> >q;


void dijkstra(){
    memset(d,127,sizeof(d));
    d[0]=0;
    q.push(MK(0,0));
    while(!q.empty()){
        LL u=q.top().second,x=q.top().first;q.pop();
        for(LL i=head[u];i!=-1;i=e[i].next ){
            LL v=e[i].v;
            if(d[v]>d[u]+e[i].w){
                d[v]=d[u]+e[i].w;
                vis[v]=1;
                if(e[i].flag)yes[v]=e[i].flag ;
                else yes[v]=0;
                ppap[v]=u;
                q.push(MK(d[v],v));
            }    
        }
    }
    printf("%I64d\n",d[tot+1]);
    LL now=tot+1;
    while(now!=0){
        if(yes[now])ans2[++(*ans2)]=yes[now];
        now=ppap[now];
    }
    printf("%I64d\n",*ans2);
    for(LL i=*ans2;i>=1;i--)printf("%I64d ",ans2[i]);
}


int main(){
    memset(head,-1,sizeof(head));
    scanf("%d%d",&n,&L);
    for(LL x,d,t,p,i=1;i<=n;i++){
        scanf("%I64d%I64d%I64d%I64d",&x,&d,&t,&p);
        if(x-p<0||x+d>L)continue;
        nod[++cnt].a=x-p,nod[cnt].b=x+d,nod[cnt].t=t+p,nod[cnt].id=i;//??
        cur[++tot]=nod[cnt].a,cur[++tot]=nod[cnt].b;
    }
    sort(cur+1,cur+1+tot);tot=unique(cur+1,cur+1+tot)-1-cur;
    for(LL i=1;i<=cnt;i++){
        nod[i].a=Q(nod[i].a),nod[i].b=Q(nod[i].b);
        adde(nod[i].a,nod[i].b,nod[i].t,nod[i].id);
    }
    for(LL i=1;i<=tot;i++){
        adde(i-1,i,cur[i]-cur[i-1],0);adde(i,i-1,cur[i]-cur[i-1],0);
    }
    adde(tot,tot+1,L-cur[tot],0);
    dijkstra();
    return 00;
}


  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值