PAT l2-001 Emergency Rescue (Java)

Source: Internet
Author: User

Import Java.util.Scanner; /** * Dijkstra Classic application of variable * l2-001. Emergency assistance * @author PANDA * */public class Main {static int point_num;//points maxint 10000 static int arc_num;//Edge number Stati c int g[][];//static int firstaid[];//Each city rescue team number static int start_point;//departure city static int end_point;//reach city public s
		tatic void Main (string[] args) {Scanner Scanner =new (Scanner);
		Point_num=scanner.nextint ();
		Arc_num=scanner.nextint ();
		Start_point=scanner.nextint ();
		End_point=scanner.nextint ();
		g=new int [Point_num][point_num];
		firstaid=new int [Point_num];
		for (int i = 0; i < firstaid.length i++) {//Initialize firstaid[i]=scanner.nextint (); for (int i = 0; i < Point_num i++) {//Note the value on the main diagonal is 0, the other is maxint for (int j = 0; J < Point_num; J +) {if (i=
				=J) g[i][j]=0;
			else g[i][j]=10000;
			for (int i = 0; i < Arc_num i++) {//note non-directional graphs are assigned int a=0,b=0,c=0;
			A=scanner.nextint ();
			B=scanner.nextint ();
			C=scanner.nextint ();
			G[a][b]=c; G[b][a]=c;
	} shortpath (Start_point); public static void ShortPath (int v0) {int pre[]=new int [point_num];//the precursor node for each point int aidall[]=new int [point_num];// Number of rescue teams from beginning to each node int dis_v0[]=new int [point_num];//to start distance int road_num[]=new int [point_num];//to the shortest path to each point Boolean vis[ ]=new boolean[point_num];//this point to access for (int i = 0; i < Point_num; i++) {//Initialize the above variable pre[i]= -1;//first set the predecessors of each point to 1 dis_v
			0[i]=10000;//to the origin of the distance to Maxint aidall[i]=0;//Total Rescue Team set to 0 vis[i]=false;
		road_num[i]=0;//Total Path number set to 0} aidall[v0]=firstaid[v0];//is now the beginning of the rescue team written to Vis[v0]=true;
		Dis_v0[v0]=0;//The origin of the original point to its own distance of 0 road_num[v0]=1;
			for (int i = 0; i < point_num i++) {int yuan_point=v0; for (int j = 0; J < Vis.length J + +) {if (!vis[j]&&10000>dis_v0[j]) {//Find the successor of a point that has already been visited before Yuan_point
					=j;//as the starting point (Origin)}} vis[yuan_point]=true;//has been accessed for (int j = 0; J < Point_num; J + +) {if (!vis[j]) { if (Dis_v0[j]>dis_v0[yuan_point]+g[yuan_point][j]) {/////If the distance from the previous point to the V0 point is greater than the distance from the current point to the original point plusThe distance from this point to the successor Point dis_v0[j]=dis_v0[yuan_point]+g[yuan_point][j];//Update the successor point distance aidall[j]=aidall[yuan_point]+firstaid[j];// To calculate the rescue team that was summoned when the successor was reached pre[j]=yuan_point;//set the optimal precursor point of the successor point for the current point road_num[j]=road_num[yuan_point];//Assign the path number of the pioneer point to the current point}el Se if (dis_v0[j]==dis_v0[yuan_point]+g[yuan_point][j]) {//If the path looks like road_num[j]+=road_num[yuan_point];// Number of paths with the current point path plus the same point as the just-searched distance if (Aidall[j]<aidall[yuan_point]+firstaid[j]) {//If rescue teams are more aidall[j]=aidall[yuan_point ]+firstaid[j];//Update Rescue Team number pre[j]=yuan_point;//also set the precursor point to just the point}}} and System.out.print (Road_num
		[end_point]+ "" +aidall[end_point]+ "\ n");
		Dispath (Pre,end_point);
	System.out.print (End_point);
			public static void Dispath (int a[],int v0) {if (a[v0]!=-1) {Dispath (A, a[v0]);
 System.out.print (a[v0]+ ""), and/or through the predecessor record array, recursively display the number of Paths}}}

Reference Blog: http://blog.csdn.net/strokess/article/details/51339933


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.