1087. All Roads Lead to Rome (30)

来源:互联网 发布:中文在线发音软件 编辑:程序博客网 时间:2024/05/19 14:38

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

虽然是典型的求最短路径,不过因为城市名称不是单纯的数字所以要映射成下标再进行,写起来还是非常费时;

#include <iostream>#include <map>#include <algorithm>#include <vector>#include <cstdio>#include <cstring>#include <utility>#define INF (0x6fffffff)#define MAX 205using namespace std;struct Node{    bool known;    int happy;    int dist;    vector<pair<int, int> > v;    Node(): known(false), happy(0), dist(INF){};};Node graph[MAX];int numofcity[MAX] = {0};//记录路径城市数量int happiness[MAX] = {0};//记录总幸福值int pre[MAX] = {0};//记录最短路径前位点int dif[MAX] = {0};//记录不同最短路径条数string key_city[MAX];//2个hash实现下标和城市名称互转map<string, int> city_key;bool flag = true;int N, K, counter = 1;string Start;void init(){    int temp;    string city;    cin >> N >> K >> Start;    city_key[Start] = 0;    key_city[0] = Start;    for(int i=1; i<N; i++)    {        cin >> city >> temp;        city_key[city] = i;        key_city[i] = city;        graph[i].happy = temp;    }    string c1, c2;    int w;    for(int i=0; i<K; i++)    {        cin >> c1 >> c2 >> w;        graph[city_key[c1]].v.push_back(make_pair(city_key[c2], w));        graph[city_key[c2]].v.push_back(make_pair(city_key[c1], w));    }}void dijkstra(int x){    int key, mindist;    graph[x].dist = 0;    dif[x] = 1;    vector<pair<int, int> >::iterator it;    for(int i=0; i<N; i++)    {        mindist = INF;        for(int j=0; j<N; j++)        {            if(!graph[j].known && graph[j].dist < mindist)            {                mindist = graph[j].dist;                key = j;            }        }        graph[key].known = true;        if(key == city_key["ROM"])            break;        for(it=graph[key].v.begin(); it!=graph[key].v.end(); it++)        {            if(!graph[it->first].known)            {                if(it->second+mindist < graph[it->first].dist)//路径较短则更新                {                    graph[it->first].dist = it->second+mindist;                    pre[it->first] = key;                    numofcity[it->first] = numofcity[key]+1;                    happiness[it->first] = happiness[key]+graph[it->first].happy;                    dif[it->first] = dif[key];                }                else if(it->second+mindist == graph[it->first].dist)//路径相同则更新不同路径数,数量增加是前位点最短路径数而不是1                {                    dif[it->first] += dif[key];                    if(happiness[it->first] < happiness[key]+graph[it->first].happy)//总幸福值较大则更新                    {                        graph[it->first].dist = it->second+mindist;                        pre[it->first] = key;                        numofcity[it->first] = numofcity[key]+1;                        happiness[it->first] = happiness[key]+graph[it->first].happy;                    }                    else if(happiness[it->first] == happiness[key]+graph[it->first].happy)//总幸福值相等时                    {                        if(numofcity[it->first] > numofcity[key]+1)//沿途城市数量较小即平均数较大则更新                        {                            graph[it->first].dist = it->second+mindist;                            pre[it->first] = key;                            numofcity[it->first] = numofcity[key]+1;                            happiness[it->first] = happiness[key]+graph[it->first].happy;                        }                    }                }            }        }    }}void print_path(int x){    if(x != 0)        print_path(pre[x]);    if(flag)    {        cout << key_city[x];        flag = false;    }    else        cout << "->" << key_city[x];}int main(){    //freopen("in.txt", "r", stdin);    init();    dijkstra(0);    int e = city_key["ROM"];    cout << dif[e] << " " << graph[e].dist << " " << happiness[e] << " " << happiness[e]/numofcity[e] << endl;    print_path(e);    return 0;}


0 0
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 唐氏筛查21三体高危怎么办 唐筛年龄高风险怎么办 21三体综合症高风险怎么办 朋友深陷李强365怎么办 飞机上烟瘾犯了怎么办 怀孕一个月吸烟了怎么办 烟瘾犯了没烟怎么办 押金交了不租了怎么办 买车首付款不够怎么办 双11订金不退怎么办 在商场买到假货怎么办 网上买到假手机怎么办 网上买了假手机怎么办 网银转账被骗了怎么办 支付宝被骗了钱怎么办 被支付宝骗了钱怎么办 头发出油怎么办小妙招 照相的时候脸歪怎么办 怀孕两个月同床了怎么办 小姐被警察抓了怎么办 我的世界迷路了怎么办 砸到手指甲很痛怎么办 手被锤子砸肿了怎么办 拇指突然疼肿了怎么办 手指甲压紫了痛怎么办 砸到手指头肿了怎么办 小婴儿体重不长怎么办 2岁半宝宝不说话怎么办 米兰叶子掉光了怎么办 qq雷霆战机闪退怎么办 微信限额20万怎么办 欧拉方程x<0怎么办 柿子核吃下去了怎么办 橡胶底白色变黄怎么办 20了丁丁还很小怎么办 5角硬币吞进肚子怎么办 孩子吞了5角硬币怎么办 左腿比右腿微粗怎么办 一个腿长一个腿短怎么办 刚买的毛笔很硬怎么办 我哥哥太爱我了怎么办