C # The pure algorithm of the urban road map and the calculation of the total power

Source: Internet
Author: User

I have read a lot of code about graph traversal.

Today, I have used a common data structure to write out the data that is purely algorithmic. In the case of time complexity to be improved, O (2 ^ n) is optimal: O (n ^ 2)

The circuit diagram is bidirectional with a valid value such as the A-B distance is 5000 km so the B-A may not be 5000 km so I did not swap variables in the LoadData method directly stored in the Set

It is a bit messy to Recursively search for the next link and return the Code as the Start Node!

Copy codeThe Code is as follows: static List <string []> maindata = null;
Static int isend = 1;
Static List <string> fresult = new List <string> ();

Static void Main (string [] args)
{
String begin = "Chongqing ";
String end = "Xiamen ";
LoadData ();
Program pl = new Program ();
List <string> beginlist = new List <string> ();
Beginlist. Add (begin );
Pl. GetF (beginlist );

Foreach (string a in fresult)
Console. WriteLine ();
Console. WriteLine (fresult. Count );
// Main data end

List <string> searchlist = new List <string> ();
String temp = "";
Foreach (string f in fresult)
{
If (f. IndexOf (end)>-1)
{
Temp = f. Substring (0, f. LastIndexOf (end) + end. Length );
If (searchlist. Contains (temp) = false)
Searchlist. Add (temp );
}
}
Console. WriteLine (begin + "------------->" + end + ":");
Foreach (string a in searchlist)
Console. WriteLine ();
Console. WriteLine (searchlist. Count );
// Search data A to B

String a1 = "Maximum permission:" + GetMaxQuk (searchlist );
Console. WriteLine (a1 );
A1 = "minimum permission:" + GetMinQuk (searchlist );
Console. WriteLine (a1 );

Console. ReadKey ();
}

Obtain the largest weight data
Private static string GetMaxQuk (List <string> nage)
{
String resultsrt = "";

String [] nagearry = null;
Int val, maxval = 0;
For (int s = 0; s <nage. Count; s ++)
{
Nagearry = nage [s]. Split ('-'); // s Array
Val = GetVal (nagearry );
If (val> maxval)
{
Maxval = val;
Resultsrt = nage [s] + ":" + val;
}
Nagearry = null;
}
Return resultsrt;
}

Obtain the smallest weight data
Private static string GetMinQuk (List <string> nage)
{
String resultsrt = "";
String [] nagearry = null;
Int val, minval = int. MaxValue;
For (int s = 0; s <nage. Count; s ++)
{
Nagearry = nage [s]. Split ('-'); // s Array
Val = GetVal (nagearry );
If (val <minval)
{
Minval = val;
Resultsrt = nage [s] + ":" + val;
}
Nagearry = null;
}
Return resultsrt;
}

Method for obtaining the weight
Private static int GetVal (string [] findarry)
{
Int val = 0;
For (int ss = 0; ss <findarry. Length-1; ss = ss + 1)
{
Foreach (string [] aa in maindata)
{
If (aa [0] = findarry [ss] & aa [1] = findarry [ss + 1])
{
Val + = Convert. ToInt32 (aa [2]);
Break;
}
}
}
Return val;
}

List <string> GetF (List <string> beginlist)
{
// If dozens of lines of code are omitted, contact
If (isend = 0)
Return GetF (returnlist );
Else
Return null;
}

