culture文化之旅

#include<stdio.h>
#include<iostream>
#include<queue>
using namespace std;
const int MAX_N = 101;
const int INF = 16843009;
int N,K,M,S,T;
int C[MAX_N];
int G[MAX_N][MAX_N];
int R[MAX_N][MAX_N];
int dist[MAX_N];
bool Inqueue[MAX_N];
bool gone[MAX_N];
struct node
{
	bool culture[MAX_N];
	int country;
};
struct node head,next;
queue<struct node> Q;
int ans = INF;
void SPFA()
{
	int i;
	int v;
	int u;
	for (i=1;i<=K;i++)
	head.culture[i]=false;
	head.country = T;
	memset(dist,1,sizeof(dist));
	memset(Inqueue,false,sizeof(Inqueue));
	dist[T]=0;
	Inqueue[T]=true;
	head.culture[T]=true;
	Q.push(head);
	while (!Q.empty())
	{
		head=Q.front();
		Q.pop();
		v=head.country;
		Inqueue[v]=false;
		for (u=1;u<=N;u++)
		if (G[u][v])
		{
			next=head;
			next.country = u;
			bool flag = true;
			for (i=1;i<=N;i++)
			if (head.culture[i]&&R[C[i]][C[u]]) flag = false;
			if (head.culture[u]) flag = false;
			if (C[T]==C[u]&&T!=u) flag = false;
			if (dist[v]+G[u][v]>=dist[u]) flag = false;
			if (!flag) continue;
			dist[u]=dist[v]+G[u][v];
			if (!Inqueue[u])
			{
				Inqueue[u]=true;
				next.culture[u]=true;
				Q.push(next);
			}
		}
	}
}
void init()
{
	int i,j;
	int s,t,w;
	scanf("%d %d %d %d %d",&N,&K,&M,&S,&T);
	for (i=1;i<=N;i++)
	scanf("%d",&C[i]);
	for (i=1;i<=K;i++)
	for (j=1;j<=K;j++)
	scanf("%d",&R[i][j]);
	for (i=1;i<=M;i++)
	{
		scanf("%d %d %d",&s,&t,&w);
		G[s][t]=(G[s][t]!=0)?min(G[s][t],w):w;
		G[t][s]=(G[t][s]!=0)?min(G[t][s],w):w;
	}
	SPFA();
	memset(gone,false,sizeof(gone));
	gone[S]=true;
}
int work(int country,int value)
{
	int i,j;
	bool flag;
	if (value+dist[country]>=ans) return 0;
	if (country==T)
	{
		ans = min(ans,value);
		return  0;
	}
	for (i=1;i<=N;i++)
	if (G[country][i])
	{
		flag = true;
		for (j=1;j<=N;j++)
		if (gone[j]&&R[C[i]][C[j]]) flag = false;
		if (C[i]==C[T]&&i!=T) flag = false;
		if (gone[i]) flag = false;
		if (!flag) continue;
		gone[i]=true;
		work(i,value+G[country][i]);
		gone[i]=false;
	}
	return  0;
}
void put()
{
	if (ans==INF) ans=-1;
	printf("%d",ans);
}
int main()
{
	freopen("culture.in","r",stdin);
	freopen("culture.out","w",stdout);
	init();
	work(S,0);
	put();
	return 0;
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值