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 <map>
#include <algorithm>
#include <vector>
#include <cstdio>
#include <cstring>
#include <utility>
#define INF (0x6fffffff)
#define MAX 205

using 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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值