Load bound data
Static void LoadData ()
{
List <string []> backlist = null;
String [] arry = null;

Backlist = new List <string []> ();

Arry = new string [3];
Arry [0] = "Chongqing ";
Arry [1] = "Beijing ";
Arry [2] = "3000 ";
Backlist. Add (arry );
Arry = null;
Arry = new string [3];
Arry [0] = "Chongqing ";
Arry [1] = "Guangzhou ";
Arry [2] = "2500 ";
Backlist. Add (arry );
Arry = null;

Arry = new string [3];
Arry [0] = "Beijing ";
Arry [1] = "Chongqing ";
Arry [2] = "3000 ";
Backlist. Add (arry );
Arry = null;
Arry = new string [3];
Arry [0] = "Beijing ";
Arry [1] = "Guangzhou ";
Arry [2] = "3100 ";
Backlist. Add (arry );
Arry = null;
Arry = new string [3];
Arry [0] = "Beijing ";
Arry [1] = "Changsha ";
Arry [2] = "2800 ";
Backlist. Add (arry );
Arry = null;

Arry = new string [3];
Arry [0] = "Changsha ";
Arry [1] = "Beijing ";
Arry [2] = "2800 ";
Backlist. Add (arry );
Arry = null;
Arry = new string [3];
Arry [0] = "Changsha ";
Arry [1] = "Guangzhou ";
Arry [2] = "1500 ";
Backlist. Add (arry );
Arry = null;
Arry = new string [3];
Arry [0] = "Changsha ";
Arry [1] = "Xiamen ";
Arry [2] = "800 ";
Backlist. Add (arry );
Arry = null;

Arry = new string [3];
Arry [0] = "Guangzhou ";
Arry [1] = "Chongqing ";
Arry [2] = "2500 ";
Backlist. Add (arry );
Arry = null;
Arry = new string [3];
Arry [0] = "Guangzhou ";
Arry [1] = "Beijing ";
Arry [2] = "3100 ";
Backlist. Add (arry );
Arry = null;
Arry = new string [3];
Arry [0] = "Guangzhou ";
Arry [1] = "Changsha ";
Arry [2] = "1500 ";
Backlist. Add (arry );
Arry = null;
Maindata = backlist;

Arry = new string [3];
Arry [0] = "Xiamen ";
Arry [1] = "Changsha ";
Arry [2] = "800 ";
Backlist. Add (arry );
Arry = null;
Arry = new string [3];
Arry [0] = "Xiamen ";
Arry [1] = "Guangzhou ";
Arry [2] = "500 ";
Backlist. Add (arry );
Arry = null;
Arry = new string [3];
Arry [0] = "Guangzhou ";
Arry [1] = "Xiamen ";
Arry [2] = "500 ";
Backlist. Add (arry );
Arry = null;

Arry = new string [3];
Arry [0] = "Guangzhou ";
Arry [1] = "Yunnan ";
Arry [2] = "3200 ";
Backlist. Add (arry );
Arry = null;
Arry = new string [3];
Arry [0] = "Yunnan ";
Arry [1] = "Guangzhou ";
Arry [2] = "3200 ";
Backlist. Add (arry );
Arry = null;
Arry = new string [3];
Arry [0] = "Yunnan ";
Arry [1] = "Changsha ";
Arry [2] = "3500 ";
Backlist. Add (arry );
Arry = null;
Arry = new string [3];
Arry [0] = "Changsha ";
Arry [1] = "Yunnan ";
Arry [2] = "3500 ";
Backlist. Add (arry );
Arry = null;
Arry = new string [3];
Arry [0] = "Yunnan ";
Arry [1] = "Xiamen ";
Arry [2] = "5400 ";
Backlist. Add (arry );
Arry = null;
Arry = new string [3];
Arry [0] = "Xiamen ";
Arry [1] = "Yunnan ";
Arry [2] = "5400 ";
Backlist. Add (arry );
Arry = null;

}

The test results are as follows:Copy codeThe Code is as follows: the possible routes starting with Chongqing

