PAT 1087. All Roads Lead to Rome (30)

来源:互联网 发布:靠谱韩代 淘宝店 良心 编辑:程序博客网 时间:2024/05/01 07:06

1087. All Roads Lead to Rome (30)

时间限制
200 ms
内存限制
32000 kB
代码长度限制
16000 B
判题程序
Standard
作者
CHEN, Yue

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 HZHROM 100PKN 40GDN 55PRS 95BLN 80ROM GDN 1BLN ROM 1HZH PKN 1PRS ROM 2BLN HZH 2PKN GDN 1HZH PRS 1
Sample Output:
3 3 195 97HZH->PRS->ROM思路: 深度搜索,根据cost剪枝 
#include <stdio.h>#include <string.h>#include <vector>using namespace std;int N, K, dis, rom_index;struct Node{char name[10];bool visit;int hap;};Node list[210];char st[10], en[10];int map[210][210];int max_hap = 0, min_dis = 99999999, cnt = 0;vector<Node> temp_res;vector<vector<Node> > res;int find_index(char *city){for(int i = 0; i < N; i++){if(strcmp(city, list[i].name) == 0){return i;}}}void fun(int cur, int total_hap, int total_dis){if(min_dis < total_dis){return;}if(cur == rom_index){if(total_dis < min_dis){cnt = 1;res.push_back(temp_res);max_hap = total_hap;min_dis = total_dis;}else if(total_dis == min_dis){cnt++;if(total_hap > max_hap){max_hap = total_hap;res.push_back(temp_res);}else if(total_hap == max_hap && res[0].size() > temp_res.size()){res.push_back(temp_res);}}return;}else{for(int i = 0; i < N; i++){if(!list[i].visit && map[cur][i] > 0){list[i].visit = true;total_hap += list[i].hap;total_dis += map[cur][i];temp_res.push_back(list[i]);fun(i, total_hap, total_dis);temp_res.pop_back();total_hap -= list[i].hap;total_dis -= map[cur][i];list[i].visit = false;}}}}int main(){while(scanf("%d %d %s", &N, &K, &list[0].name) != EOF){list[0].visit = true;int s, e;for(int i = 1; i < N; i++){list[i].visit = false;scanf("%s %d", &list[i].name, &list[i].hap);if(strcmp(list[i].name, "ROM") == 0){rom_index = i;}}memset(map, 0, sizeof(map));for(int i = 0; i < K; i++){scanf("%s %s %d", &st, &en, &dis);s = find_index(st);e = find_index(en);map[s][e] = dis;map[e][s] = dis;}fun(0, 0, 0);printf("%d %d %d %d\n", cnt, min_dis, max_hap, max_hap / res.back().size());printf("%s", list[0].name);for(int i = 0; i < res.back().size(); i++){printf("->%s", res.back()[i].name);}printf("\n");}return 0;}

0 0
原创粉丝点击