弗洛伊德最短路径

 时间复杂度为O(n*n*n) 结构简单,可以算任意两点间最短路径

#include <cstdio>
#include <iostream>
#include <algorithm>
#include <cstring>
#include <string>
#include <queue>
#include <cmath>
#include <fstream>
const int MAX = 1e4+10;
const int INF = 1e6;

using namespace std;

int d[MAX][MAX];
int V, E;

void  Floyd(){
	for(int i=0; i<V; i++)
		for(int j=0; j<V; j++)
			for(int k =0; k<V; k++)
				d[j][k] = min(d[j][k], d[j][i]+d[i][k]);
}
//测试函数 
int main(){
	ifstream cin ("D:\\钢铁程序员\\程序数据\\003最短路径.txt");//从文件读取数据流,省去手动输入的麻烦 
	if(!cin){//读取如果失败 
		cout << "ERROR" << endl;
	}
	cin >> V >> E;
	for(int i=0; i<V; i++)
		for(int j=0; j<V; j++){
			if(i == j)
				d[i][j] = 0;
			else
				d[i][j] = INF;
		}
	for(int i=0; i<E; i++){
		int a, b, c;
		cin >> a >> b >> c;
		d[a][b] = c;
		d[b][a] = c;
	}
	
	Floyd();
	cout << d[0][V-1] << endl;
	cin.close();//打开文件以后要关闭 
	return 0;
} 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值