// Begin
Chongqing-Beijing
Chongqing-Guangzhou
Chongqing-Beijing-Guangzhou
Chongqing-Beijing-Changsha
Chongqing-Guangzhou-Beijing
Chongqing-Guangzhou-Changsha
Chongqing-Guangzhou-Xiamen
Chongqing-Guangzhou-Yunnan
Chongqing-Beijing-Guangzhou-Changsha
Chongqing-Beijing-Guangzhou-Xiamen
Chongqing-Beijing-Guangzhou-Yunnan
Chongqing-Beijing-Changsha-Guangzhou
Chongqing-Beijing-Changsha-Xiamen
Chongqing-Beijing-Changsha-Yunnan
Chongqing-Guangzhou-Beijing-Changsha
Chongqing-Guangzhou-Changsha-Beijing
Chongqing-Guangzhou-Changsha-Xiamen
Chongqing-Guangzhou-Changsha-Yunnan
Chongqing-Guangzhou-Xiamen-Changsha
Chongqing-Guangzhou-Xiamen-Yunnan
Chongqing-Guangzhou-Yunnan-Changsha
Chongqing-Guangzhou-Yunnan-Xiamen
Chongqing-Beijing-Guangzhou-Changsha-Xiamen
Chongqing-Beijing-Guangzhou-Changsha-Yunnan
Chongqing-Beijing-Guangzhou-Xiamen-Changsha
Chongqing-Beijing-Guangzhou-Xiamen-Yunnan
Chongqing-Beijing-Guangzhou-Yunnan-Changsha
Chongqing-Beijing-Guangzhou-Yunnan-Xiamen
Chongqing-Beijing-Changsha-Guangzhou-Xiamen
Chongqing-Beijing-Changsha-Guangzhou-Yunnan
Chongqing-Beijing-Changsha-Xiamen-Guangzhou
Chongqing-Beijing-Changsha-Xiamen-Yunnan
Chongqing-Beijing-Changsha-Yunnan-Guangzhou
Chongqing-Beijing-Changsha-Yunnan-Xiamen
Chongqing-Guangzhou-Beijing-Changsha-Xiamen
Chongqing-Guangzhou-Beijing-Changsha-Yunnan
Chongqing-Guangzhou-Changsha-Xiamen-Yunnan
Chongqing-Guangzhou-Changsha-Yunnan-Xiamen
Chongqing-Guangzhou-Xiamen-Changsha-Beijing
Chongqing-Guangzhou-Xiamen-Changsha-Yunnan
Chongqing-Guangzhou-Xiamen-Yunnan-Changsha
Chongqing-Guangzhou-Yunnan-Changsha-Beijing
Chongqing-Guangzhou-Yunnan-Changsha-Xiamen
Chongqing-Guangzhou-Yunnan-Xiamen-Changsha
Chongqing-Beijing-Guangzhou-Changsha-Xiamen-Yunnan
Chongqing-Beijing-Guangzhou-Changsha-Yunnan-Xiamen
Chongqing-Beijing-Guangzhou-Xiamen-Changsha-Yunnan
Chongqing-Beijing-Guangzhou-Xiamen-Yunnan-Changsha
Chongqing-Beijing-Guangzhou-Yunnan-Changsha-Xiamen
Chongqing-Beijing-Guangzhou-Yunnan-Xiamen-Changsha
Chongqing-Beijing-Changsha-Guangzhou-Xiamen-Yunnan
Chongqing-Beijing-Changsha-Guangzhou-Yunnan-Xiamen
Chongqing-Beijing-Changsha-Xiamen-Guangzhou-Yunnan
Chongqing-Beijing-Changsha-Xiamen-Yunnan-Guangzhou
Chongqing-Beijing-Changsha-Yunnan-Guangzhou-Xiamen
Chongqing-Beijing-Changsha-Yunnan-Xiamen-Guangzhou
Chongqing-Guangzhou-Beijing-Changsha-Xiamen-Yunnan
Chongqing-Guangzhou-Beijing-Changsha-Yunnan-Xiamen
Chongqing-Guangzhou-Xiamen-Yunnan-Changsha-Beijing
Chongqing-Guangzhou-Yunnan-Xiamen-Changsha-Beijing
Count: 61
// End of all route charts

Search the path from Chongqing to Xiamen
// Chongqing to Xiamen begin
Chongqing-Guangzhou-Xiamen
Chongqing-Beijing-Guangzhou-Xiamen
Chongqing-Beijing-Changsha-Xiamen
Chongqing-Guangzhou-Changsha-Xiamen
Chongqing-Guangzhou-Yunnan-Xiamen
Chongqing-Beijing-Guangzhou-Changsha-Xiamen
Chongqing-Beijing-Guangzhou-Yunnan-Xiamen
Chongqing-Beijing-Changsha-Guangzhou-Xiamen
Chongqing-Beijing-Changsha-Yunnan-Xiamen
Chongqing-Guangzhou-Beijing-Changsha-Xiamen
Chongqing-Guangzhou-Changsha-Yunnan-Xiamen
Chongqing-Guangzhou-Yunnan-Changsha-Xiamen
Chongqing-Beijing-Guangzhou-Changsha-Yunnan-Xiamen
Chongqing-Beijing-Guangzhou-Yunnan-Changsha-Xiamen
Chongqing-Beijing-Changsha-Guangzhou-Yunnan-Xiamen
Chongqing-Beijing-Changsha-Yunnan-Guangzhou-Xiamen
Chongqing-Guangzhou-Beijing-Changsha-Yunnan-Xiamen
Count: 17
Maximum privilege: Chongqing-Guangzhou-Beijing-Changsha-Yunnan-Xiamen: 17300
Minimum weight: Chongqing-Guangzhou-Xiamen: 3000
// Chongqing to Xiamen end

Last ps: Although I have some ignorance about my methods, I will introduce myself to others.

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.