灾后重建 Floyd

 

Code:

#include<cstdio>
#include<algorithm>
using namespace std;
const int N=205;
const int INF=10000000+233;
int d[N][N],times[N];
int main()
{
	//freopen("in.txt","r",stdin);
	int n,m;
	scanf("%d%d",&n,&m);
	for(int i=0;i<n;++i)
		for(int j=0;j<n;++j)
			if(i!=j)d[i][j]=d[j][i]=INF;
	for(int i=0;i<n;++i)scanf("%d",&times[i]);
	times[n]=INF;
	for(int i=1;i<=m;++i)
	{
		int a,b,c;
		scanf("%d%d%d",&a,&b,&c);
		d[a][b]=d[b][a]=c;
	}
	int q,ed=0,k=0;
	scanf("%d",&q);
	while(q--)
	{
		int a,b,c;
		scanf("%d%d%d",&a,&b,&c);
		while(times[ed+1]<=c)++ed;
		for(;k<=ed;++k)
			for(int i=0;i<n;++i)
				for(int j=0;j<n;++j)
					if(d[i][k]+d[k][j]<d[i][j])d[i][j]=d[j][i]=(d[i][k]+d[k][j]);
		if(d[a][b]==INF||a>ed||b>ed)printf("-1\n");
		else printf("%d\n",d[a][b]);
	}
	return 0;
}

  

posted @ 2018-10-17 00:12  EM-LGH  阅读(106)  评论(0编辑  收藏  举报