Graduation voyage pour sauver des problèmes d'argent

Un laboratoire de recherche réunit des moines de différentes provinces. Juste avant l'obtention du diplôme, Yuan a proposé de procéder à un petit groupe voyage de fin d'études, à la recherche de carrière de tous Monk laisser une bonne note. Mike se sentait très bien, il représente toute la pensée de jour sur la façon de jouer le fort soutien. Un jour, le professeur Li lors d'un voyage d'affaires, rien d'autre, à son tour commencé à vagabonder, vagabonder aux questions de voyage: de Fuzhou, et enfin de retour à Fuzhou, comment Voyage entre (Nanchang, Nanjing, Hefei, Taiyuan, Zhengzhou) capitales provinciales péages la plupart des provinces? Ensuite, a écrit le code de ce C à l'ordinateur ont des solutions. Dans cet esprit, il est utile pour l'avenir.

Code des idées: le voyage, ainsi que parmi toutes les villes peuvent être considérées comme un graphique non orienté, pondéré entièrement connecté, recherche en profondeur d'abord à partir d'un point de départ pour trouver la bague de plus petit poids.

#include <vector>
#include<iostream>
#include<string>
using namespace std;

struct City//城市节点,记录名称以及是否被访问
{
	bool visited;
	string name;
	City():visited(false),name("no name"){}
};

enum CityName{
	FuZhou		=	0,
	NanChang	=	1,
	NanJing		=	2,
	HeFei		=	3,
	TaiYuan		=	4,
	ZhengZhou	=	5,
	TotCity		=	6,
};

const static int TravelPrice[TotCity][TotCity]={//各个城市之间的车票或者飞机票价格
	{0,		160,	314,	357,	430,	451},
	{160,	0,		369,	316,	235,	291},
	{314,	369,	0,		67,		395,	317},
	{357,	316,	67,		0,		278,	307},
	{430,	235,	395,	278,	0,		257},
	{451,	291,	317,	307,	257,	0}
}; 

const static string CityName_s[TotCity]={
	"FuZhou","NanChang","NanJing","HeFei","TaiYuan","ZhengZhou"
};

class Solution {
public:
	string SearchLowestPricePath(){
		string path, optPath;            //可能的路径以及最佳路径
		int price=0, optPrice=INT_MAX;   //可能的价格以及最佳价格
		vector<City> ourCity(TotCity);
		for(int i=0; i<TotCity; i++)
			ourCity[i].name=CityName_s[i];
		path="start";
		Travel(ourCity, FuZhou, path, optPath, price, 5, optPrice);//5块乘地铁
		return optPath;
	}
	void Travel(vector<City> visCity, int lastCity, string path, string& optPath, int Price, int curPrice, int& optPrice){	
		visCity[lastCity].visited=true;//上一城市已游玩
		path=path+" -> "+CityName_s[lastCity];//路径历史
		Price+=curPrice;
		for(int i=0; i<TotCity; i++){
			if(!visCity[i].visited)
				break;
			else if(i==TotCity-1){//递归基,所有城市都已访问,回福州
				Price+=TravelPrice[lastCity][FuZhou];
				path=path+" -> "+CityName_s[FuZhou]+" "+to_string(Price);
				if(Price<optPrice){
					optPrice=Price;
					optPath=path;
				}
				return;
			}
		}
		for(int i=1; i<TotCity; i++){//去下一个城市
			if(visCity[i].visited)//去过的不去
				continue;
			Travel(visCity, i, path, optPath, Price, TravelPrice[lastCity][i], optPrice);
		}
	}
};


int main()
{
	Solution c;
	cout<<c.SearchLowestPricePath()<<endl;
	system("pause");
	return 0;
}

Je veux obtenir un diplôme.

Publié 18 articles originaux · louanges gagnées 0 · Vues 789

Je suppose que tu aimes

Origine blog.csdn.net/afiguresomething/article/details/103205853
conseillé
Classement