Catalog
  1. 1. 数据是这样给出的:
图的深度优先遍历

数据是这样给出的:

好了,下面附上代码。

#include<iostream>
#include<algorithm>
#include<cstdlib>
using namespace std;
const int limit = 999999;
int min_ = limit, book[101], n, e[101][101];
void dfs(int cur, int dis){
int j;
if (dis > min_) return;
if (cur == n) {
if (dis < min_) min_ = dis;
return;
}
for (j = 1; j <= n; j++) {
if (e[cur][j] != limit && book[j] == 0) {
book[j] = 1;
dfs(j, dis + e[cur][j]);
book[j] = 0;
}
}
return;

}
int main() {
int m, a, b, c;
cin >> n >> m;
for (int i = 1; i <= n; i++)
for (int j = 1; j <= n; j++)
if (i == j) e[i][j] = 0;
else e[i][j] = limit;
for (int i = 1; i <= m; i++) {
cin >> a >> b >> c;
e[a][b] = c;
}
book[1] = 0;
dfs(1, 0);
cout << min_;

return 0;
}
Author: superzhaoyang
Link: http://yoursite.com/2019/09/25/图的深度优先遍历/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.
Donate
  • 微信
  • 支付宝

Comment