美文网首页
All Roads Lead to Rome

All Roads Lead to Rome

作者: 今天也要努力呀y | 来源:发表于2019-11-23 11:56 被阅读0次

    题目大意:
    你应该找到最小花费且最多快乐的一条路径

    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.

    输入:
    第一行两个正数:
    N,城市的数量
    K,道路的数量
    接下来是起始城市
    接下来的N-1行每一行给出了城市的名字和一个数代表你可以获得的快乐值(不包括起始城镇)
    接下来K行,每一个代表两城镇之间的cost
    三个字母代表一个城市,终点为ROM,代表罗马

    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.

    你要找到最小cost的路径,相同cost的情况下找到最大快乐值的路径,如果还是不止一条,输出一条最大平均快乐值的
    输出4个值:
    有多少条最小cost的路径
    最好的一条路径的cost,happiness,average happiness
    路径

    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
    

    代码:

    import java.util.HashMap;
    import java.util.Scanner;
    public class Main {
        public static void main(String[] args) {
            Scanner in = new Scanner(System.in);
            int n = in.nextInt();//numbers of city
            int k = in.nextInt();//numbers of routes
            String start = in.next();//start city
            int[] costs = new int[n];//每一个点到顶点的cost
            int[] hpns = new int[n];//每一个点到顶点的happiness值
            int[] steps = new int[n];//每一条路径的节点数,不包括头节点
            int[] routes = new int[n];//到达每个点的路由数量
            int[] parent = new int[n];//每一个结点的父节点
            int[][] M = new int[n][n];//邻接矩阵
            HashMap<String, Integer> index = new HashMap<String, Integer>();//记录每个城市的下标
            boolean[] visited = new boolean[n];//标记已经访问的结点
            int[] h = new int[n];//每个城市的幸福值
            String[] names = new String[n];//每个城市的名称
            routes[0] = 1;
            parent[0] = -1;
            index.put(start, 0);
            names[0] = start;
            //初始化
            for(int i = 1;i<n;i++){
                costs[i] = Integer.MAX_VALUE;
                hpns[i] = Integer.MIN_VALUE;
                names[i] = in.next();
                index.put(names[i], i);
                h[i] = in.nextInt();
            }
            int end = index.get("ROM");
             
            for(int i = 0;i<k;i++){
                int s = index.get(in.next());
                int e = index.get(in.next());
                int cost = in.nextInt();
                M[s][e] = cost;
                M[e][s] = cost;
            }
            for(int t = 0;t<n;t++){
                int v = -1;
                for(int i = 0;i<n;i++){
                    if (!visited[i] && ((v < 0) || (costs[i] < costs[v])))
                        v = i;
                }
                visited[v] = true;
                for(int i = 0;i<n;i++){
                    if(!visited[i]&&M[v][i]!=0){
                        int cost = costs[v] + M[v][i];
                        int happy = hpns[v] + h[i];
                        int step = steps[v] + 1;
                        boolean flag = false;
                        if(cost<costs[i]){
                            costs[i] = cost;
                            routes[i] = routes[v];
                            flag = true;
                        }else if(cost==costs[i]){
                            routes[i] += routes[v];
                            if(happy>hpns[i]){
                                flag = true;
                            }else if(happy==hpns[i] && step<steps[i]){
                                flag = true;
                            }
                        }
                        if(flag){
                            costs[i] = cost;
                            hpns[i] = happy;
                            steps[i] = step;
                            parent[i] = v;
                        }
                    }
                }
            }
            int total = steps[end];
            int happiness = hpns[end];
            int avg = happiness/total;
            total++;
            System.out.println(routes[end]+" "+costs[end]+" "+happiness+" "+avg);
            String[] result = new String[total];
            int j = total-1;
            while(end!=-1){
                result[j--] = names[end];
                end = parent[end];
            }
            for(int i = 0;i<result.length-1;i++){
                System.out.print(result[i]+"->");
            }
            System.out.println(result[total-1]);
        }
    }
    

    相关文章

      网友评论

          本文标题:All Roads Lead to Rome

          本文链接:https://www.haomeiwen.com/subject/cyuhmctx.html