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;
}