⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 graphprogram1.java

📁 Classes composed of directed weighted graph.
💻 JAVA
字号:

import java.io.*;
import java.util.*;

public class GraphProgram1 {
	
	public static void main(String[] args)  throws IOException {
		
		BufferedReader stdin = new BufferedReader(new FileReader("graph2.in"));
		
		String input = stdin.readLine();
		
		String[] tmp = input.split(" ");
		
		int number = Integer.parseInt(tmp[0]);
		int start = Integer.parseInt(tmp[1]);
		int end = Integer.parseInt(tmp[2]);
		
		int[] list = new int[number+1];
		
		WeightedGraph theGraph = new DirectedWeightedGraph(number);
				
		while (stdin.ready()) {
			input = stdin.readLine();
			String[] tokens = input.split(" ");
			
			int num1 = Integer.parseInt(tokens[0]);
			int num2 = Integer.parseInt(tokens[1]);
			int weight = Integer.parseInt(tokens[2]);
			int cost = Integer.parseInt(tokens[3]);
			
			Vertex v1 = new Vertex(num1);
			Vertex v2 = new Vertex(num2);
			
			if(!search(list,v1.el)) {
				list[num1]=1;
				theGraph.incrementVertices();
			}
			
			if(!search(list,v2.el)) {
				list[num2]=1;
				theGraph.incrementVertices();
			}
			
			
			WeightedEdge e1 = new WeightedEdge(v1, v2, weight, cost);
			theGraph.addEdge(e1);	
		}
	     
	   
		System.out.println();
		System.out.println("No. of vertices = " + theGraph.getVertices());
		System.out.println("No. of edges = " + theGraph.getEdges());
		
		System.out.print("Vertices : ");
		displayVertices(theGraph);
		System.out.println();
		
		System.out.println("Edges : " );
		displayEdges(theGraph);
		System.out.println();
		theGraph.maxFlowMinCostAlgorithm(start, end);
		
	}  //end main
	
		
	static boolean search(int[] list, int number) {
		if(list[number] == 1)
			return true;
		return false;
	}
	
	static void displayVertices(Graph theGraph){
	
		Vector vertexList = theGraph.getVertexList();
		
		for(int i=0; i<vertexList.size(); i++)
			System.out.print((Vertex) vertexList.elementAt(i));
	}
	
	static void displayEdges(Graph theGraph){
	
	 	//list of edges
		Vector edgeList = theGraph.getEdgeList();
		
		for(int i=0; i<edgeList.size(); i++)
			System.out.println((Edge) edgeList.elementAt(i));

	}
	
		
}  //end GraphProgram1


⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -