P7936 dfs 搜索求调,悬关
查看原帖
P7936 dfs 搜索求调,悬关
799877
AK_heaven楼主2024/5/9 21:15

样例都能过,但是全部都 wa。

#include <bits/stdc++.h>

#define rep(a, b, c) for(int c = a; c <= b; c++)
#define rer(a, b, c) for(int c = a; c >= b; c--)
#define rG(u) for(int i = h[u], v = e[i].v; i; i = e[i].last, v = e[i].v)
#define ls (p<<1)
#define rs (p<<1|1)

using namespace std;

const int maxn = 3e5 + 10;

int n, k, ex, ey, dx[maxn], dy[maxn], sx, sy, sq;

vector<pair<int, int> > X[maxn], Y[maxn];

vector<pair<int, int>> ans;

void dfs(int x, int y, int q) {
//	cout << q << '\n';
	if(x == ex && y == ey) {
		ans.push_back({x, y});
		cout << q << '\n';
		cout << ans.size() << '\n';
		for(int i = 0; i < ans.size()-1; i++)
		  cout << ans[i].first << ' ' << ans[i].second << '\n';
        cout << ans[ans.size()-1].first << ' ' << ans[ans.size()-1].second << '\n';
		exit(0);
	}
	if(q < k) return ;
	q -= k;
	ans.push_back({x, y});
	for(int i = dx[x]; i < X[x].size(); i++)
	  if(y < X[x][i].first) dfs(x, X[x][i].first, q+X[x][i].second);
	for(int i = dy[y]; i < Y[y].size(); i++)
	  if(x < Y[y][i].first) dfs(Y[y][i].first, y, q+Y[y][i].second);
	ans.pop_back();
}

int main() {
	ios::sync_with_stdio(false);
	cin.tie(0); cout.tie(0);
	cin >> n >> k;
	int x, y, z;
	rep(1, n, i) {
	  cin >> x >> y >> z, X[x].push_back({y, z}), Y[y].push_back({x, z});
	  if(i == 1)
	    sx = x, sy = y, sq = z;
	  if(i == n)
	    ex = x, ey = y;
	}
	rep(1, n, i)
	  sort(X[i].begin(), X[i].end()),
	  sort(Y[i].begin(), Y[i].end());
	dfs(sx, sy, sq);
	return 0;
}
2024/5/9 21:15
加载中...