1002 All Roads Lead to Rome

题目描述
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

package p1002;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Scanner;

public class Main_dfs {

    static Map<String, Integer> happiness = new HashMap<String, Integer>();
    static Map<String, Integer> cost = new HashMap<String, Integer>();
    static List<String> visited = new ArrayList<String>();

    static int min_cost = Integer.MAX_VALUE, cur_cost = 0, max_happiness = 0, cur_happiness = 0;
    static int same_cost = 0;

    static List<String> path;

    public static void main(String[] args) {

        Scanner sc = new Scanner(System.in);

        int cities = sc.nextInt();
        int roads  = sc.nextInt();
        String start  = sc.next();

        for(int i=0; i<cities-1; i++) {
            happiness.put(sc.next(), sc.nextInt());
        }

        for(int i=0; i<roads; i++) {
            String c1 = sc.next();
            String c2 = sc.next();
            int c = sc.nextInt();
            cost.put(c1 + c2, c);
            cost.put(c2 + c1, c);
        }

        visited.add(start);
        dfs(start);

        System.out.println(same_cost + " " + min_cost + " " + max_happiness + " " + max_happiness / (path.size()-1));
        for(int i=0; i<path.size()-1; i++)
            System.out.print(path.get(i) + "->");
        System.out.println(path.get(path.size()-1));
    }

    public static void dfs(String start) {

        if("ROM".equals(start)) {
            if(cur_cost < min_cost) {
                path = new ArrayList<String>(visited);
                min_cost = cur_cost;
                max_happiness = cur_happiness;
                same_cost = 1;

            } else if(cur_cost == min_cost) {
                if(cur_happiness > max_happiness || (cur_happiness == max_happiness && visited.size() < path.size())) {
                    max_happiness = cur_happiness;
                    path = new ArrayList<String>(visited);
                }
                same_cost ++;

            }
            return;
        }

        for(String next : happiness.keySet()) {
            if(!visited.contains(next) && cost.get(start+next) != null) {
                cur_cost += cost.get(start+next);
                cur_happiness += happiness.get(next);
                visited.add(next);
                dfs(next);
                cur_cost -= cost.get(start+next);
                cur_happiness -= happiness.get(next);
                visited.remove(next);
            }
        }
    }
}
package p1002;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;

public class CopyOfMain {

    static int min_cost = Integer.MAX_VALUE, cur_cost = 0, max_happiness = 0, cur_happiness = 0;
    static int same_cost = 0;
    static ArrayList<Integer> path = new ArrayList<Integer>(), temp_path = new ArrayList<Integer>(); 
    static ArrayList<Integer> all_costs = new ArrayList<Integer>(), all_happiness = new ArrayList<Integer>();
    static ArrayList<ArrayList<Integer>> all_paths = new ArrayList<ArrayList<Integer>>();

    public static void main(String[] args) {

        Scanner sc = new Scanner(System.in);

        int cities = sc.nextInt();
        int roads  = sc.nextInt();
        String start  = sc.next();

        Map<String, Integer> map = new HashMap<String, Integer>();
        String[] map2 = new String[cities];
        map.put(start, 0);

        // 到每个城市的哈皮值
        int[] happiness = new int[cities];
        for(int i=1; i<cities; i++) {
            String city = sc.next();
            map.put(city, i);
            map2[i] = city;
            happiness[i] = sc.nextInt();
        }

        // 费用
        int[][] costs = new int[cities][cities];
        for(int i=0; i<cities; i++)
            for(int j=0; j<cities; j++)
                costs[i][j] = Integer.MAX_VALUE;
        for(int i=0; i<roads; i++) {
            int city1 = map.get(sc.next());
            int city2 = map.get(sc.next());
            int cost = sc.nextInt();
            costs[city1][city2] = cost;
            costs[city2][city1] = cost;
        }



        // Dijkstra algorithm

        int[] dist = new int[cities];
        for(int i=0; i<cities; i++)
            dist[i] = costs[0][i];
        if(dist[map.get("ROM")] != Integer.MAX_VALUE) {
            all_costs.add(dist[map.get("ROM")]);
            all_happiness.add(happiness[map.get("ROM")]);
            temp_path.add(map.get("ROM"));
            all_paths.add(new ArrayList<Integer>(temp_path));
        }

        int[] prev = new int[cities];

        boolean[] flag = new boolean[cities];

        flag[0] = true;
        dist[0] = 0;

        int k = 0;
        for(int i=1; i<cities; i++) {

            int min = Integer.MAX_VALUE;
            for(int j=0; j<cities; j++) {
                if(flag[j] == false && dist[j] < min) {
                    min = dist[j];
                    k = j;
                }
            }

            flag[k] = true;
//          if(k == map.get("ROM"))
//              break;

            // 更新的时候要记录一下到达ROM时的costs,在找出最小的有几个
            for(int j=0; j<cities; j++) {

                int temp = 0;

                temp = (costs[k][j] == Integer.MAX_VALUE ? Integer.MAX_VALUE : (min + costs[k][j]));
                if(flag[j] == false && temp <= dist[j]) {
                    dist[j] = temp;
                    prev[j] = k;
                }

                if(j == map.get("ROM") && temp <= dist[j]) {
                    temp_path.clear();
                    cur_cost = 0;
                    cur_happiness = 0;
                    temp = j;
                    while(temp != 0) {
                        cur_cost += costs[prev[temp]][temp];
                        cur_happiness += happiness[temp];
                        temp_path.add(temp);
                        temp = prev[temp];
                    }

                    all_costs.add(cur_cost);
                    all_happiness.add(cur_happiness);
                    all_paths.add(new ArrayList<Integer>(temp_path));
                }

            }
        }

        for(int i=0; i<all_costs.size(); i++)
            if(all_costs.get(i) == dist[map.get("ROM")]) {
                same_cost ++;
                if(all_happiness.get(i) > max_happiness || (all_happiness.get(i) == max_happiness && all_paths.get(i).size() < path.size())) {
                    max_happiness = all_happiness.get(i);
                    path = all_paths.get(i);
                }
            }


            System.out.println(same_cost + " " + dist[map.get("ROM")] + " " + max_happiness + " " + max_happiness / path.size());
            System.out.print(start);
            for(int i=path.size()-1; i>=0; i--)
                System.out.print("->" + map2[path.get(i)]);
    }
}

第一次用dfs写的,超时了,改用Dijkstra算法,但是一直没能通过所有的测试用例

Dijkstra算法有几个地方需要注意:
(1)

temp = (costs[k][j] == Integer.MAX_VALUE ? Integer.MAX_VALUE : (min + costs[k][j]));
                if(flag[j] == false && temp <= dist[j]) {
                    dist[j] = temp;
                    prev[j] = k;
                }

因为要记录路径,所以temp <= dist[j] 要写成 <=,不然会漏掉

(2)

if(dist[map.get("ROM")] != Integer.MAX_VALUE) {
            all_costs.add(dist[map.get("ROM")]);
            all_happiness.add(happiness[map.get("ROM")]);
            temp_path.add(map.get("ROM"));
            all_paths.add(new ArrayList<Integer>(temp_path));
        }

最开始对dist初始化时也要把可能一步达到目的地的情况考虑进去

(3)最好不要改动原来的Dijkstra算法,在原始代码上进行一些附加操作就可以了

虽然我没有通过所有用例。。。

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值