40分求助!
查看原帖
40分求助!
838044
Johnjin楼主2024/8/2 10:55

请各位帮帮本蒟蒻QWQ 代码:

#include<bits/stdc++.h>
using namespace std;
const int  N=2005;
const int M=1e6+5;
typedef long long ll;
typedef pair<int,ll> P;
ll n,k,m,s,t,x,y,z;
int head[N+5],v[N+5],nex[N+5],w[N+5],cnt,dis[M],vis[M],a[N][N],c[N];
priority_queue <P,vector<P>,greater<P> > q;
vector<int>d[N];
ll read(){
	int f=1;
	ll x=0;
	char ch=getchar();
	while(ch<'0'||ch>'9'){
		if(ch=='-')	f=-1;
		ch=getchar();
	}
	while(ch>='0'&&ch<='9'){
		x=x*10+(ch-'0');
		ch=getchar();
	}
	return x*f;
}
void add(int x,int y,int z){
	v[++cnt]=y;	
	w[cnt]=z;
	nex[cnt]=head[x]; 
	head[x]=cnt;
}

bool f(int x,int y){
	for(int j=0;j<d[y].size();j++){
		if(d[y][j]==x)	return false;
	}
	return true;
}

void dijkstra(int x){
	memset(dis,127,sizeof(dis));
	dis[x]=0;
	q.push(P(0,x));
	while(!q.empty()){
		P p1=q.top();q.pop();
		int now=p1.second;
		vis[now]=0;
		for(int i=head[now];~i;i=nex[i]){
			int p=v[i];
			if(f(c[now],c[p])){
				if(dis[now]+w[i]<dis[p]){
					dis[p]=dis[now]+w[i];
					if(!vis[p]){
						vis[p]=1;
						q.push(P(dis[p],p));
					}
				}
			}else{
				continue;
			}
			
		}
	}
}

int main(){
	memset(head,-1,sizeof(head));
	n=read(),k=read(),m=read(),s=read(),t=read();
	
	for(int i=1;i<=n;i++)	c[i]=read();
	
	for(int i=1;i<=k;i++){
		for(int j=1;j<=k;j++){
			a[i][j]=read();
			if(a[i][j])	d[i].push_back(j);
		}
	}
	
	while(m--){
		x=read(),y=read(),z=read(); 
		if(!a[x][y])	add(y,x,z);
		if(!a[y][x])	add(x,y,z);
	}
	
	dijkstra(s);
	if(dis[t]!=2139062143)	cout<<dis[t]<<"\n";
	else cout<<-1<<"\n";
	
	return 0;
}
2024/8/2 10:55
加载中...