(java)Pat刷题日记-1003 Emergency (25 分)

一看到题目就知道其实是最短路径问题
但是不知道用Java怎么写图。。

所以去看了一下Java的书
感觉c语言的结构提可以用Java的类来表示

class Vertex
{
	public int numT;
	public boolean wasVisited;
	
	public Vertex(int num) {
		numT=num;
		wasVisited = false;
	}
}



class Graph
{
	private final int MAX_VERTS = 505;
	private final int INFINITY = 100000;
	public Vertex vertexlis[];
	public int adjMat[][];
	private int nVerts;


	public Graph()
	{
		vertexlis = new Vertex[MAX_VERTS];
		adjMat = new int [MAX_VERTS][MAX_VERTS];
		nVerts = 0;
		for(int j = 0;j

用类的形式来构造图的结构
接下来就是最短路径问题
就运用Dijkstra算法

		for(int i = 0 ;idist[index]+citys.adjMat[index][k])
					{
						
						dist[k]=dist[index]+citys.adjMat[index][k];
						num[k]=num[index];
						w[k]=w[index]+citys.vertexlis[k].numT;
					}
					else if(dist[k]==dist[index]+citys.adjMat[index][k])
					{
						
						num[k]=num[index]+num[k];
						if(w[index]+citys.vertexlis[k].numT > w[k])
						{
							w[k] = w[index]+citys.vertexlis[k].numT;
						}
					}
				}
			}
		}
在这里插入代码片

全部代码如下

import java.util.Arrays;
import java.util.Scanner;

import javax.crypto.CipherInputStream;
import javax.swing.plaf.basic.BasicInternalFrameTitlePane.MaximizeAction;
import javax.swing.text.html.HTMLDocument.HTMLReader.SpecialAction;
class Vertex
{
	public int numT;
	public boolean wasVisited;
	
	public Vertex(int num) {
		numT=num;
		wasVisited = false;
	}
}



class Graph
{
	private final int MAX_VERTS = 505;
	private final int INFINITY = 100000;
	public Vertex vertexlis[];
	public int adjMat[][];
	private int nVerts;


	public Graph()
	{
		vertexlis = new Vertex[MAX_VERTS];
		adjMat = new int [MAX_VERTS][MAX_VERTS];
		nVerts = 0;
		for(int j = 0;jdist[index]+citys.adjMat[index][k])
					{
						
						dist[k]=dist[index]+citys.adjMat[index][k];
						num[k]=num[index];
						w[k]=w[index]+citys.vertexlis[k].numT;
					}
					else if(dist[k]==dist[index]+citys.adjMat[index][k])
					{
						
						num[k]=num[index]+num[k];
						if(w[index]+citys.vertexlis[k].numT > w[k])
						{
							w[k] = w[index]+citys.vertexlis[k].numT;
						}
					}
				}
			}
		}

		System.out.print(num[C2] + " " + w[C2]);
	
}
}

你可能感兴趣的:((java)Pat刷题日记-1003 Emergency (25 分))