请问为什么re50
查看原帖
请问为什么re50
242317
xiaofu15191楼主2022/1/31 14:22
#include<iostream>
#include<vector>
#include<cstring>
#include<cstdio>
using namespace std;
struct p1119
{
	int from,to,distance;
};
vector<p1119>map;
int ttime[2001],asks[3],father[2001],d[4000001],dis[2001];
bool bz[2001],bz2[2001],bz3[2001];
int find(int x)
{
	if(father[x]!=x)
		father[x]=find(father[x]);
	return father[x];
}
void unionn(int x,int y)
{
	father[y]=x;
}
int main()
{
	//freopen("P1119_2.in","r",stdin);
	//freopen("P1119_2.out","w",stdin);
	ios::sync_with_stdio(0);
	int n,m,q,now=0;
	cin>>n>>m;
	for(int i=0;i<n;i++)
	{
		cin>>ttime[i];
		father[i]=i;
	}
	for(int i=1;i<=m;i++)
	{
		p1119 t1,t2;
		cin>>t1.from>>t1.to>>t1.distance;
		t2.from=t1.to;
		t2.to=t1.from;
		t2.distance=t1.distance;
		map.push_back(t1);
		map.push_back(t2);
	}
	cin>>q;
	for(int i=1;i<=q;i++)
	{
		cin>>asks[0]>>asks[1]>>asks[2];
		for(;now<=asks[2];now++)
			for(int j=0;j<n;j++)
				if(ttime[j]==now)
				{
					bz2[j]=1;
					for(int k=0;k<map.size();k++)
						if((map[k].from==j||map[k].to==j)&&bz2[map[k].to]==1&&bz2[map[k].from]==1)
						{
							bz3[k]=1;
							if(find(map[k].from)!=find(map[k].to))
								unionn(find(map[k].from),find(map[k].to));
						}
				}
		now--;
		if(find(asks[0])!=find(asks[1]))
			cout<<-1<<endl;
		else
		{
			int head=0,tail=1;
			memset(bz,0,sizeof(bz));
			memset(dis,0x3f,sizeof(dis));
			dis[asks[0]]=0;
			d[1]=asks[0];
			while(head<tail)
			{
				head++;
				bz[d[head]]=0;
				for(int j=0;j<map.size();j++)
				{
					if(map[j].from==d[head]&&bz3[j]==1)
					{
						if(dis[map[j].to]>dis[map[j].from]+map[j].distance)
						{
							dis[map[j].to]=dis[map[j].from]+map[j].distance;
							if(bz[map[j].to]==0)
							{
								bz[map[j].to]=1;
								tail++;
								d[tail]=map[j].to;
							}
						}
					}
				}
			}
			cout<<dis[asks[1]]<<endl;
		}
	}
}
2022/1/31 14:22
加载中...