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 recommanded. 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 recommanded 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

思路:

用dijkstra 求边权最短值,点权最大值,求出最短路径的条数,最短路径的结点个数, 保存最短路径。
6个考点吧。比较综合。

回忆最短路, 调了一小h..

第一次代码

#include<bits/stdc++.h>
using namespace std;
const int inf = 0x3f3f3f3f;
const int maxsize = 205;
int n, m, cnt = 0;
int edge[maxsize][maxsize], dis[maxsize], w[maxsize], num[maxsize], pre[maxsize], pathNum[maxsize], happys[maxsize];
string sname[maxsize];
bool vis[maxsize];
unordered_map<string, int> ma;
int dijkstra(int s) {
    fill(dis, dis + n, inf); //
    dis[s] = 0, num[s] = 1, pre[s] = -1, pathNum[s] = 1;
    for(int i = 0; i < n; i++) {
        int mins = inf, pos = -1;
        for(int j = 0; j < n; j++) {
            if(vis[j] != true && dis[j] < mins) {
               mins = dis[j];
               pos = j;
            }
        }
        if(pos == -1) return -1;
        vis[pos] = true;
        for(int j = 0; j < n; j++) {
            if(!vis[j] && edge[pos][j] != inf) {
                if(edge[pos][j] + dis[pos] < dis[j]) {
                    dis[j] = edge[pos][j] + dis[pos];
                    pre[j] = pos;
                    pathNum[j] = pathNum[pos];
                    num[j] = num[pos] + 1;
                    w[j] = w[pos] + happys[j];
                } else if(edge[pos][j] + dis[pos] == dis[j]) {
                    pathNum[j] = pathNum[pos] + pathNum[j]; //必须移到外面来
                    if(w[pos] + happys[j] > w[j]) {
                        pre[j] = pos;
                        num[j] = num[pos] + 1;
                        w[j] = w[pos] + happys[j];
                    } else if(w[pos] + happys[j] == w[j]){
                        if(num[pos] + 1 < num[j]) {
                            pre[j] = pos;
                            num[j] = num[pos] + 1;
                        }
                    }
                }
            }
        }

    }
}
int main()
{
    scanf("%d%d", &n, &m);
    fill(edge[0], edge[0] + maxsize * maxsize, inf); //
    string name;
    cin >> name;
    sname[cnt] = name;
    ma[name] = cnt++;
    int happy = -1;
    for(int i = 0; i < n - 1; i++) {
        cin >> name >> happy;
        sname[cnt] = name;
        ma[name] = cnt++;
        happys[ma[name]] = happy;
    }
    string from, to;
    int cost;
    for(int i = 0; i < m; i++) {
        cin >> from >> to >> cost;
        edge[ma[from]][ma[to]] = cost;
        edge[ma[to]][ma[from]] = cost;
    }
    dijkstra(0);
    int end = ma["ROM"];
    cout << pathNum[end] << " "; //
    cout << dis[end] << " ";
    cout << w[end] << " ";
    cout << w[end] / (num[end] - 1) << endl;
    vector<string> res;
    while(pre[end] != -1) {
        res.push_back(sname[end]);
        end = pre[end];
    }
    res.push_back(sname[0]);
    for(auto i = res.rbegin(); i != res.rend(); i++) { //
        printf("%s", i != res.rbegin() ? "->" : "");
        cout << *i;
    }
    return 0;
}

第二次 20min

#include<bits/stdc++.h>
using namespace std;
const int inf = 0x3f3f3f3f;
const int maxsize = 205;
int n, m, edge[maxsize][maxsize], cnt = 0;
int dis[maxsize], path[maxsize], pre[maxsize], w[maxsize], num[maxsize], cost[maxsize];
bool vis[maxsize];
unordered_map<string, int> ma;
vector<string> sname;
vector<int> res;
void dfs(int end) {
    if(end == -1) {
        return;
    }
    dfs(pre[end]);
    res.push_back(end);
}
int dijkstra(int start) {
    dis[start] = 0;
    num[start] = 1, path[start] = 1, pre[start] = -1;
    for(int i = 0; i < n; i++) {
        int mins = inf, p = -1;
        for(int j = 0; j < n; j++) {
            if(mins > dis[j] && !vis[j]) {
                mins = dis[j];
                p = j;
            }
        }
        if(p == -1) return -1;
        vis[p] = true;
        for(int j = 0; j < n; j++) {
            if(edge[p][j] != inf && !vis[j]) {
                if(dis[p] + edge[p][j] < dis[j]) {
                    dis[j] = dis[p] + edge[p][j];
                    num[j] = num[p] + 1;
                    path[j] = path[p];
                    pre[j]  = p;
                    w[j] = w[p] + cost[j];
                } else if(dis[p] + edge[p][j] == dis[j]) {
                    path[j] = path[p] + path[j];
                    if(w[j] < w[p] + cost[j]) {
                        w[j] = w[p] + cost[j];
                        pre[j] = p;
                        num[j] = num[p] + 1;
                    } else if(w[j] == w[p] + cost[j]) {
                        if(num[j] > num[p] + 1) {
                            pre[j] = p;
                            num[j] = num[p] + 1;
                        }
                    }
                }
            }
        }
    }
}
int main() {
    fill(edge[0], edge[0] + maxsize * maxsize, inf);
    int happy;
    scanf("%d%d", &n, &m);
    fill(dis, dis + n, inf);
    string name;
    cin >> name;
    sname.push_back(name);
    ma[name] = cnt++;
    for(int i = 0; i < n - 1; i++) {
        cin >> name;
        scanf("%d", &happy);
        sname.push_back(name);
        cost[cnt] = happy;
        ma[name] = cnt++;
    }
    string from, to;
    int val;
    for(int i = 0; i < m; i++) {
        cin >> from >> to >> val;
        edge[ma[from]][ma[to]] = edge[ma[to]][ma[from]] = min(val, edge[ma[from]][ma[to]]);
    }
    dijkstra(0);
    int end = ma["ROM"];
    printf("%d %d %d %d\n", path[end], dis[end], w[end], w[end] / (num[end] - 1));
    dfs(end);
    for(int i = 0; i < res.size(); i++) {
        printf("%s%s", i == 0 ? "" : "->", sname[res[i]].c_str());
    }
    return 0;
}

猜你喜欢

转载自www.cnblogs.com/csyxdh/p/12466118.html
今日推荐