[OJ] DS图—图的最短路径

DS图—图的最短路径

题目描述

给出一个图的邻接矩阵,输入顶点v,用迪杰斯特拉算法求顶点v到其它顶点的最短路径。
输入

第一行输入t,表示有t个测试实例

第二行输入顶点数n和n个顶点信息

第三行起,每行输入邻接矩阵的一行,以此类推输入n行

第i个结点与其它结点如果相连则为距离,无连接则为0,数据之间用空格

隔开。第四行输入v0,表示求v0到其他顶点的最短路径距离

以此类推输入下一个示例
输出

对每组测试数据,输出:

每行输出v0到某个顶点的最短距离和最短路径

每行格式:v0编号-其他顶点编号-最短路径值----[最短路径]。没有路径输出:v0编号-其他顶点编号–1。具体请参考示范数据
样例输入
2
5 0 1 2 3 4
0 5 0 7 15
0 0 5 0 0
0 0 0 0 1
0 0 2 0 0
0 0 0 0 0
0
6 V0 V1 V2 V3 V4 V5
0 0 10 0 30 100
0 0 5 0 0 0
0 0 0 50 0 0
0 0 0 0 0 10
0 0 0 20 0 60
0 0 0 0 0 0
V0
样例输出
0-1-5----[0 1 ]
0-2-9----[0 3 2 ]
0-3-7----[0 3 ]
0-4-10----[0 3 2 4 ]
V0-V1–1
V0-V2-10----[V0 V2 ]
V0-V3-50----[V0 V4 V3 ]
V0-V4-30----[V0 V4 ]
V0-V5-60----[V0 V4 V3 V5 ]

#include <iostream>
#include <queue>
#include <vector>
#include <stack>
#include <algorithm>
#include <map>
#include <unordered_map>
using namespace std;
class Node {
public:
	int loc, prev, cost;
	Node(int loc, int prev, int cost) :loc(loc), prev(prev), cost(cost) {
		;
	}
};
struct cmp {
	bool operator ()(Node a, Node b) {
		return a.cost > b.cost;
	}
};
int main() {
	int t;
	cin >> t;
	while (t--) {
		priority_queue<Node,vector<Node>,cmp> pq;
		int n;
		cin >> n;
		vector<int> visited(n, 0);
		vector<int> distance(n, 999999);
		unordered_map<int, int> check;
		vector<int> father(n, -1);
		vector<vector<int>> matrx(n, visited);
		for (int i = 0;i < n;i++) {
			for (int j = 0;j < n;j++) {
				cin >> matrx[i][j];
			}
		}
		int start = 0;
		cin >> start;
		int index = 0;
		cin >> index;
		pq.push({start,start,0});
		int counter = -1;
		int tmp = -1;
		vector<int> res;
		while (!pq.empty()) {
			Node cur = pq.top();
			pq.pop();
			if (visited[cur.loc])continue;
			counter++;
			if (counter == index) {
				tmp = cur.cost;
				res.push_back(cur.loc);
			}
			else if (cur.cost == tmp) res.push_back(cur.loc);
		//	cout <<"counter=:"<<counter<< "  index" << cur.loc << "   prev=" << cur.prev << "   cost=" << cur.cost << endl;
			
			visited[cur.loc] = 1;
			father[cur.loc] = cur.prev;
			distance[cur.loc] = cur.cost;
			check[cur.cost] = cur.loc;
			for (int i = 0;i < n;i++) {
				if (visited[i]) continue;
				else if (matrx[cur.loc][i] != 0) {
					pq.emplace(i, cur.loc, cur.cost + matrx[cur.loc][i]);
				}
			}
			
		}
		sort(res.begin(), res.end());
		for (auto it : res) {
			if (it != *res.begin()) {
				cout << " ";
			}
			cout << it;
		}
		cout << endl;
		
	}
}
```cpp
在这里插入代码片

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
抱歉,作为一个AI语言模型,我没有代码运行的环境,无法提供完整的书借阅(对象数组 构造)的代码。不过我可以提供一个简单的例子来帮助你理解: 假设我们有一个Book类用于保存一本书的基本信息,如书名、作者、出版社等。我们要实现一个类似书馆的系统,可以将书籍加入、借出和归还等。我们可以用一个Book数组来保存所有的书籍,并通过编号来进行借阅操作: ```java public class Book { private String title; private String author; private String publisher; private boolean borrowed; public Book(String title, String author, String publisher) { this.title = title; this.author = author; this.publisher = publisher; this.borrowed = false; } public boolean isBorrowed() { return borrowed; } public void setBorrowed(boolean borrowed) { this.borrowed = borrowed; } // getters and setters for other properties } public class Library { private Book[] books; public Library(Book[] books) { this.books = books; } public void borrow(int bookId) { if (bookId < 0 || bookId >= books.length) { System.out.println("Invalid book ID"); return; } if (books[bookId].isBorrowed()) { System.out.println("This book is already borrowed"); } else { books[bookId].setBorrowed(true); System.out.println("You have successfully borrowed " + books[bookId].getTitle()); } } public void returnBook(int bookId) { if (bookId < 0 || bookId >= books.length) { System.out.println("Invalid book ID"); return; } if (books[bookId].isBorrowed()) { books[bookId].setBorrowed(false); System.out.println("You have successfully returned " + books[bookId].getTitle()); } else { System.out.println("This book is not borrowed"); } } } // main method to demostrate usage public static void main(String[] args) { Book[] books = new Book[3]; books[0] = new Book("Java Programming", "John Doe", "ABC Publications"); books[1] = new Book("Python Programming", "Jane Doe", "XYZ Publications"); books[2] = new Book("C++ Programming", "Jack Doe", "123 Publications"); Library library = new Library(books); // borrow book ID 1 library.borrow(1); // try to borrow book ID 1 again library.borrow(1); // return book ID 0 library.returnBook(0); // try to return book ID 1 (which is not borrowed) library.returnBook(1); } ``` 这个例子中,我们定义了一个Book类,然后创建一个Book数组来保存所有的书籍。借阅和归还操作是通过调用Library类中的方法来实现的。你可以参考这个例子来实现你自己的书借阅程序。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值