spfa——文化之旅

就像是玛丽卡和最有贸易一样,这道题应该也算是很经典的spfa的题目,在spfa的过程中增加了一个判断文化的函数,其他的地方都没什么太大的变化

#include<iostream>
using namespace std;
const int MAXN=100001;
const int INF=9999999;
int tot,n,m,k,s,t;
int g[MAXN];
int q[MAXN],dis[MAXN],judge[MAXN];
int culture[MAXN][MAXN];
int cul[MAXN];
bool learn[MAXN][MAXN];
struct line
{
int to;
int w;
int next;
}edge[MAXN*2];
void add1(int a,int b,int c)
{
tot++;
edge[tot].to=b;
edge[tot].w=c;
edge[tot].next=g[a];
g[a]=tot;
}
bool check(int x,int cul)
{
if(learn[x][cul])return false;
for(int i=1;i<=k;i++)
{
if(learn[x][i]&&culture[cul][i])return false;//??
}
return true;
}
int spfa(int a,int b)
{
for(int i=1;i<=n;i++)
dis[i]=INF;
int h=0;
int t=1;
q[t]=a;
dis[a]=0;
learn[s][cul[s]]=true;
while(h!=t)
{
h=h%n+1;
int x=q[h];
int temp=g[x];
while(temp)
{
if(dis[edge[temp].to]>dis[x]+edge[temp].w&&check(x,cul[edge[temp].to]))
{
learn[edge[temp].to][cul[edge[temp].to]]=true;
dis[edge[temp].to]=dis[x]+edge[temp].w;
for(int i=1;i<=k;i++)//文化状态的转移 
{
if(learn[x][i])learn[edge[temp].to][i]=true;
else learn[edge[temp].to][i]=false;
}
if(!judge[edge[temp].to])
{
t=t%n+1;
q[t]=edge[temp].to;
judge[edge[temp].to]=true;
}
}
temp=edge[temp].next;
}
judge[x]=false;
}
return dis[b];
}
int main()
{
cin>>n>>k>>m>>s>>t;
for(int i=1;i<=n;i++)
cin>>cul[i];
for(int i=1;i<=k;i++)
for(int j=1;j<=k;j++)
cin>>culture[i][j];
for(int i=1;i<=m;i++)
{
int x,y,z;
cin>>x>>y>>z;
add1(x,y,z);
add1(y,x,z);
}
int ans=spfa(s,t);
if(ans==INF)cout<<"-1";
else cout<<ans;
return 0;

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值