CF #228 div1 B. Fox and Minimal Path

Source: Internet
Author: User

Title Link: Http://codeforces.com/problemset/problem/388/B

The main idea is to use no more than 1000 points to construct a side right 1 of the graph, so that point 1 to 2 of the shortest number of a given value K, where k is not more than 1e9 positive integers, output adjacency matrix

There are more than one method of construction

There is a layered approach to structuring this,

The point of Layer I and point 1th is the distance of i-1

Place number 1th on the first floor.

The second layer places 3rd and 4th points, respectively, with the 1th points of the previous layer, their shortest circuit is 1, and the number is 1

Then each layer will be more than the number of points on the previous layer 1, the i+1 layer, first with the first I point layer connected, and then the first i+1 point and the first layer of all points connected

In this way, the shortest number of short circuits is represented by the layer

1

1 1

1 1 2

1 1 2 4

1 1 2) 4 8

1 1 2 4 8 16

Go on like this. The main thing is to use the 1+2+...+2^n=2^ (n+1)-1 more a number of 1 points will be able to make up the next 2 power.

Other construction methods can also be continuously separated 2 fork, and then convergence, and then bifurcation, so that each junction point is also a power of 2, but note that each junction point can not be directly connected to the 2nd points, because each junction point and 1th point distance is not equal, the processing method can be dragged down to a "bus", Bus end connected to point 2nd

The following is the first way to construct the code

1 ImportJava.io.OutputStream;2 Importjava.io.IOException;3 ImportJava.io.InputStream;4 ImportJava.io.PrintWriter;5 ImportJava.util.StringTokenizer;6 Importjava.io.IOException;7 ImportJava.io.BufferedReader;8 ImportJava.io.InputStreamReader;9 ImportJava.io.InputStream;Ten  One  Public classMain { A      Public Static voidMain (string[] args) { -InputStream InputStream =system.in; -OutputStream OutputStream =System.out; theInputreader in =NewInputreader (inputstream); -PrintWriter out =NewPrintWriter (outputstream); -Task Solver =NewTask (); -Solver.solve (1, in, out); + out.close (); -     } +  A     Static classTask { at         voidAddedge (Boolean[] g,intUintv) { -G[U][V] =true; -G[v][u] =true; -         } -  -          Public voidSolveintTestnumber, Inputreader in, printwriter out) { in             intK =in.nextint (); -             Boolean[] G =New Boolean[1001] [10001]; to             intn = 2; +             intFrom = 0, to = 1; -              for(intp = 1, l = 1; P <= k; P <<= 1, l++) { the                 intNfrom =N; *                  for(inti = from; I < to; i++) { $Addedge (g, I, n++);Panax Notoginseng                 } -                  for(inti = from; I < to; i++) { the Addedge (g, I, n); +                 } Afrom =Nfrom; theto = + +N; +             } -              for(inti = from + 1; I < to; i++) { $                 if((K & (1 << i-from-1))! = 0) { $Addedge (g, I, 1); -                 } -             } the out.println (n); -              for(inti = 0; I < n; i++) {Wuyi                  for(intj = 0; J < N; J + +) { theOut.print ((G[i][j]? "Y": "N")); -                 } Wu out.println (); -             } About         } $  -     } -  -     Static classInputreader { A         Private FinalBufferedReader Reader; +         PrivateStringTokenizer Tokenizer; the  -          PublicInputreader (InputStream stream) { $Reader =NewBufferedReader (NewInputStreamReader (stream)); theTokenizer =NULL; the         } the  the          PublicString Next () { -              while(Tokenizer = =NULL|| !Tokenizer.hasmoretokens ()) { in                 Try { theTokenizer =NewStringTokenizer (Reader.readline ()); the}Catch(IOException e) { About                     Throw NewRuntimeException (e); the                 } the             } the             returnTokenizer.nexttoken (); +         } -  the          Public intNextint () {Bayi             returnInteger.parseint (Next ()); the         } the  -     } -}
View Code

CF #228 div1 B. Fox and Minimal Path

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.