hdu 1599 find the mincost route_最小环

#include <iostream>
#include<cstdio>
using namespace std;
#define N 110
#define INF 0xffffff
int map[N][N],n,m,dist[N][N];
/* run this program using the console pauser or add your own getch, system("pause") or input loop */
void floyd(){
	int i,j,k,ans=INF;
	for(i=0;i<n;i++)
		for(j=0;j<n;j++)
			dist[i][j]=map[i][j];
	for(k=0;k<n;k++){
		for(i=0;i<k;i++)
			for(j=i+1;j<k;j++)
				ans=min(ans,dist[i][j]+map[i][k]+map[k][j]);
		for(i=0;i<n;i++)
			for(j=0;j<n;j++)
				dist[i][j]=min(dist[i][j],dist[i][k]+dist[k][j]);
	}
	if(ans<INF)
		printf("%d\n",ans);
	else
		printf("It's impossible.\n");			
}
int main(int argc, char** argv) {
	int i,j,a,b,d;
	while(scanf("%d%d",&n,&m)!=EOF){
		for(i=0;i<n;i++)
			for(j=0;j<n;j++)
				map[i][j]=INF;
		for(i=0;i<m;i++){
			scanf("%d%d%d",&a,&b,&d);
			if(map[a-1][b-1]>d)
				map[a-1][b-1]=map[b-1][a-1]=d;
		}
		floyd();
	}
	return 0;
}

你可能感兴趣的:(route)