csp 2014_9_4 最优配餐
来源:互联网 发布:ubuntu虚拟机nat联网 编辑:程序博客网 时间:2024/05/22 11:36
注意 变量的范围 要取long类型
题目链接
package csp2014_9_4;import java.util.Arrays;import java.util.LinkedList;import java.util.List;import java.util.Scanner;import org.w3c.dom.css.ViewCSS;public class Main {long[][]map;boolean [][]visit;int n, m, k, d;int [][]dir = {{0,1},{0,-1},{1,0},{-1,0}};long ans;LinkedList<Node> que;public static void main(String[] args) {new Main().run();}public void run() {Scanner in = new Scanner(System.in);while(in.hasNext()) {n = in.nextInt();m = in.nextInt();k = in.nextInt();d = in.nextInt();que = new LinkedList<>();visit = new boolean[n+1][n+1];map = new long[n+1][n+1];for(int i = 0; i < m; i++ ) {int x = in.nextInt();int y = in.nextInt();que.offer(new Node(x, y, 0));}for(int i = 0; i < k; i++ ) {int x = in.nextInt();int y = in.nextInt();long num = in.nextInt();map[x][y] = num; }for(int i = 0; i < d; i++ ) {int x = in.nextInt();int y = in.nextInt();visit[x][y] = true;}ans = 0;bfs();System.out.println(ans);}}public void bfs() {int cnt = 0;while(!que.isEmpty()) {Node node = que.poll();for(int i = 0; i < 4; i++ ) {int xx = node.x + dir[i][0];int yy = node.y + dir[i][1];if(xx>0&&yy>0&&xx<=n&&yy<=n&&!visit[xx][yy]) {visit[xx][yy] = true;ans += (node.step+1)*map[xx][yy];Node tmp = new Node(xx, yy, node.step+1);que.offer(tmp);}}}}}class Node{int x, y;long mum;long step;public Node(int x, int y, long s) {step = s;this.x = x;this.y = y;}}
上面 可以在达到目标后提前终止
if(map[xx][yy] != 0) {ans += (node.step+1)*map[xx][yy];cnt++;if(cnt == k)return;}
阅读全文
0 0
- csp 2014_9_4 最优配餐
- CSP 最优配餐
- CCF CSP 最优配餐 BFS
- CCF CSP 2014年9月第4题 最优配餐(多源多汇BFS)
- CCF-CSP 201409-4 最优配餐(BFS)
- CCF CSP 最优配餐 JAVA 201409_4 90分
- 最优配餐
- 最优配餐
- CSP 2017_9_4 通信网络
- 【CCF】最优配餐
- ccf 最优配餐
- CCF 最优配餐
- CCF最优配餐
- 最优配餐-CCF
- CCF201409-4最优配餐
- ccf 最优配餐
- CCF 最优配餐
- 201409-4 最优配餐
- JavaApi操作Hadoop
- 人工智能之Python语言视频课程
- Android应用程序App应用上线流程
- react-router 4.2.0<link>页面跳转-传参与不传参
- 压缩版JS的调试利器JavaScript Source Map基本介绍
- csp 2014_9_4 最优配餐
- Spark中groupByKey与reduceByKey算子之间的区别
- 蓝桥进制转换题集
- Python爬虫
- Log4j实例简单配置
- springboot下及其方便的使用缓存
- 算法-循环链表[约瑟夫问题]
- 内存初始化-----memset函数的使用
- 计算机语言发展史