返回题解分享
讨论 / 题解分享/ 帖子详情

海贼王之伟大航路 - 题解

开O2优化可以用dfsAC



#include <bits/stdc++.h>
using namespace std;

typedef long long LL;

const int N = 21;

int n;
int d[N][N];
bool st[N];
LL ans = 0x3f3f3f3f;

void dfs(int u, int cnt, LL cost)// 当前所在岛屿, 总共经过了多少个岛屿, 经过cnt个岛屿的花费
{
	if(cost > ans) return;
	if (cnt == n && u == n)
	{
		ans = min(ans, cost);
		return;
	}

	for (int i = 1; i <= n; i++)
	{
		if (i == u) continue;
		if (st[i]) continue;
		if (d[u][i] >= ans) continue;

		st[i] = true;
		dfs(i, cnt + 1, cost + d[u][i]);
		st[i] = false;

	}
}

void solve()
{
	cin >> n;
	for (int i = 1; i <= n; i++)
		for (int j = 1; j <= n; j++)
			cin >> d[i][j];
			
	st[1] = true;
	dfs(1, 1, 0);
}

int main()
{
	solve();
	cout << ans << endl;
	return 0;
}
0 回复 0 转发 2 喜欢 2 阅读
回复 (0)
默认 最新
暂无回复,快来抢沙发!