您的位置:首页 > 编程语言 > C语言/C++

1087. All Roads Lead to Rome (30)

2016-03-02 23:12 441 查看
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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息