| 试题编号: | 201409-4 |
| 试题名称: | 最优配餐 |
| 时间限制: | 1.0s |
| 内存限制: | 256.0MB |
| 问题描述: | 问题描述 栋栋最近开了一家餐饮连锁店,提供外卖服务。随着连锁店越来越多,怎么合理的给客户送餐成为了一个急需解决的问题。 栋栋的连锁店所在的区域可以看成是一个n×n的方格图(如下图所示),方格的格点上的位置上可能包含栋栋的分店(绿色标注)或者客户(蓝色标注),有一些格点是不能经过的(红色标注)。 方格图中的线表示可以行走的道路,相邻两个格点的距离为1。栋栋要送餐必须走可以行走的道路,而且不能经过红色标注的点。 |
解题代码(java):
import java.util.LinkedList;import java.util.Queue;import java.util.Scanner;public class Main { static class Vertex implements Cloneable { public int x; public int y; public int step; public Vertex(int x, int y, int step) { this.x = x; this.y = y; this.step = step; } public Vertex() { } } public static void main(String[] args) { long[][] map = new long[1001][1001]; Queue<Vertex> q = new LinkedList<Vertex>(); boolean[][] vis = new boolean[1001][1001]; int[][] move = { { 0, 1 }, { 0, -1 }, { 1, 0 }, { -1, 0 } }; Scanner in = new Scanner(System.in); long size = in.nextLong(); long m = in.nextLong(); long k = in.nextLong(); long d = in.nextLong(); for (int i = 0; i < m; i++) { int x = in.nextInt(); int y = in.nextInt(); int step = 0; q.add(new Vertex(x, y, step)); } for (int i = 0; i < k; i++) { int x = in.nextInt(); int y = in.nextInt(); int z = in.nextInt(); map[x][y] = z; } for (int i = 0; i < d; i++) { int x = in.nextInt(); int y = in.nextInt(); vis[x][y] = true; } in.close(); long cnt = 0; long ans = 0; while (!q.isEmpty()) { Vertex u = q.remove(); for (int i = 0; i < 4; i++) { Vertex tem = new Vertex(); tem.x = u.x; tem.y = u.y; tem.step = u.step; tem.x += move[i][0]; tem.y += move[i][1]; tem.step++; if (tem.x > 0 && tem.y <= size && tem.y > 0 && tem.x <= size && !vis[tem.x][tem.y]) { vis[tem.x][tem.y] = true; if (map[tem.x][tem.y] != 0) { ans += map[tem.x][tem.y] * tem.step; ++cnt; if (cnt == k) break; } q.add(tem); } } } System.out.PRintln(ans); }}得分80 ,欢迎指正
新闻热点
疑难解答