{"id":213,"date":"2013-07-02T00:44:43","date_gmt":"2013-07-02T04:44:43","guid":{"rendered":"http:\/\/www.ferociouscoder.com\/blog\/?p=213"},"modified":"2013-07-13T02:48:26","modified_gmt":"2013-07-13T06:48:26","slug":"uva-1112-mice-and-maze","status":"publish","type":"post","link":"https:\/\/www.ferociouscoder.com\/blog\/archives\/uva-1112-mice-and-maze-213.html","title":{"rendered":"UVA 1112 &#8211; Mice and Maze"},"content":{"rendered":"<p>This question is part of the Competitive Programming 3 book however I haven&#8217;t quite reached that chapter yet. Let me see how my approach to solving this problem changes once I get there.<\/p>\n<p>Below is the way I would have done it without having reading the book.<\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\nimport java.io.PrintWriter;\r\nimport java.util.ArrayList;\r\nimport java.util.Comparator;\r\nimport java.util.HashMap;\r\nimport java.util.PriorityQueue;\r\nimport java.util.Scanner;\r\n\r\n\/**\r\n * \r\n * @author Sanchit M. Bhatnagar\r\n * @see http:\/\/uhunt.felix-halim.net\/id\/74004\r\n * \r\n *\/\r\npublic class P1112 {\r\n\r\n  public static void main(String&#x5B;] args) {\r\n    Scanner sc = new Scanner(System.in);\r\n    PrintWriter out = new PrintWriter(System.out);\r\n\r\n    int zz = sc.nextInt();\r\n    for (int i = 0; i &amp;lt; zz; i++) {\r\n      if (i != 0)\r\n        out.println();\r\n      int N = sc.nextInt();\r\n      int E = sc.nextInt() - 1;\r\n      int T = sc.nextInt();\r\n      int M = sc.nextInt();\r\n      Node&#x5B;] graph = new Node&#x5B;N];\r\n      for (int j = 0; j &amp;lt; N; j++) {\r\n        graph&#x5B;j] = new Node(j);\r\n      }\r\n      for (int j = 0; j &amp;lt; M; j++) {\r\n        int a = sc.nextInt() - 1;\r\n        int b = sc.nextInt() - 1;\r\n        int c = sc.nextInt();\r\n        graph&#x5B;a].add(graph&#x5B;b], c);\r\n      }\r\n      int&#x5B;] shortest = new int&#x5B;N];\r\n      for (int j = 0; j &amp;lt; N; j++) {\r\n        if (shortest&#x5B;j] == 0) {\r\n          Object&#x5B;] output = dfs(graph, j, E);\r\n          if (output != null) {\r\n            @SuppressWarnings(&amp;quot;unchecked&amp;quot;)\r\n            ArrayList&amp;lt;Integer&amp;gt; tmp = (ArrayList&amp;lt;Integer&amp;gt;) output&#x5B;0];\r\n            int cost = (int) output&#x5B;1];\r\n            int size = tmp.size();\r\n            int thing = 0;\r\n            shortest&#x5B;tmp.get(0)] = cost;\r\n            for (int k = 1; k &amp;lt; size; k++) {\r\n              thing += graph&#x5B;tmp.get(k - 1)].cost.get(tmp.get(k));\r\n              shortest&#x5B;tmp.get(k)] = cost - thing;\r\n            }\r\n          } else {\r\n            shortest&#x5B;j] = -1;\r\n          }\r\n          for (int k = 0; k &amp;lt; N; k++) {\r\n            graph&#x5B;k].visited = false;\r\n          }\r\n        }\r\n      }\r\n      int ans = 0;\r\n      for (int j = 0; j &amp;lt; N; j++) {\r\n        if (shortest&#x5B;j] &amp;lt;= T &amp;amp;&amp;amp; shortest&#x5B;j] != -1) {\r\n          ans++;\r\n        }\r\n      }\r\n      out.println(ans);\r\n    }\r\n    out.close();\r\n    sc.close();\r\n  }\r\n\r\n  @SuppressWarnings(&amp;quot;unchecked&amp;quot;)\r\n  private static Object&#x5B;] dfs(Node&#x5B;] graph, int j, int e) {\r\n    PriorityQueue&amp;lt;Object&#x5B;]&amp;gt; queue = new PriorityQueue&amp;lt;Object&#x5B;]&amp;gt;(1, new Comparator&amp;lt;Object&#x5B;]&amp;gt;() {\r\n      @Override\r\n      public int compare(Object&#x5B;] o1, Object&#x5B;] o2) {\r\n        return Integer.compare((int) o1&#x5B;2], (int) o2&#x5B;2]);\r\n      }\r\n    });\r\n    queue.add(new Object&#x5B;] { graph&#x5B;j], new ArrayList&amp;lt;Integer&amp;gt;(), 0 });\r\n    while (!queue.isEmpty()) {\r\n      Object&#x5B;] next = queue.poll();\r\n      Node n = ((Node) next&#x5B;0]);\r\n      ((ArrayList&amp;lt;Integer&amp;gt;) next&#x5B;1]).add(n.idx);\r\n      n.visited = true;\r\n      if (n.idx == e) {\r\n        return new Object&#x5B;] { next&#x5B;1], next&#x5B;2] };\r\n      } else {\r\n        for (Integer in : n.cost.keySet()) {\r\n          if (!graph&#x5B;in].visited) {\r\n            ArrayList&amp;lt;Integer&amp;gt; tmp = new ArrayList&amp;lt;Integer&amp;gt;();\r\n            tmp.addAll((ArrayList&amp;lt;Integer&amp;gt;) next&#x5B;1]);\r\n            queue.add(new Object&#x5B;] { graph&#x5B;in], tmp, (int) next&#x5B;2] + n.cost.get(in) });\r\n          }\r\n        }\r\n      }\r\n    }\r\n    return null;\r\n  }\r\n\r\n  private static class Node {\r\n    boolean visited;\r\n    int idx;\r\n    HashMap&amp;lt;Integer, Integer&amp;gt; cost;\r\n\r\n    public Node(int idx) {\r\n      this.idx = idx;\r\n      cost = new HashMap&amp;lt;Integer, Integer&amp;gt;();\r\n    }\r\n\r\n    public void add(Node n, int cost) {\r\n      if (this.cost.containsKey(n.idx)) {\r\n        int tmp = this.cost.get(n);\r\n        this.cost.put(n.idx, Math.min(tmp, cost));\r\n      } else {\r\n        this.cost.put(n.idx, cost);\r\n      }\r\n    }\r\n  }\r\n\r\n}\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>This question is part of the Competitive Programming 3 book however I haven&#8217;t quite reached that chapter yet. Let me see how my approach to solving this problem changes once I get there. Below is the way I would have done it without having reading the book. import java.io.PrintWriter; import java.util.ArrayList; import java.util.Comparator; import java.util.HashMap; &hellip; <a href=\"https:\/\/www.ferociouscoder.com\/blog\/archives\/uva-1112-mice-and-maze-213.html\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">UVA 1112 &#8211; Mice and Maze<\/span> <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":true,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[18,19,17],"tags":[21,20,22],"class_list":["post-213","post","type-post","status-publish","format-standard","hentry","category-competitive-programming-3","category-java","category-uva-online-judge","tag-competitive-programming-3-2","tag-java-2","tag-uva-online-judge-2"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.4 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>UVA 1112 - Mice and Maze - Ferocious Coder<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.ferociouscoder.com\/blog\/archives\/uva-1112-mice-and-maze-213.html\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"UVA 1112 - Mice and Maze - Ferocious Coder\" \/>\n<meta property=\"og:description\" content=\"This question is part of the Competitive Programming 3 book however I haven&#8217;t quite reached that chapter yet. Let me see how my approach to solving this problem changes once I get there. Below is the way I would have done it without having reading the book. import java.io.PrintWriter; import java.util.ArrayList; import java.util.Comparator; import java.util.HashMap; &hellip; Continue reading UVA 1112 &#8211; Mice and Maze &rarr;\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.ferociouscoder.com\/blog\/archives\/uva-1112-mice-and-maze-213.html\" \/>\n<meta property=\"og:site_name\" content=\"Ferocious Coder\" \/>\n<meta property=\"article:published_time\" content=\"2013-07-02T04:44:43+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2013-07-13T06:48:26+00:00\" \/>\n<meta name=\"author\" content=\"Rawrosaur\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Rawrosaur\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"3 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/www.ferociouscoder.com\\\/blog\\\/archives\\\/uva-1112-mice-and-maze-213.html#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.ferociouscoder.com\\\/blog\\\/archives\\\/uva-1112-mice-and-maze-213.html\"},\"author\":{\"name\":\"Rawrosaur\",\"@id\":\"https:\\\/\\\/www.ferociouscoder.com\\\/blog\\\/#\\\/schema\\\/person\\\/1fb5cbee546cffd619a7b301e3dc447a\"},\"headline\":\"UVA 1112 &#8211; Mice and Maze\",\"datePublished\":\"2013-07-02T04:44:43+00:00\",\"dateModified\":\"2013-07-13T06:48:26+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/www.ferociouscoder.com\\\/blog\\\/archives\\\/uva-1112-mice-and-maze-213.html\"},\"wordCount\":534,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\\\/\\\/www.ferociouscoder.com\\\/blog\\\/#\\\/schema\\\/person\\\/1fb5cbee546cffd619a7b301e3dc447a\"},\"keywords\":[\"competitive programming 3\",\"java\",\"uva online judge\"],\"articleSection\":[\"Competitive Programming 3\",\"Java\",\"UVA Online Judge\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\\\/\\\/www.ferociouscoder.com\\\/blog\\\/archives\\\/uva-1112-mice-and-maze-213.html#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/www.ferociouscoder.com\\\/blog\\\/archives\\\/uva-1112-mice-and-maze-213.html\",\"url\":\"https:\\\/\\\/www.ferociouscoder.com\\\/blog\\\/archives\\\/uva-1112-mice-and-maze-213.html\",\"name\":\"UVA 1112 - Mice and Maze - Ferocious Coder\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.ferociouscoder.com\\\/blog\\\/#website\"},\"datePublished\":\"2013-07-02T04:44:43+00:00\",\"dateModified\":\"2013-07-13T06:48:26+00:00\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/www.ferociouscoder.com\\\/blog\\\/archives\\\/uva-1112-mice-and-maze-213.html#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/www.ferociouscoder.com\\\/blog\\\/archives\\\/uva-1112-mice-and-maze-213.html\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/www.ferociouscoder.com\\\/blog\\\/archives\\\/uva-1112-mice-and-maze-213.html#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/www.ferociouscoder.com\\\/blog\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"UVA 1112 &#8211; Mice and Maze\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/www.ferociouscoder.com\\\/blog\\\/#website\",\"url\":\"https:\\\/\\\/www.ferociouscoder.com\\\/blog\\\/\",\"name\":\"Ferocious Coder\",\"description\":\"RAWR!\",\"publisher\":{\"@id\":\"https:\\\/\\\/www.ferociouscoder.com\\\/blog\\\/#\\\/schema\\\/person\\\/1fb5cbee546cffd619a7b301e3dc447a\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/www.ferociouscoder.com\\\/blog\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":[\"Person\",\"Organization\"],\"@id\":\"https:\\\/\\\/www.ferociouscoder.com\\\/blog\\\/#\\\/schema\\\/person\\\/1fb5cbee546cffd619a7b301e3dc447a\",\"name\":\"Rawrosaur\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/1372156bd3b16de375c8727c2c617467bf6ff38f1679a7912f48286349c17e96?s=96&d=retro&r=pg\",\"url\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/1372156bd3b16de375c8727c2c617467bf6ff38f1679a7912f48286349c17e96?s=96&d=retro&r=pg\",\"contentUrl\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/1372156bd3b16de375c8727c2c617467bf6ff38f1679a7912f48286349c17e96?s=96&d=retro&r=pg\",\"caption\":\"Rawrosaur\"},\"logo\":{\"@id\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/1372156bd3b16de375c8727c2c617467bf6ff38f1679a7912f48286349c17e96?s=96&d=retro&r=pg\"},\"sameAs\":[\"http:\\\/\\\/www.ferociouscoder.com\\\/\"],\"url\":\"https:\\\/\\\/www.ferociouscoder.com\\\/blog\\\/archives\\\/author\\\/admin\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"UVA 1112 - Mice and Maze - Ferocious Coder","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.ferociouscoder.com\/blog\/archives\/uva-1112-mice-and-maze-213.html","og_locale":"en_US","og_type":"article","og_title":"UVA 1112 - Mice and Maze - Ferocious Coder","og_description":"This question is part of the Competitive Programming 3 book however I haven&#8217;t quite reached that chapter yet. Let me see how my approach to solving this problem changes once I get there. Below is the way I would have done it without having reading the book. import java.io.PrintWriter; import java.util.ArrayList; import java.util.Comparator; import java.util.HashMap; &hellip; Continue reading UVA 1112 &#8211; Mice and Maze &rarr;","og_url":"https:\/\/www.ferociouscoder.com\/blog\/archives\/uva-1112-mice-and-maze-213.html","og_site_name":"Ferocious Coder","article_published_time":"2013-07-02T04:44:43+00:00","article_modified_time":"2013-07-13T06:48:26+00:00","author":"Rawrosaur","twitter_misc":{"Written by":"Rawrosaur","Est. reading time":"3 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.ferociouscoder.com\/blog\/archives\/uva-1112-mice-and-maze-213.html#article","isPartOf":{"@id":"https:\/\/www.ferociouscoder.com\/blog\/archives\/uva-1112-mice-and-maze-213.html"},"author":{"name":"Rawrosaur","@id":"https:\/\/www.ferociouscoder.com\/blog\/#\/schema\/person\/1fb5cbee546cffd619a7b301e3dc447a"},"headline":"UVA 1112 &#8211; Mice and Maze","datePublished":"2013-07-02T04:44:43+00:00","dateModified":"2013-07-13T06:48:26+00:00","mainEntityOfPage":{"@id":"https:\/\/www.ferociouscoder.com\/blog\/archives\/uva-1112-mice-and-maze-213.html"},"wordCount":534,"commentCount":0,"publisher":{"@id":"https:\/\/www.ferociouscoder.com\/blog\/#\/schema\/person\/1fb5cbee546cffd619a7b301e3dc447a"},"keywords":["competitive programming 3","java","uva online judge"],"articleSection":["Competitive Programming 3","Java","UVA Online Judge"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/www.ferociouscoder.com\/blog\/archives\/uva-1112-mice-and-maze-213.html#respond"]}]},{"@type":"WebPage","@id":"https:\/\/www.ferociouscoder.com\/blog\/archives\/uva-1112-mice-and-maze-213.html","url":"https:\/\/www.ferociouscoder.com\/blog\/archives\/uva-1112-mice-and-maze-213.html","name":"UVA 1112 - Mice and Maze - Ferocious Coder","isPartOf":{"@id":"https:\/\/www.ferociouscoder.com\/blog\/#website"},"datePublished":"2013-07-02T04:44:43+00:00","dateModified":"2013-07-13T06:48:26+00:00","breadcrumb":{"@id":"https:\/\/www.ferociouscoder.com\/blog\/archives\/uva-1112-mice-and-maze-213.html#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.ferociouscoder.com\/blog\/archives\/uva-1112-mice-and-maze-213.html"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.ferociouscoder.com\/blog\/archives\/uva-1112-mice-and-maze-213.html#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.ferociouscoder.com\/blog"},{"@type":"ListItem","position":2,"name":"UVA 1112 &#8211; Mice and Maze"}]},{"@type":"WebSite","@id":"https:\/\/www.ferociouscoder.com\/blog\/#website","url":"https:\/\/www.ferociouscoder.com\/blog\/","name":"Ferocious Coder","description":"RAWR!","publisher":{"@id":"https:\/\/www.ferociouscoder.com\/blog\/#\/schema\/person\/1fb5cbee546cffd619a7b301e3dc447a"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.ferociouscoder.com\/blog\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":["Person","Organization"],"@id":"https:\/\/www.ferociouscoder.com\/blog\/#\/schema\/person\/1fb5cbee546cffd619a7b301e3dc447a","name":"Rawrosaur","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/secure.gravatar.com\/avatar\/1372156bd3b16de375c8727c2c617467bf6ff38f1679a7912f48286349c17e96?s=96&d=retro&r=pg","url":"https:\/\/secure.gravatar.com\/avatar\/1372156bd3b16de375c8727c2c617467bf6ff38f1679a7912f48286349c17e96?s=96&d=retro&r=pg","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/1372156bd3b16de375c8727c2c617467bf6ff38f1679a7912f48286349c17e96?s=96&d=retro&r=pg","caption":"Rawrosaur"},"logo":{"@id":"https:\/\/secure.gravatar.com\/avatar\/1372156bd3b16de375c8727c2c617467bf6ff38f1679a7912f48286349c17e96?s=96&d=retro&r=pg"},"sameAs":["http:\/\/www.ferociouscoder.com\/"],"url":"https:\/\/www.ferociouscoder.com\/blog\/archives\/author\/admin"}]}},"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p1mYMV-3r","jetpack_likes_enabled":true,"_links":{"self":[{"href":"https:\/\/www.ferociouscoder.com\/blog\/wp-json\/wp\/v2\/posts\/213","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.ferociouscoder.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.ferociouscoder.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.ferociouscoder.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.ferociouscoder.com\/blog\/wp-json\/wp\/v2\/comments?post=213"}],"version-history":[{"count":3,"href":"https:\/\/www.ferociouscoder.com\/blog\/wp-json\/wp\/v2\/posts\/213\/revisions"}],"predecessor-version":[{"id":278,"href":"https:\/\/www.ferociouscoder.com\/blog\/wp-json\/wp\/v2\/posts\/213\/revisions\/278"}],"wp:attachment":[{"href":"https:\/\/www.ferociouscoder.com\/blog\/wp-json\/wp\/v2\/media?parent=213"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.ferociouscoder.com\/blog\/wp-json\/wp\/v2\/categories?post=213"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.ferociouscoder.com\/blog\/wp-json\/wp\/v2\/tags?post=213"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}