Code vs 1391 伊吹萃香(虫洞)

QAQ
思路:SPFA+分层图
dis[i][j]表示第i个节点在第j个时间的最短路径
可以看出j并不需要全部的记下来,我们只需要用0,1来记录时间就行了
同理vis数组同样两维
根据题目要求拓展的花费,注意处理等待的情况
优化,双向队列,如果当前拓展出的节点dis比队首小,就将这个节点放在队首,这样就可以更快的拓展出最终的状态辣

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<queue>
#include<cstring>
using namespace std;
const int maxn=60005;      
struct dqs
{
    int f,t,c;
}hh[maxn];
struct dqm
{
    int pos;
    bool bw;
};
int first[maxn],next[maxn];
int tot=0;
void build(int f,int t,int c)
{
    hh[++tot]=(dqs){f,t,c};
    next[tot]=first[f];
    first[f]=tot;
}
bool color[maxn],used[maxn][2];
int dis[maxn][2],w[maxn],s[maxn];
int cha(int f,int t,int bow)
{
    int fcha=color[f]^bow;
    int tcha=color[t]^bow;
    if(fcha==tcha)
        return 0;
    else
    {
        int cha=abs(w[f]-w[t]);
        if(fcha==1&&tcha==0)
            return cha;
        else
            return -cha;
    }
}
deque<dqm>q;
void spfa(int st)
{
    dqm begin;
    begin.pos=st;
    begin.bw=0;
    q.push_back(begin);
    dis[st][0]=0;
    used[st][0]=1;
    while(!q.empty())
    {
        dqm head=q.front();
        q.pop_front();
        used[head.pos][head.bw]=0;
        for(int i=first[head.pos];i;i=next[i])
        {
            int u=hh[i].t;
            dqm now;
            now.pos=u;
            now.bw=head.bw^1;           
            if(dis[u][now.bw]>dis[head.pos][head.bw]+max(hh[i].c+cha(head.pos,u,head.bw),0))
            {
                dis[u][now.bw]=dis[head.pos][head.bw]+max(hh[i].c+cha(head.pos,u,head.bw),0);
                if(!used[u][now.bw])
                {
                    if(q.size()!=0)
                    {
                         if(dis[u][now.bw]<=dis[q.front().pos][q.front().bw])
                            q.push_front(now);
                        else
                            q.push_back(now);           
                    }
                    else
                        q.push_back(now);
                    used[u][now.bw]=1;                  
                }
            }   
        }
        if(dis[head.pos][head.bw^1]>dis[head.pos][head.bw]+s[head.pos]*(color[head.pos]^head.bw))
        {
            dis[head.pos][head.bw^1]=dis[head.pos][head.bw]+s[head.pos]*(color[head.pos]^head.bw);
            if(!used[head.pos][head.bw^1])
            {
                used[head.pos][head.bw^1]=1;
                if(q.size()!=0)
                {
                    if(dis[head.pos][head.bw^1]<=dis[q.front().pos][q.front().bw])
                        q.push_front((dqm){head.pos,head.bw^1});
                    else
                        q.push_back((dqm){head.pos,head.bw^1});                                         
                }           
                else
                    q.push_back((dqm){head.pos,head.bw^1}); 
            }
        }
    }
}
int main()
{
    int n,m,f,t,c;
    scanf("%d%d",&n,&m);
    memset(dis,0x3f,sizeof(dis));
    for(int i=1;i<=n;i++)
        scanf("%d",&color[i]);
    for(int i=1;i<=n;i++)
        scanf("%d",&w[i]);
    for(int i=1;i<=n;i++)
        scanf("%d",&s[i]);
    for(int i=1;i<=m;i++)
    {
        scanf("%d%d%d",&f,&t,&c);
        build(f,t,c);
    }
    spfa(1);
    printf("%d",min(dis[n][0],dis[n][1]));
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值