准备PAT之All Roads Lead to Rome Dijikstra算法变型

All Roads Lead to Rome (30)
时间限制 1000 ms 内存限制 65536 KB 代码长度限制 100 KB 判断程序 Standard (来自 小小)
题目描述
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.

输入描述:
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.

输出描述:
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”.

输入例子:
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

输出例子:
3 3 195 97
HZH->PRS->ROM

#include<iostream>
#include<algorithm>
#include<vector>
#include<string>
#include<cstring>
#include<map>
using namespace std;
const int INF = 0x3f3f3f3f;
const int max_city = 200 + 5;
map<string, int>city;//保证可以通过城市i的名字就可以找到它的编号
map<int, string>r_city;//保证知道城市编号就可以知道它的名字
int cost[max_city][max_city], city_num, road_num;
int dist[max_city];//最小距离
int happiness[max_city], have_happiness[max_city];//城市i本身的快乐;到城市i你拥有的快乐
int if_visit[max_city];
int pass_city_num[max_city], pre[max_city];//到达城市i时所通过的最小城市数
int arrive_road_num[max_city];//到达城市i的最短路径条数
void Init() {
    for (int i = 0; i <= city_num; i++) {
        happiness[i] = 0;
        for (int j = 0; j <= city_num; j++) {
            cost[i][j] = INF;
        }
    }
}
void Input() {
    string star_city;
    cin >> city_num >> road_num >> star_city;

    Init();
    city[star_city] = 0;
    r_city[0] = star_city;
    happiness[city[star_city]] = 0;

    for (int i = 1; i < city_num; i++) {
        string city_name;
        cin >> city_name >> happiness[i];
        city[city_name] = i;
        r_city[i] = city_name;
    }
    for (int i = 0; i < road_num; i++) {
        string from, to; int happy;
        cin >> from >> to >> happy;
        cost[city[from]][city[to]] = min(cost[city[from]][city[to]], happy);
        cost[city[to]][city[from]] = min(cost[city[to]][city[from]], happy);
    }
}
void Dijikstra() {
    for (int i = 0; i <= city_num; i++) {
        if_visit[i] = have_happiness[i] = pass_city_num[i] = 0;
        dist[i] = INF;
    }
    dist[0] =0;
    pre[0] = -1;
    arrive_road_num[0] = 1;
    for (int i = 0; i < city_num; i++) {
        int v = -1;
        //找到一个未被访问过的点
        for (int u = 0; u < city_num; u++) {
            if (!if_visit[u] && (v == -1 || dist[u] < dist[v])) {
                v = u;
            }
        }
        if (v == -1)    break;
        if_visit[v] = 1;
        //从该未被访问的点v依次访问与其连接且未被访问的点j
        for (int j = 0; j < city_num; j++){
            if (!if_visit[j] && cost[v][j] < INF) {
                int temp = dist[v] + cost[v][j];
                if (temp < dist[j]) {                   //若从点v到点j距离更小则更新
                    dist[j] = temp;
                    have_happiness[j] = have_happiness[v] + happiness[j];//加上城市j的快乐
                    arrive_road_num[j] = arrive_road_num[v];//到达点j的最短路径条数就是到达点v的路径条数
                    pass_city_num[j]=pass_city_num[v]+1;//通过的城市num加1
                    pre[j] = v;//记录路径——把v加进去
                }
                else if (temp == dist[j]) {
                    arrive_road_num[j] += arrive_road_num[v];
                    int _happyiness = happiness[j] + have_happiness[v];
                    int _pass_num = pass_city_num[v] + 1;
                    if (_happyiness > have_happiness[j] || (_happyiness == have_happiness[j] && _pass_num < pass_city_num[j])) {
                        have_happiness[j] = _happyiness;
                        pass_city_num[j] = _pass_num;
                        pre[j] = v;
                    }
                }
            }
        }
    }
}
void Output() {
    int n = city["ROM"];
    cout << arrive_road_num[n] << " "
        << dist[n] << " "
        << have_happiness[n] << " "
        << have_happiness[n] / pass_city_num[n] << endl;
    cout << r_city[0];

    vector<string>path;                                                                     //依次把经过的城市的名字保存到数组里 
    for (int t = city["ROM"]; t != -1; t = pre[t]) {
        path.push_back(r_city[t]);
    }

    for (int i = path.size() - 2; i >= 0; i--) {//因为最后一个是ROM,但要保证输出统一格式
        cout << "->" << path[i];                                    //所以从倒数第二开始
    }
    cout << endl;
}
int main() {
    Input();
    Dijikstra();
    Output();
    return 0;
}

“`

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值