HDU 3499 Flight(SPFA + dp)

题目

Recently, Shua Shua had a big quarrel with his GF. He is so upset that he decides to take a trip to some other city to avoid meeting her. He will travel only by air and he can go to any city if there exists a flight and it can help him reduce the total cost to the destination. There’s a problem here: Shua Shua has a special credit card which can reduce half the price of a ticket ( i.e. 100 becomes 50, 99 becomes 49. The original and reduced price are both integers. ). But he can only use it once. He has no idea which flight he should choose to use the card to make the total cost least. Can you help him?

Input
There are no more than 10 test cases. Subsequent test cases are separated by a blank line.
The first line of each test case contains two integers N and M ( 2 <= N <= 100,000
0 <= M <= 500,000 ), representing the number of cities and flights. Each of the following M lines contains “X Y D” representing a flight from city X to city Y with ticket price D ( 1 <= D <= 100,000 ). Notice that not all of the cities will appear in the list! The last line contains “S E” representing the start and end city. X, Y, S, E are all strings consisting of at most 10 alphanumeric characters.

Output
One line for each test case the least money Shua Shua have to pay. If it’s impossible for him to finish the trip, just output -1.

Sample Input

4 4
Harbin Beijing 500
Harbin Shanghai 1000
Beijing Chengdu 600
Shanghai Chengdu 400
Harbin Chengdu
4 0
Harbin Chengdu

Sample Output

800
-1

解析

题目大意:就是给你一些城市,和这些城市之间的航班的价格,Shua Shua将会从一个地点到另一个地点的最小花费,典型的最短路径问题!但是这题有个额外的条件,就是她有一次打折的机会,在这个条件上进行求最小值。简称为带条件约数的最短路径问题。
思路: 对于这个打折条件,我们可以得出对于每一次坐飞机都可以有打折和不打折的情况,故而属于动态规划问题,即这次状态的最优值依赖于上一个状态的解。这里假设dp[city][0]表示不打折到city的最优解,dp[city][1]表示打折到city的最优解。显然有如下方程:

d p [ i ] [ 0 ] = m i n { d p [ i ] [ 0 ] , d p [ i 1 ] [ 0 ] + c o s t [ i ] }

d p [ i ] [ 1 ] = m i n { d p [ i ] [ 1 ] , d p [ i 1 ] [ 0 ] + c o s t [ i ] / 2 , d p [ i 1 ] [ 1 ] + c o s t [ i ] }

最短路径算法选择

此题目是单源路径最短问题,故而可以有迪杰斯克拉算法和SPFA算法。这里使用的SPFA算法。

SPFA基本步骤:
1. 初始化一个队列,一个访问标记数组
2. 源点进队列
3. 从队列中取一个点,对该点的所有的邻接点进行松弛操作,若更新到该邻接点的距离,则判断该点是否在队列中,若不在,则把点放到队列中。
4. 重复3的步骤,直到队列为空。

由于我们的距离更新记录是分打折和不打折的情况,分别记录更新,所以互不影响。

哈希记录顶点序号

利用哈希记录顶点序号,把字符串映射为整型,方便我们用数组。

代码

package com.special.HDU;

import java.util.*;

/**
 * Created by Special on 2018/6/5 10:25
 */
public class HDU3499 {
    static class Node{
        int id;
        int dis;
        public Node(int id, int dis){
            this.id = id;
            this.dis = dis;
        }
    }
    static int n, m, cnt, s, e;
    static Map<String, Integer> indicies;
    static List<Node>[] adj;
    static final long MAX = Long.MAX_VALUE;
    static long[][] dis;

    static void spfa(int s){
        Queue<Integer> queue = new LinkedList<>();
        boolean[] vis = new boolean[n];
        for(int i = 0; i < n; i++){
            dis[i][0] = dis[i][1] = MAX;
        }
        queue.offer(s);
        dis[s][0] = dis[s][1] = 0; //关键
        while(!queue.isEmpty()){
            int t = queue.poll();
            vis[t] = false;
            List<Node> nodes = adj[t];
            if(nodes != null){
                for(int i = 0; i < nodes.size(); i++){
                    Node node = nodes.get(i);
                    boolean flag = false;
                    if(dis[node.id][0] > dis[t][0] + node.dis) {
                        flag = true;
                        dis[node.id][0] = dis[t][0] + node.dis;
                    }
                    if(dis[node.id][1] > dis[t][1] + node.dis
                            || dis[node.id][1] > dis[t][0] + node.dis / 2){
                        flag = true;
                        dis[node.id][1] = Math.min(dis[t][1] + node.dis, dis[t][0] + node.dis / 2);
                    }
                    if(flag && !vis[node.id]){
                        vis[node.id] = true;
                        queue.offer(node.id);
                    }
                }
            }
        }
    }

    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        while(input.hasNext()){
            n = input.nextInt();
            m = input.nextInt();
            cnt = 0;
            indicies = new HashMap<>();
            adj = new ArrayList[n];
            dis = new long[n][2];
            for(int i = 0; i < n; i++){
                adj[i] = new ArrayList<>();
            }
            String src, drc;
            while(m-- > 0){
                int dis;
                src = input.next();
                drc = input.next();
                dis = input.nextInt();
                if(!indicies.containsKey(src)){
                    indicies.put(src, cnt++);
                }
                if(!indicies.containsKey(drc)){
                    indicies.put(drc, cnt++);
                }
                adj[indicies.get(src)].add(new Node(indicies.get(drc), dis));
            }
            src = input.next();
            drc = input.next();
            //以下两步也是坑
            if (!indicies.containsKey(src)) {
                indicies.put(src, cnt++);
            }
            if (!indicies.containsKey(drc)) {
                indicies.put(drc, cnt++);
            }
            s = indicies.get(src);
            e = indicies.get(drc);
            spfa(s);
            System.out.println((dis[e][1] == MAX ? -1 : dis[e][1]));
        }
    }
}

猜你喜欢

转载自blog.csdn.net/dawn_after_dark/article/details/80578809