[Graph Theory] C_Counting Competition_On the Elevator (two-dimensional abstract one-dimensional)

1. Title Description

After the power of the elevator was turned on, the expedition members entered the vertical tunnel where the elevator was running. What was in sight was a track leading to the top of the tower, an elevator parked at the bottom of the track, and a rod The huge handle that controls the elevator.

The Nescafé tower has N floors, and the elevator has a stop on each floor.

The handle has M control slots, a number Ci is marked beside the i th control slot, satisfying C1 <C2 <C3 <... <CM.

If Ci> 0, it means that when the handle is moved to this slot, the elevator will rise up to the Ci floor; if Ci <0, it means that when the handle is moved to this slot, the elevator will fall to −Ci floor; It was originally in this slot.

Note that the elevator can only move between the 1st and Nth floors, so it is not allowed to pull the control slot to move the elevator below the first floor and above the Nth floor.

It takes 2 seconds for each elevator to move one floor, and it takes 1 second for the handle to move from one control slot to the adjacent slot.

The expedition team members are now on level 1, and want to reach level N as soon as possible. They want to know how long it will take at least level 1 to level N?

Input format

Two positive integers N, M in the first row.
M integers C1, C2 ... CM in the second line.

Output format

The output of an integer indicates the answer, that is, how long it takes at least.
If it is impossible to reach output -1.

data range

1≤N≤1000,
2≤M≤20,
−N<C1<C2<…<CM<N

输入样例:
6 3
-1 0 2
输出样例:
19

Sample explanation

The handle moves from the second slot to the third slot (0 to 2), which takes 1 second, and the elevator goes up to the third floor in 4 seconds.
The handle does not move in the third slot, and the elevator goes up to the fifth floor again in 4 seconds.
Move the handle to the first slot (2 to -1), it takes 2 seconds, the elevator descends to the 4th floor, it takes 2 seconds.
Move the handle to the third slot (-1 pulls down 2), it takes 2 seconds, the elevator goes up to the 6th floor, it takes 4 seconds.
The total time is (1 + 4) +4+ (2 + 2) + (2 + 4) = 19 seconds.

Second, the solution

Method 1: Examine the map

At first glance, it looks like dp. Yes, dp can be done. A relatively new idea is to turn the problem into a graph theory shortest problem:

  • Node 1 is the source point and n is the end point.
  • C [ i ] C[i] originally meant that the elevator will move C i C_i Layer, and in this algorithm I express it as the edge weight of a directed edge.
  • Each control slot C [ i ] C[i] represents different directed edges, and each floor elevator is a node in the figure.
  • Since the edge weight may be negative, there may be an infinite cyclic movement.
  • So far, this problem can be simplified to find the minimum time from point 1 to point n.
import java.util.*;
import java.math.*;
import java.io.*;
public class Main{
	static int n, m; 
	static int[] C;
	static int[][] dist;
	static int INF = 0x3f3f3f3f;
	
	static void spfa(int S) {
	    for (int i = 1; i <= n; i++)
	    for (int j = 1; j <= m; j++) {
	        dist[i][j] = INF;
	    }
		Queue<Node> q = new ArrayDeque<>();
		boolean[][] inq = new boolean[n+1][m+1];
		q.add(new Node(1, S));
		inq[1][S] = true;
		dist[1][S] = 0;
		
		while (!q.isEmpty()) {
			Node t = q.poll();
			int f = t.f, s = t.s;
			inq[f][s] = false;
			for (int i = 1; i <= m; i++) {
				int tf = f + C[i];
				if (tf < 1 || tf > n)
					continue;
				if (dist[tf][i] > dist[f][s] + Math.abs(f-tf)*2 + Math.abs(s-i)) {
					dist[tf][i] = dist[f][s] + Math.abs(f-tf)*2 + Math.abs(s-i);
					if (!inq[tf][i]) {
						q.add(new Node(tf, i));
						inq[tf][i] = true;
					}
				}
			}
		}
	}
    public static void main(String[] args) throws IOException {  
        Scanner sc = new Scanner(new BufferedInputStream(System.in));
        BufferedWriter w = new BufferedWriter(new OutputStreamWriter(System.out));
        n = sc.nextInt();
		m = sc.nextInt();
		C = new int[m+1];
		
		int start = 0;
		for (int i = 1; i <= m; i++) {
			C[i] = sc.nextInt();
			if (C[i] == 0)
				start = i;
		}
		int min = INF;
		dist = new int[n+1][m+1];
		spfa(start);
		for (int i = 1; i <= m; i++) {
			min = Math.min(min, dist[n][i]);
		}
		System.out.println(min == INF ? -1 : min);
    }
	static class Node {
		int f, s;
		Node(int f, int s) {
			this.f = f;
			this.s = s;
		}
	}
}

Complexity analysis

  • time complexity: O ( k m ) O (km)
  • Space complexity: O ( . . . ) O(...) ,

Method 2: dp

Charge d'affaires ...


Complexity analysis

  • time complexity: O ( ) O()
  • Space complexity: O ( ) O()
Published 714 original articles · praised 199 · 50,000+ views

Guess you like

Origin blog.csdn.net/qq_43539599/article/details/105589208