## Pat(A) 1087. All Roads Lead to Rome (30)

### 1087. All Roads Lead to Rome (30)

Indeed there are many different tourist routes from our city to Rome. You are supposed to find your clients the route with the least cost while gaining the most happiness.

Input Specification:

Each input file contains one test case. For each case, the first line contains 2 positive integers N (2<=N<=200), the number of cities, and K, the total number of routes between pairs of cities; followed by the name of the starting city. The next N-1 lines each gives the name of a city and an integer that represents the happiness one can gain from that city, except the starting city. Then K lines follow, each describes a route between two cities in the format "City1 City2 Cost". Here the name of a city is a string of 3 capital English letters, and the destination is always ROM which represents Rome.

Output Specification:

For each test case, we are supposed to find the route with the least cost. If such a route is not unique, the one with the maximum happiness will be recommended. If such a route is still not unique, then we output the one with the maximum average happiness -- it is guaranteed by the judge that such a solution exists and is unique.

Hence in the first line of output, you must print 4 numbers: the number of different routes with the least cost, the cost, the happiness, and the average happiness (take the integer part only) of the recommended route. Then in the next line, you are supposed to print the route in the format "City1->City2->...->ROM".

Sample Input:
6 7 HZH
ROM 100
PKN 40
GDN 55
PRS 95
BLN 80
ROM GDN 1
BLN ROM 1
HZH PKN 1
PRS ROM 2
BLN HZH 2
PKN GDN 1
HZH PRS 1

Sample Output:
3 3 195 97
HZH->PRS->ROM

### 代码

#include "iostream"
#include "vector"
#include "map"
#include "string"
using namespace std;

int N,K;
string startCity;
map<string,int> city2id;
map<int,string> id2city;
vector<int> happiness;
int G[200 + 5][200 + 5];
vector<int> routes;
vector<bool> visited;
int numOfRoutes,cost,happ,numOfCity;
int tempCost = 0,tempHapp = 0,tempNumOfCity = 1;
vector<int> tempRoutes;

void init(){
cin>>N>>K>>startCity;
happiness.resize(N);
routes.resize(N);
visited.resize(N,false);
tempRoutes.resize(N);
numOfCity = cost = happ = numOfCity = 0;
int i;
city2id[startCity] = 0;
id2city = startCity;
string city;
int h;
for(i = 1; i < N; i++){
cin>>city>>h;
city2id[city] = i;
id2city[i] = city;
happiness[i] = h;
}
string city2;//下面用的变量应该是出发城市，到达城市和花费的，节省一点变量，直接用上面的了。
for(i = 0; i < K; i++){
cin>>city>>city2>>h;
cost += h;
G[city2id[city]][city2id[city2]] = h;
G[city2id[city2]][city2id[city]] = h;
}
}

void dfs(int city){
if(city == city2id[string("ROM")]){
if(tempCost<cost){
numOfRoutes = 1;
happ = tempHapp;
cost = tempCost;
numOfCity = tempNumOfCity;
for(int i = 0; i<= numOfCity; i++){
routes[i] = tempRoutes[i];
}
}else if(tempCost == cost){
numOfRoutes ++;
if(tempHapp > happ){
happ = tempHapp;
numOfCity = tempNumOfCity;
for(int i = 0; i<= numOfCity; i++){
routes[i] = tempRoutes[i];
}
}else if(tempHapp == happ && tempNumOfCity < numOfCity){
numOfCity = tempNumOfCity;
for(int i = 0; i<= numOfCity; i++){
routes[i] = tempRoutes[i];
}
}

}
return;
}
for(int i = 1; i < N; i++){
if(!visited[i] && G[city][i]){
tempCost += G[city][i];
tempHapp += happiness[i];
tempNumOfCity ++;
tempRoutes[tempNumOfCity] = i;
visited[i] = true;
dfs(i);
visited[i] = false;
tempCost -= G[city][i];
tempHapp -= happiness[i];
tempNumOfCity --;
}
}
}

void print_G(){
int i,j;
for(i = 0;i < N; i++)
cout<<"\t"<<i;
cout<<endl;
for(i = 0;i < N; i++)
cout<<"-------\t";
cout<<endl;
for(i = 0; i < N; i++){
cout<<i<<"|";
for(j=0; j < N; j++)
cout<<"\t"<<G[i][j];
cout<<endl;
}

}

int main(){
init();
//print_G();
tempNumOfCity = 0;
tempRoutes = 0;
visited = true;
dfs(0);
printf("%d %d %d %d\n",numOfRoutes,cost,happ,happ/numOfCity);
for(int i = 0; i < numOfCity; i++)
cout<<id2city[routes[i]]<<"->";
cout<<"ROM"<<endl;
//system("pause");
}

还没有人评论...