poj1125之floyd

/*floyd*/
#include <iostream>
#define N 201
//sometimes it(Inifite we define) plus an integer,if it is 0x7fffffff,it will overflow and 
//becomes an negative integer.And it will be smaller than anyone.
//Don't use memset to initial an array,it works on binary.If the array is supposed to be 1 or 0,
//the memset function will be a good choice.
//I make mistakes when I come to find Max and find Min actually!The exchange of index confuse me.OMG...
//Every single column or row can be treated as a point to the others in floyd algorithm.
//The path is the shortest.
#define Inifite 0x3fffffff
using namespace std;
int edge[N][N];
int numOfBrokers;

int smaller(int a,int b)
{
	return a>b?b:a;
}
int findOneMax(int i)
{
	int index=1;
	for (int j=1;j<=numOfBrokers;++j)
	{
		if(edge[i][index]<edge[i][j])
			index=j;
	}
	return index;
}
void Initial()
{
	for (int i=1;i<=numOfBrokers;i++)
	{
		for (int j=1;j<=numOfBrokers;j++)
		{
			if(i==j) edge[i][j]=0;
			else edge[i][j]=Inifite;
		}
	}
}
void floyd()
{
	int minLength=Inifite,minIndex=1;
	for (int k=1;k<=numOfBrokers;++k)
	{
		int columnMax;
		for (int i=1;i<=numOfBrokers;++i)
		{
			for (int j=1;j<=numOfBrokers;++j)
			{
				edge[i][j]=smaller(edge[i][j],edge[i][k]+edge[k][j]);
			}
			columnMax=findOneMax(i);//the maxIndex of the i-st column
			if (edge[i][columnMax]<minLength)
			{
				minIndex=i;
				minLength=edge[i][columnMax];
			}
		}
	}
	if (minLength==Inifite)
		cout << "disjoint" << endl;
	else cout << minIndex << " " << minLength << endl;
}
int main()
{
	//freopen("in.txt","r",stdin);
	int from,to,costMinutes,j=1;
	while (cin >> numOfBrokers &&numOfBrokers)
	{
		//situation just like this:has many times inputs,remember initial some 
		//necessary variations at first,especially counters or outputs
		int minLength=Inifite,minIndex=1,temp,j=1;//every time j will be set as 1;
		Initial();
		while (j<=numOfBrokers)
		{	
			int contracts,i=1;
			cin >> contracts;
			while (i<=contracts)
			{
				from=j;
				cin >> to >> costMinutes;
				edge[from][to]=costMinutes;
				++i;
			}
			++j;
		}
		floyd();
	}
}


1) { JOptionPane.showMessageDialog(this, "请选择要删除的图书!", "提示", JOptionPane.WARNING_MESSAGE); } else { 这是一道经典的最短路问题,可以使用Dijkstra算法或者Floyd算法来解决。 D String bookId = bookInfoTable.getValueAt(selectedRow, 0).toString(); int option = JOptionPane.showConfirmDialog(this, "确定要删除编号为 " + bookId + " 的图书吗?", "提示", JOptionPane.YES_NO_OPTION); if (optionijkstra算法的基本思想是从起点开始,依次遍历所有节点,每次选择距离起点最 == JOptionPane.YES_OPTION) { deleteBookInfo(bookId); loadBookInfo(); } } } else if (e.getSource() == backButton) { new MainFrame(); setVisible(false); } } private void loadBookInfo() { 近的节点进行松弛操作,直到到达终点或者所有节点都被遍历完。 Floyd算法则 tableModel.setRowCount(0); Connection conn = null; Statement stmt = null; ResultSet rs = null; try { 是通过动态规划求解所有节点之间的最短路径。具体来说,利用一个二维数组存 conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/book_management?useSSL=false&serverTimezone=UTC", "root", "root"); stmt = conn.createStatement(); rs = stmt.executeQuery("SELECT * FROM book_info"); while (rs.next())储任意两个节点之间的最短距离,然后依次考虑增加中间节点,更新所有节点 { Object[] rowData = new Object[6]; rowData[0] = rs.getInt("book_id"); rowData[1]之间的距离,直到所有节点都被考虑过。 无论使用哪种算法,都需要先构建 = rs.getString("book_name"); rowData[2] = rs.getString("author"); rowData[3] = rs.getString("publisher"); rowData[4] = rs.getDouble("price"); rowData[5] = rs.getInt("quantity"); tableModel.addRow(row出图的邻接矩阵或邻接表,然后根据算法的要求进行处理即可。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值