Java implementation Floyd statistics Tianjin Metro site distance

Source: Internet
Author: User

One: Description

(1) Using Floyd to realize the calculation records and paths of each site

(2) Site acquisition and initial distance are based on external files

(3) results are stored as external files

(4) Inter-site interchange, the initial value is considered to be 1

(5) Code comments more detailed, if in doubt or code has, please contact me, thank you

(6) Definition of two-dimensional data in Java:

A:float[][] Numthree; Defines a 2-dimensional array of type float
Numthree=new Float[5][5]; Allocate 5 rows and 5 columns to the space size
numthree[0][0]=1.1f; Access 1 rows and 1 columns through the subscript index =1.1
numthree[1][0]=1.2f; 2 rows 1 Columns =1.2
B:int[][] Numseven=new int[][]{{10,20,30},{40,50},{60}}; There's nothing to say if you don't understand it then don't learn it!
C:list two-dimensional array: list<object>[][]lists=new arraylist[4][4];
List two-dimensional array that holds two-dimensional object types: list<object[][]>[][] list=new arraylist[4][4];
List:list<object[][]> list=new arraylist<object[][]> () for storing two-dimensional arrays

(7) Array traversal

A:

int arr[][] = new int[][] {{1}, {1, 2}, {1, 2, 3}}; for (int i = 0; i < arr.length; i++) {  int[] arr2 = arr[i];  for (int c = 0; c < arr2.length; C + +) {   System.out.print (arr2[c]);  }
B:ARR[I][J] the way

C:

Java traversal ArrayList four ways to package Com.test;import java.util.arraylist;import Java.util.iterator;import java.util.List; public class Arraylistdemo {public    static void Main (String args[]) {        list<string> List = new Arraylist<s Tring> ();        List.add ("Luojiahui");        List.add ("Luojiafeng");        Method 1        Iterator it1 = List.iterator ();        while (It1.hasnext ()) {            System.out.println (It1.next ());        }        Method 2 for        (Iterator it2 = List.iterator (); It2.hasnext ();) {             System.out.println (It2.next ());        }        Method 3 for        (String tmp:list) {            System.out.println (TMP);        }        Method 4 for        (int i = 0;i < List.size (); i + +) {            System.out.println (List.get (i))        ;    }}}


Two: The complete code is as follows

Package Edu.tju.cs;import Java.io.bufferedreader;import Java.io.bufferedwriter;import java.io.File;import Java.io.fileinputstream;import Java.io.filewriter;import Java.io.ioexception;import Java.io.InputStreamReader; Import Java.util.arraylist;import java.util.hashmap;import Java.util.list;import Java.util.map;public class Metrofloyd {private final int max_size = 92;//86+1 + 5 Open array space, can be larger some private int k = 0;private int Vertex = 0;//number of sites , by reading each record to get private int[] line = new int[max_size];//and path to record the path of private int[][] path = new int[max_size][max_size];//record Path private int[][] Dist = new int[max_size][max_size];//record the distance between sites private list<integer> StationID = new ARRAYLIST&L T;integer> ();//Get the site number private map<integer,string> mapidname = new hashmap<integer,string> ();// The corresponding relationship of the site number and name/* Function: Read the external file, put the site number into the Stationid array, the site number and name of the corresponding relationship mapidname, and vertex statistics * @param: OriginalPath read the path name of the file * @ Call other functions: null * @return: null */////2305, Liu Can, 117.123174,39.214493public void ReadText (STring OriginalPath) {try {Vertex = 0;            String regex = ",";//partition dependent symbol, \ string encoding= "GBK";            File File=new file (OriginalPath);                if (File.isfile () && file.exists ()) {//Determine if the file exists inputstreamreader read = new InputStreamReader ( New FileInputStream (file), encoding);//considering the encoding format BufferedReader BufferedReader = new BufferedReader            (read);                The original row of data and whether the data needs to change the symbol String originalline = null; Originalline = Bufferedreader.readline ();//First row identifier filter out if (Originalline = = null) {Read.close                     ();                Bufferedreader.close ();                Return } stationid.add (0); Invalid value since the site number is starting from 1 while ((Originalline = Bufferedreader.readline ()) = null) {//character                String delimited string tmp[] = Originalline.split (regex); If the STATIONID.ADD is compliant with the re-synthesis (Integer.parseint (tMp[0]));                Mapidname.put (Integer.parseint (tmp[0]), tmp[1]);                vertex++;                System.out.println ("Jilu:" + Vertex);                }//Close Write file Read.close ();    Bufferedreader.close ();    } else {System.out.println ("The specified file cannot be found"); }} catch (Exception e) {System.out.println ("Readtowrite ...        Error reading file contents ");    E.printstacktrace (); }system.out.println ("Readtowrite ... Devide is OVER!!! ");} /* Function: The number of paths to each site and the path through the statistics, the format is as follows: Site name * @param: DestinationPath2 The path name of the write file * @ Call other functions: null * @return: null * Result Example : * ==========================source: Two-Wei road target: Tianjin Bay Plaza Distance:6path: Two---Hai-Guang temple------Anshan Road--Yingkou Road--Yingkou Road-- Heping Road--Tianjin Bay Plaza ========================== *///public void writeText2 (String destinationPath2) {try {BufferedWriter Bufferwriter = new BufferedWriter (new FileWriter (destinationPath2));    String newLine = ""; int p,q,m;      for (p=1;p<=vertex;p++) {  for (q=p+1;q<=vertex;q++) {newLine = "\n==========================\nsource:" + mapidname.get (StationID. Get (p)) + "\ntarget:" + mapidname.get (Stationid.get (q)) + "\ndistance:" + dist[p][q] + "\npath:" + mapidname.get (StationID             . Get (p));            k=2;            Root (P,Q);            for (m=2;m<=k-1;m++) {newLine = NewLine +--+ mapidname.get (Stationid.get (line[m]));            } NewLine + = "\n==========================\n";        Bufferwriter.write (NewLine); }} bufferwriter.close ();} catch (Exception e) {System.out.println ("Mergemindistortbyday_zheng ...        Error reading file contents ");    E.printstacktrace (); }}/* function: The number of paths of each site and the path through the statistics, the format is as follows; just the number of the site, which is the file format to use later. * @param: DestinationPath2 Write file path name * @ Call other functions: private void Root (int p,int q) Returns the site of the path * @return: null *///Output Shortest path, numbered only Correspondence between 2314,533 and 2314,533,6,2314,2313,529,530,531,532,533public void WriteText (String destinationpath) {try { BufferedWriter BufferwriteR = new BufferedWriter (new FileWriter (DestinationPath));    String newLine = ""; int p,q,m; for (p=1;p<=vertex;p++) {for (q=1;q<=vertex;q++) {newLine = "" + stationid.get (P) + "," + St             Ationid.get (q) + "," + dist[p][q] + "," + Stationid.get (p);            k=2;            Root (P,Q);            for (m=2;m<=k-1;m++) {newLine = NewLine + "," + Stationid.get (Line[m]);            } newLine + = "\ n";        Bufferwriter.write (NewLine); }} bufferwriter.close ();} catch (Exception e) {System.out.println ("Mergemindistortbyday_zheng ...        Error reading file contents ");    E.printstacktrace (); }}/* function: Initialize the initial distance between each site, that is, adjacent to 1, not adjacent to 0, interchange station to sit special treatment, and interchange between the station is considered the path is 1 * @param: null * @ Call OTHER functions: null * @return: null *///Initprivat e void init () {/*| | (Stationid.get (p) ==280 && 533-stationid.get (q) = = 1) | | (Stationid.get (p) ==280 && 791-stationid.get (q) = = 1) | | (Stationid.get (p) ==533 && 280-stationid.get (q) = = 1) | | (Stationid.get (P) ==533 && 791-stationid.get (q) = = 1) | | (Stationid.get (p) ==791 && 280-stationid.get (q) = = 1) | | (Stationid.get (p) ==791 && 533-stationid.get (q) = = 1) | | (Stationid.get (q) ==280 && 533-stationid.get (p) = = 1) | | (Stationid.get (q) ==280 && 791-stationid.get (p) = =-1) | | (Stationid.get (q) ==533 && 280-stationid.get (p) = =-1) | | (Stationid.get (q) ==533 && 791-stationid.get (p) = =-1) | | (Stationid.get (q) ==791 && 280-stationid.get (p) = =-1) | | (Stationid.get (q) ==791 && 533-stationid.get (p) = =-1) *///Initialize for (int p=1;p<=vertex;p++) {for (Int. q=1;q<= vertex;q++) {//280 533 791 Tjif (Stationid.get (P)-stationid.get (q) ==-1 | | stationid.get (P)-stationid.get (q) ==1) {Dist[ P][Q] = 1;dist[q][p] = 1;} ELSE{DIST[P][Q] = 0;dist[q][p] = 0;} PATH[P][Q] = 0; PATH[P][Q] = 0;}} Yingkou Road Interchange is considered to be a station, 13 78 to be strictly in accordance with the order of the documents written, their own number to dist[13][78]=1;dist[78][13]=1;//southwest corner of the transfer is considered a station to dist[9][53]=1;dist[53][9]=1;// Tianjin station Interchange think is a station to Dist[46][57]=1;dist[57][46]=1;dist[46][81]=1;dist[81] [46]=1;dist[57][81]=1;dist[81][57]=1;} /* Function: Call of a recursive function, when calculating the site that the path passes through, the * @param: null * @ Call OTHER functions: null * @return: null *///recursively seek point private void Root on each path (int p,int q        {if (path[p][q]>0) {Root (p,path[p][q]);    Root (PATH[P][Q],Q);        }else{line[k]=q;    k++; }}/* function: Core algorithm, Floyd calculate path between each site * @param: null * @ Call OTHER functions: null * @return: null *///Floyd algorithm shortest path private void Floyd () {i    NT K,p,q; for (k=1;k<=vertex;k++) {for (p=1;p<=vertex;p++) {if (dist[p][k]>0) {for (q=1;q<= vertex;q++) {if (dist[k][q]>0) {if (((dist[p][q]>dist[p][k]+dist[k][q)) | | (dist[p][q]==0))                            && (p!=q)) {dist[p][q]=dist[p][k]+dist[k][q];                        Path[p][q]=k; }}}}}}}public static void Main (string[] args) {Metrofloyd Metro Floyd = new Metrofloyd ();//2305, Liu Can, 117.123174,39.214493stRing OriginalPath = "D:\\tjdata_metro\\stationid_name_new.csv"; String destinationpath= "D:\\tjdata_metro\\metrofloydid.csv"; String destinationpath2= "D:\\tjdata_metro\\metrofloydname.csv"; Metrofloyd.readtext (OriginalPath); Metrofloyd.init (); Metrofloyd.floyd (); Metrofloyd.writetext (DestinationPath); Metrofloyd.writetext2 ( DESTINATIONPATH2);}


Java implementation Floyd statistics Tianjin Metro site distance

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.