1087. All Roads Lead to Rome (30)

DFS, 走的过程,把符合条件的全记录下来

#include <iostream>
#include <cstdio>
#include <vector>
#include <unordered_map>

using namespace std;

#define MAX_CITY (205)
#define INF (~(1<<31))

vector<vector<int>> graph(MAX_CITY, vector<int>(MAX_CITY, -1));
vector<int> happy(MAX_CITY, 0);
vector<bool> used(MAX_CITY, false);
vector<vector<int>> paths;
vector<int> bestPath;
vector<string> cities;

int minCost = INF;
int maxHappy = -INF;
int numCity = 0;

void dfs(int src, int dst, int cost, int num, int hap, vector<int>& path){
	if(src == dst){
		if(minCost > cost){
			minCost = cost;
			maxHappy = hap;
			numCity = num;
			bestPath = path;
			paths = vector<vector<int>>(1, bestPath);
		}else if(minCost == cost){
			paths.push_back(path);

			if(maxHappy < hap){
				maxHappy = hap;
				numCity = num;
				bestPath = path;
			}else if(maxHappy == hap){
				if(maxHappy / numCity < hap / num){
					numCity = num;
					bestPath = path;
				}
			}
		}
	}

	for(int i = 0; i < MAX_CITY; ++i){
		if(!used[i] && graph[src][i] != -1){
			used[i] = true;
			path.push_back(i);
			dfs(i, dst, cost + graph[src][i], num+1, hap+happy[i], path);
			used[i] = false;
			path.pop_back();
		}
	}
}


int main(){
	string city;
	int n, k;

	cin >> n >> k >> city;

    unordered_map<int, string> int2city;
    unordered_map<string, int> city2int;

	int2city[0] = city;
	city2int[city] = 0;

	for(int i = 0; i < n-1; ++i){
		int h;
		string c;
		cin >> c >> h;
		int2city[i+1] = c;
		city2int[c] = i+1;
		happy[i+1] = h;
	}

	for(int i = 0; i < k; ++i){
		int cost;
		string c1, c2;
		cin >> c1 >> c2 >> cost;

		int x = city2int[c1], y = city2int[c2];
		graph[x][y] = cost;
		graph[y][x] = cost;
	}

	int src = city2int[city], dst = city2int["ROM"];
	used[src] = true;
	vector<int> path(1, src);
	dfs(src, dst, 0, 0, 0, path);

	printf("%d %d %d %d\n", (int)paths.size(), minCost, maxHappy, maxHappy/numCity);

	for(size_t i = 0; i < bestPath.size(); ++i){
		if(i) printf("->");
		printf("%s", int2city[bestPath[i]].c_str());
	}


	return 0;
}


评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值