⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 main.cpp

📁 船舶运货算法问题
💻 CPP
字号:
#include <iostream>
#include <fstream>
using namespace std;
int IsLetter(string test_str){
	for(string::size_type iterator=0 ; iterator != test_str.size() ; ++iterator)
		if(test_str[iterator] == '.' || test_str[iterator]>='A' && test_str[iterator]<='Z'|| test_str[iterator]>='a' && test_str[iterator]<='z')
			continue;
		else
			return 0;
	return 1;
}
int main()
{
	//文件名字符串
	string filename;
	//手动指定文件名
	cout << "Please enter the file name that you want to open:" << endl;
	//验证文件名的输入是否正确
	while(cin >> filename){
		if(1 == IsLetter(filename))
			break;
		else
			cout << "Please enter the file name that you want to open:" << endl;
	}
	//构造一个ifstream对象,并将"filename"文件名捆绑到其上
	ifstream infile(filename.c_str());
	//判断文件是否可以打开
	if(!infile){
		cerr << "error: unable to open input file: " << filename << endl;
		return -1;
	}
	//定义游艇出租站的个数
	int BargeLeaseStopNum;
	infile>>BargeLeaseStopNum;
	int path[BargeLeaseStopNum][BargeLeaseStopNum];
	//初始化主对角线元素
	for(int i=0 ; i<BargeLeaseStopNum ; ++i)
		path[i][i] = 0;
	//初始化上三角矩阵元素
	for(int i=0 ; i<BargeLeaseStopNum ; ++i)
		for(int j=i+1 ; j<BargeLeaseStopNum ; ++j)
			infile>>path[i][j];
	//对路径信息进行更新
	for(int Update=2 ; Update<BargeLeaseStopNum ; ++Update)
		for(int i=0 ; i<BargeLeaseStopNum-1 ; ++i)
			for(int j=i+1 ; j<Update; ++j){
				int temp = path[i][j]+path[j][Update];
				path[i][Update]=path[i][Update]>temp?temp:path[i][Update];
			}
	cout<<"The shortest path from the first stop to the end stop is: "<<path[0][BargeLeaseStopNum-1]<<endl;
	return 0;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -