{"id":1575,"date":"2016-11-01T09:15:42","date_gmt":"2016-11-01T06:15:42","guid":{"rendered":"http:\/\/java.mazurok.com\/?p=1575"},"modified":"2016-11-01T15:52:35","modified_gmt":"2016-11-01T12:52:35","slug":"e-olymp-974","status":"publish","type":"post","link":"https:\/\/java.mazurok.com\/?p=1575","title":{"rendered":"e-olymp 974. \u0424\u043b\u043e\u0439\u0434-1"},"content":{"rendered":"<p>\u041f\u043e\u043b\u043d\u044b\u0439 \u043e\u0440\u0438\u0435\u043d\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u044b\u0439 \u0432\u0437\u0432\u0435\u0448\u0435\u043d\u043d\u044b\u0439 \u0433\u0440\u0430\u0444 \u0437\u0430\u0434\u0430\u043d \u043c\u0430\u0442\u0440\u0438\u0446\u0435\u0439 \u0441\u043c\u0435\u0436\u043d\u043e\u0441\u0442\u0438. \u041f\u043e\u0441\u0442\u0440\u043e\u0439\u0442\u0435 \u043c\u0430\u0442\u0440\u0438\u0446\u0443 \u043a\u0440\u0430\u0442\u0447\u0430\u0439\u0448\u0438\u0445 \u043f\u0443\u0442\u0435\u0439 \u043c\u0435\u0436\u0434\u0443 \u0435\u0433\u043e \u0432\u0435\u0440\u0448\u0438\u043d\u0430\u043c\u0438. \u0413\u0430\u0440\u0430\u043d\u0442\u0438\u0440\u0443\u0435\u0442\u0441\u044f, \u0447\u0442\u043e \u0432 \u0433\u0440\u0430\u0444\u0435 \u043d\u0435\u0442 \u0446\u0438\u043a\u043b\u043e\u0432 \u043e\u0442\u0440\u0438\u0446\u0430\u0442\u0435\u043b\u044c\u043d\u043e\u0433\u043e \u0432\u0435\u0441\u0430.<\/p>\n<h3>\u0412\u0445\u043e\u0434\u043d\u044b\u0435 \u0434\u0430\u043d\u043d\u044b\u0435<\/h3>\n<p>\u0412 \u043f\u0435\u0440\u0432\u043e\u0439 \u0441\u0442\u0440\u043e\u043a\u0435 \u0437\u0430\u043f\u0438\u0441\u0430\u043d\u043e \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u043e \u0432\u0435\u0440\u0448\u0438\u043d \u0433\u0440\u0430\u0444\u0430 n (<strong>1<\/strong> \u2264 [latex]n[\/latex] \u2264 <strong>100<\/strong>). \u0412 \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u0445 n \u0441\u0442\u0440\u043e\u043a\u0430\u0445 \u0437\u0430\u043f\u0438\u0441\u0430\u043d\u043e \u043f\u043e [latex]n[\/latex] \u0447\u0438\u0441\u0435\u043b \u2014 \u043c\u0430\u0442\u0440\u0438\u0446\u0430 \u0441\u043c\u0435\u0436\u043d\u043e\u0441\u0442\u0438 \u0433\u0440\u0430\u0444\u0430 ([latex]j[\/latex]-\u043e\u0435 \u0447\u0438\u0441\u043b\u043e \u0432 [latex]i[\/latex]-\u043e\u0439 \u0441\u0442\u0440\u043e\u043a\u0435 \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0443\u0435\u0442 \u0432\u0435\u0441\u0443 \u0440\u0435\u0431\u0440\u0430 \u0438\u0437 \u0432\u0435\u0440\u0448\u0438\u043d\u044b [latex]i[\/latex] \u0432 \u0432\u0435\u0440\u0448\u0438\u043d\u0443 [latex]j[\/latex]). \u0412\u0441\u0435 \u0447\u0438\u0441\u043b\u0430 \u043f\u043e \u043c\u043e\u0434\u0443\u043b\u044e \u043d\u0435 \u043f\u0440\u0435\u0432\u044b\u0448\u0430\u044e\u0442 <strong>100<\/strong>. \u041d\u0430 \u0433\u043b\u0430\u0432\u043d\u043e\u0439 \u0434\u0438\u0430\u0433\u043e\u043d\u0430\u043b\u0438 \u043c\u0430\u0442\u0440\u0438\u0446\u044b \u2014 \u0432\u0441\u0435\u0433\u0434\u0430 \u043d\u0443\u043b\u0438.<\/p>\n<h3>\u0412\u044b\u0445\u043e\u0434\u043d\u044b\u0435 \u0434\u0430\u043d\u043d\u044b\u0435<\/h3>\n<p>\u0412\u044b\u0432\u0435\u0434\u0438\u0442\u0435 [latex]n[\/latex] \u0441\u0442\u0440\u043e\u043a \u043f\u043e [latex]n[\/latex] \u0447\u0438\u0441\u0435\u043b \u2014 \u043c\u0430\u0442\u0440\u0438\u0446\u0443 \u043a\u0440\u0430\u0442\u0447\u0430\u0439\u0448\u0438\u0445 \u0440\u0430\u0441\u0441\u0442\u043e\u044f\u043d\u0438\u0439 \u043c\u0435\u0436\u0434\u0443 \u043f\u0430\u0440\u0430\u043c\u0438 \u0432\u0435\u0440\u0448\u0438\u043d. [latex]j[\/latex]-\u043e\u0435 \u0447\u0438\u0441\u043b\u043e \u0432 [latex]i[\/latex]-\u043e\u0439 \u0441\u0442\u0440\u043e\u043a\u0435 \u0434\u043e\u043b\u0436\u043d\u043e \u0440\u0430\u0432\u043d\u044f\u0442\u044c\u0441\u044f \u0432\u0435\u0441\u0443 \u043a\u0440\u0430\u0442\u0447\u0430\u0439\u0448\u0435\u0433\u043e \u043f\u0443\u0442\u0438 \u0438\u0437 \u0432\u0435\u0440\u0448\u0438\u043d\u044b [latex]i[\/latex] \u0432 \u0432\u0435\u0440\u0448\u0438\u043d\u0443 [latex]j[\/latex].<\/p>\n<h3>\u0410\u043b\u0433\u043e\u0440\u0438\u0442\u043c<\/h3>\n<p>(\u0432\u0437\u044f\u0442\u043e \u0441 <a href=\"https:\/\/ru.wikipedia.org\/wiki\/%D0%90%D0%BB%D0%B3%D0%BE%D1%80%D0%B8%D1%82%D0%BC_%D0%A4%D0%BB%D0%BE%D0%B9%D0%B4%D0%B0_%E2%80%94_%D0%A3%D0%BE%D1%80%D1%88%D0%B5%D0%BB%D0%BB%D0%B0\" target=\"_blank\">\u0412\u0438\u043a\u0438\u043f\u0435\u0434\u0438\u0438<\/a>)<\/p>\n<p>\u041f\u0443\u0441\u0442\u044c \u0432\u0435\u0440\u0448\u0438\u043d\u044b \u0433\u0440\u0430\u0444\u0430 [latex]{\\displaystyle G=(V,\\;E),\\;|V|=n}[\/latex] \u043f\u0440\u043e\u043d\u0443\u043c\u0435\u0440\u043e\u0432\u0430\u043d\u044b \u043e\u0442 <strong>1<\/strong> \u0434\u043e [latex] {\\displaystyle n}[\/latex] \u0438 \u0432\u0432\u0435\u0434\u0435\u043d\u043e \u043e\u0431\u043e\u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 [latex]{\\displaystyle d_{ij}^{k}}[\/latex] \u0434\u043b\u044f \u0434\u043b\u0438\u043d\u044b \u043a\u0440\u0430\u0442\u0447\u0430\u0439\u0448\u0435\u0433\u043e \u043f\u0443\u0442\u0438 \u043e\u0442 [latex] {\\displaystyle i}[\/latex] \u0434\u043e [latex]{\\displaystyle j}[\/latex], \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u043a\u0440\u043e\u043c\u0435 \u0441\u0430\u043c\u0438\u0445 \u0432\u0435\u0440\u0448\u0438\u043d [latex] {\\displaystyle i,\\;j} [\/latex] \u043f\u0440\u043e\u0445\u043e\u0434\u0438\u0442 \u0442\u043e\u043b\u044c\u043a\u043e \u0447\u0435\u0440\u0435\u0437 \u0432\u0435\u0440\u0448\u0438\u043d\u044b [latex]{\\displaystyle 1\\ldots k} [\/latex]. \u041e\u0447\u0435\u0432\u0438\u0434\u043d\u043e, \u0447\u0442\u043e [latex]{\\displaystyle d_{ij}^{0}} [\/latex] \u2014 \u0434\u043b\u0438\u043d\u0430 (\u0432\u0435\u0441) \u0440\u0435\u0431\u0440\u0430 [latex]{\\displaystyle (i,\\;j)}[\/latex], \u0435\u0441\u043b\u0438 \u0442\u0430\u043a\u043e\u0432\u043e\u0435 \u0441\u0443\u0449\u0435\u0441\u0442\u0432\u0443\u0435\u0442 (\u0432 \u043f\u0440\u043e\u0442\u0438\u0432\u043d\u043e\u043c \u0441\u043b\u0443\u0447\u0430\u0435 \u0435\u0433\u043e \u0434\u043b\u0438\u043d\u0430 \u043c\u043e\u0436\u0435\u0442 \u0431\u044b\u0442\u044c \u043e\u0431\u043e\u0437\u043d\u0430\u0447\u0435\u043d\u0430 \u043a\u0430\u043a [latex]{\\displaystyle \\infty } [\/latex] ).<\/p>\n<p>\u0421\u0443\u0449\u0435\u0441\u0442\u0432\u0443\u0435\u0442 \u0434\u0432\u0430 \u0432\u0430\u0440\u0438\u0430\u043d\u0442\u0430 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u044f [latex] {\\displaystyle d_{ij}^{k},\\;k\\in \\mathbb {(} 1,\\;\\ldots ,\\;n)} d_{ij}^{k},\\;k\\in \\mathbb {(} 1,\\;\\ldots ,\\;n)[\/latex]:<\/p>\n<p>\u041a\u0440\u0430\u0442\u0447\u0430\u0439\u0448\u0438\u0439 \u043f\u0443\u0442\u044c \u043c\u0435\u0436\u0434\u0443 [latex]{\\displaystyle i,\\;j}[\/latex] \u043d\u0435 \u043f\u0440\u043e\u0445\u043e\u0434\u0438\u0442 \u0447\u0435\u0440\u0435\u0437 \u0432\u0435\u0440\u0448\u0438\u043d\u0443 [latex] {\\displaystyle k}[\/latex], \u0442\u043e\u0433\u0434\u0430 [latex]{\\displaystyle d_{ij}^{k}=d_{ij}^{k-1}}[\/latex]\n\u0421\u0443\u0449\u0435\u0441\u0442\u0432\u0443\u0435\u0442 \u0431\u043e\u043b\u0435\u0435 \u043a\u043e\u0440\u043e\u0442\u043a\u0438\u0439 \u043f\u0443\u0442\u044c \u043c\u0435\u0436\u0434\u0443 [latex]{\\displaystyle i,\\;j}[\/latex], \u043f\u0440\u043e\u0445\u043e\u0434\u044f\u0449\u0438\u0439 \u0447\u0435\u0440\u0435\u0437 [latex]{\\displaystyle k}[\/latex], \u0442\u043e\u0433\u0434\u0430 \u043e\u043d \u0441\u043d\u0430\u0447\u0430\u043b\u0430 \u0438\u0434\u0451\u0442 \u043e\u0442 [latex]{\\displaystyle i} [\/latex] \u0434\u043e [latex] {\\displaystyle k} [\/latex], \u0430 \u043f\u043e\u0442\u043e\u043c \u043e\u0442 [latex] {\\displaystyle k} [\/latex] \u0434\u043e [latex] {\\displaystyle j} [\/latex]. \u0412 \u044d\u0442\u043e\u043c \u0441\u043b\u0443\u0447\u0430\u0435, \u043e\u0447\u0435\u0432\u0438\u0434\u043d\u043e, [latex]{\\displaystyle d_{ij}^{k}=d_{ik}^{k-1}+d_{kj}^{k-1}}[\/latex]\n<p>\u0422\u0430\u043a\u0438\u043c \u043e\u0431\u0440\u0430\u0437\u043e\u043c, \u0434\u043b\u044f \u043d\u0430\u0445\u043e\u0436\u0434\u0435\u043d\u0438\u044f \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u044f \u0444\u0443\u043d\u043a\u0446\u0438\u0438 \u0434\u043e\u0441\u0442\u0430\u0442\u043e\u0447\u043d\u043e \u0432\u044b\u0431\u0440\u0430\u0442\u044c \u043c\u0438\u043d\u0438\u043c\u0443\u043c \u0438\u0437 \u0434\u0432\u0443\u0445 \u043e\u0431\u043e\u0437\u043d\u0430\u0447\u0435\u043d\u043d\u044b\u0445 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0439.<\/p>\n<p>\u0422\u043e\u0433\u0434\u0430 \u0440\u0435\u043a\u0443\u0440\u0440\u0435\u043d\u0442\u043d\u0430\u044f \u0444\u043e\u0440\u043c\u0443\u043b\u0430 \u0434\u043b\u044f [latex]{\\displaystyle d_{ij}^{k}}[\/latex] \u0438\u043c\u0435\u0435\u0442 \u0432\u0438\u0434:<\/p>\n[latex]{\\displaystyle d_{ij}^{0}}[\/latex] \u2014 \u0434\u043b\u0438\u043d\u0430 \u0440\u0435\u0431\u0440\u0430 [latex] {\\displaystyle (i,\\;j);} (i,\\;j);[\/latex]\n[latex]{\\displaystyle d_{ij}^{k}=\\min(d_{ij}^{k-1},\\;d_{ik}^{k-1}+d_{kj}^{k-1}).}[\/latex]\n<p>\u0410\u043b\u0433\u043e\u0440\u0438\u0442\u043c \u0424\u043b\u043e\u0439\u0434\u0430-\u0423\u043e\u0440\u0448\u0435\u043b\u043b\u0430 \u043f\u043e\u0441\u043b\u0435\u0434\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u043d\u043e \u0432\u044b\u0447\u0438\u0441\u043b\u044f\u0435\u0442 \u0432\u0441\u0435 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u044f [latex]{\\displaystyle d_{ij}^{k},} [\/latex], [latex]{\\displaystyle \\forall i,\\;j}[\/latex] \u0434\u043b\u044f [latex] {\\displaystyle k} [\/latex] \u043e\u0442 1 \u0434\u043e [latex] {\\displaystyle n} [\/latex]. \u041f\u043e\u043b\u0443\u0447\u0435\u043d\u043d\u044b\u0435 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u044f [latex] {\\displaystyle d_{ij}^{n}}[\/latex] \u044f\u0432\u043b\u044f\u044e\u0442\u0441\u044f \u0434\u043b\u0438\u043d\u0430\u043c\u0438 \u043a\u0440\u0430\u0442\u0447\u0430\u0439\u0448\u0438\u0445 \u043f\u0443\u0442\u0435\u0439 \u043c\u0435\u0436\u0434\u0443 \u0432\u0435\u0440\u0448\u0438\u043d\u0430\u043c\u0438 [latex]. {\\displaystyle i,\\;j.} [\/latex].<\/p>\n<h3>\u041a\u043e\u0434<\/h3>\n<pre class=\"lang:java decode:true \">\/* package whatever; \/\/ don't place package name! *\/\r\n\r\nimport java.util.*;\r\nimport java.lang.*;\r\nimport java.io.*;\r\n\r\n\r\nclass Ideone\r\n{\r\n\tpublic static void main (String[] args) throws java.lang.Exception\r\n\t{\r\n\t\t\tScanner in =new Scanner(System.in);\r\n\t\t\tint n = in.nextInt();\r\n\t\t\tint w[][] = new int[n][n];\t \r\n\t\t\tfor( int i = 0 ; i &lt; n ; i++ ){\r\n\t\t\t\tfor( int j = 0 ; j &lt; n ; j++ ){\r\n\t\t\t\t\tw[i][j] = in.nextInt();\r\n\t\t\t\t}\r\n\t\t\t}\r\n\t\r\n\t\t\tfor( int k = 0 ; k &lt; n ; k++ ){\r\n\t\t\t\tfor( int i = 0 ; i&lt;n ; i++ ){\r\n\t\t\t\t\tfor( int j=0 ; j&lt;n ; j++ ){\r\n\t\t\t\t\t    w[i][j] = Math.min(w[i][j], w[i][k] + w[k][j]);\r\n\t\t\t\t\t}\r\n\t\t\t\t}\r\n\t\t\t}\r\n    \r\n\t\t\tfor( int i = 0 ; i &lt; n ; i++ ){\r\n\t\t\t\tfor( int j = 0 ; j &lt; n ; j++ ){\r\n\t\t    \t\tSystem.out.print(w[i][j] + \" \");\r\n\t\t\t\t}\r\n\t\t\t\tSystem.out.println();\r\n    \t\t}\r\n\t}\r\n}<\/pre>\n<p>\u0423\u0441\u043b\u043e\u0432\u0438\u0435 \u043d\u0430 <a href=\"https:\/\/www.e-olymp.com\/ru\/problems\/974\" target=\"_blank\">e-olymp.com<\/a><br \/>\n\u0420\u0435\u0448\u0435\u043d\u0438\u0435 \u043d\u0430 <a href=\"https:\/\/www.e-olymp.com\/ru\/submissions\/2885946\" target=\"_blank\">e-olymp.com<\/a><br \/>\n\u041a\u043e\u0434 \u043d\u0430 <a href=\"http:\/\/ideone.com\/CjHUma\" target=\"_blank\">ideone.com<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u041f\u043e\u043b\u043d\u044b\u0439 \u043e\u0440\u0438\u0435\u043d\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u044b\u0439 \u0432\u0437\u0432\u0435\u0448\u0435\u043d\u043d\u044b\u0439 \u0433\u0440\u0430\u0444 \u0437\u0430\u0434\u0430\u043d \u043c\u0430\u0442\u0440\u0438\u0446\u0435\u0439 \u0441\u043c\u0435\u0436\u043d\u043e\u0441\u0442\u0438. \u041f\u043e\u0441\u0442\u0440\u043e\u0439\u0442\u0435 \u043c\u0430\u0442\u0440\u0438\u0446\u0443 \u043a\u0440\u0430\u0442\u0447\u0430\u0439\u0448\u0438\u0445 \u043f\u0443\u0442\u0435\u0439 \u043c\u0435\u0436\u0434\u0443 \u0435\u0433\u043e \u0432\u0435\u0440\u0448\u0438\u043d\u0430\u043c\u0438. \u0413\u0430\u0440\u0430\u043d\u0442\u0438\u0440\u0443\u0435\u0442\u0441\u044f, \u0447\u0442\u043e \u0432 \u0433\u0440\u0430\u0444\u0435 \u043d\u0435\u0442 \u0446\u0438\u043a\u043b\u043e\u0432 \u043e\u0442\u0440\u0438\u0446\u0430\u0442\u0435\u043b\u044c\u043d\u043e\u0433\u043e \u0432\u0435\u0441\u0430. \u0412\u0445\u043e\u0434\u043d\u044b\u0435 \u0434\u0430\u043d\u043d\u044b\u0435 \u0412 \u043f\u0435\u0440\u0432\u043e\u0439 \u0441\u0442\u0440\u043e\u043a\u0435 \u0437\u0430\u043f\u0438\u0441\u0430\u043d\u043e \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u043e \u0432\u0435\u0440\u0448\u0438\u043d \u0433\u0440\u0430\u0444\u0430 n (1 \u2264 [latex]n[\/latex] \u2264 100). \u0412 \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u0445 n \u0441\u0442\u0440\u043e\u043a\u0430\u0445 \u0437\u0430\u043f\u0438\u0441\u0430\u043d\u043e \u043f\u043e [latex]n[\/latex] \u0447\u0438\u0441\u0435\u043b \u2014 \u043c\u0430\u0442\u0440\u0438\u0446\u0430 \u0441\u043c\u0435\u0436\u043d\u043e\u0441\u0442\u0438 \u0433\u0440\u0430\u0444\u0430 ([latex]j[\/latex]-\u043e\u0435 \u0447\u0438\u0441\u043b\u043e \u0432 [latex]i[\/latex]-\u043e\u0439 \u0441\u0442\u0440\u043e\u043a\u0435 \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0443\u0435\u0442 &hellip; <a href=\"https:\/\/java.mazurok.com\/?p=1575\" class=\"more-link\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":85,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[109,26],"tags":[194,142,193,192],"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/java.mazurok.com\/index.php?rest_route=\/wp\/v2\/posts\/1575"}],"collection":[{"href":"https:\/\/java.mazurok.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/java.mazurok.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/java.mazurok.com\/index.php?rest_route=\/wp\/v2\/users\/85"}],"replies":[{"embeddable":true,"href":"https:\/\/java.mazurok.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1575"}],"version-history":[{"count":20,"href":"https:\/\/java.mazurok.com\/index.php?rest_route=\/wp\/v2\/posts\/1575\/revisions"}],"predecessor-version":[{"id":1596,"href":"https:\/\/java.mazurok.com\/index.php?rest_route=\/wp\/v2\/posts\/1575\/revisions\/1596"}],"wp:attachment":[{"href":"https:\/\/java.mazurok.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1575"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/java.mazurok.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1575"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/java.mazurok.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1575"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}