RQNOJ 389 心灵的抚慰【解题报告】

Floyd算法:

#include <cstdio>
#include <iostream>
#include <cstring>
using namespace std;
int n,m,a[251][251],d[251][251],ans,x,y;
int main()
{
	scanf("%d%d",&n,&m);
	for (int i=1;i<=n;i++)
	for (int j=1;j<=n;j++)
	{
		a[i][j]=20000000;
		d[i][j]=20000000;
	}	
	ans=20000000;
	for (int i=1;i<=m;i++)
	{
		scanf("%d%d",&x,&y);
		scanf("%d",&d[x][y]);
		d[y][x]=d[x][y];
		a[x][y]=d[x][y];
		a[y][x]=d[x][y];
	}
	for (int k=1;k<=n;k++)
	{
		for (int i=1;i<k;i++)
			for (int j=i+1;j<k;j++)
			{
				if (ans>a[i][j]+d[i][k]+d[k][j]) ans=a[i][j]+d[i][k]+d[k][j];
			}
		for (int i=1;i<=n;i++)
			for (int j=1;j<=n;j++)
			{
				if(a[i][k]+a[k][j]<a[i][j])
				a[i][j]=a[i][k]+a[k][j];
			}
	}
	if (ans==20000000) printf("He will never come back.");
	else printf("%d",ans);
	return 0;
}



你可能感兴趣的:(图论,floyd)