通俗易懂的图的遍历

题目大意

给定三个数 为 A 点到 的唯一时间, 点到 点的唯二时间,求 点的最短时间

解法

可以建一个有向图,将 连向 连向 ,然后跑一边图的遍历,每次遍历时,记录一下时间,最后到达 点后记录最小值

代码来咯:

//
#include <bits/stdc++.h>
#define ll long long
#define LM LLONG_MAX
#define IM INT_MAX
#define IMN INT_MIN
#define LMN LLONG_MIN
#define tr true
#define fa false 

using namespace std;

const int Maxn = 10;
vector<int> g[Maxn];
int ans = IM, a[Maxn];
struct node{
	int x, y;
}b[Maxn];

void dfs(int x, int time){
	if(x == 3){
		ans = min(ans, time);
		return ;
	}
	for(int i = 0; i <= g[x].size(); i++){   //遍历图 
		dfs(x + 1, b[x].x + time);
		dfs(x + 1, b[x].y + time);
	}
}

int main(){
//  freopen(".in", "r", stdin);
//  freopen(".out", "w", stdout);
  for(int i = 1; i <= 3; i++){
  	cin >> a[i];
  }
  b[1].x = b[1].y = a[1];		//初始化图
  b[2].x = a[2];
  b[2].y = a[3];
  g[1].push_back(2);
  g[2].push_back(3);
  dfs(1, 0);
  cout << ans << endl;
  return 0;
}  

如有错误,请指出,谢谢🙏