牛客小白月赛 22 D.收集纸片

Anna ·
更新时间:2024-09-21
· 991 次阅读

题目连接

https://ac.nowcoder.com/acm/contest/4462/D

一开始还想的排序什么鬼,并不是,再一看数据范围,爆搜吧~但是调试了不少时间,码力码力码力!!!

如果可以的话,以后会写一下状态压缩版本,还一点不会。。

更新一下 ,这个代码虽然ac了,我感觉有错误的地方,应该是数据不太行。在这个搜索中我默认拾完最后一个纸片距离最短然后记录最后一个点,再用这个搜到的最短距离+记录的最后一个点和起始点的距离就是答案,明显有问题。

#include #include #include #include #include #include #include #include #include #include #include #include #define IO \ // ios::sync_with_stdio(false); \ // cin.tie(0); \ // cout.tie(0); using namespace std; typedef long long LL; const int maxn = 1e5 + 10; const int inf = 0x3f3f3f3f; struct Node { int x, y; } node[maxn]; int vis[maxn]; int sx, sy; int f; int ans = 0; int t = 0; int T, n, m, k; void dfs(int s, int n) { if (n == 0) { if (t < ans) { f = s; ans = t; return; } return; } for (int i = 1; i > T; while (T--) { cin >> n >> m; cin >> node[0].x >> node[0].y; cin >> k; for (int i = 1; i > node[i].x >> node[i].y; t = 0; ans = inf; memset(vis, 0, sizeof vis); dfs(0, k); // cout << f << endl; ans += abs(node[f].x - node[0].x) + abs(node[f].y - node[0].y); cout << "The shortest path has length " << ans << endl; } return 0; }

正解

#include #include #include #include #include #include #include #include #include #include #include #include #define IO \ // ios::sync_with_stdio(false); \ // cin.tie(0); \ // cout.tie(0); using namespace std; typedef long long LL; const int maxn = 1e5 + 10; const int inf = 0x3f3f3f3f; struct Node { int x, y; } node[maxn]; int vis[maxn]; int sx, sy; int f; int ans = 0; int t = 0; int T, n, m, k; void dfs(int s, int n) { if (n == 0) { t += abs(node[s].x - node[0].x) + abs(node[s].y - node[0].y); if (t < ans) { f = s; ans = t; t -= abs(node[s].x - node[0].x) + abs(node[s].y - node[0].y); return; } t -= abs(node[s].x - node[0].x) + abs(node[s].y - node[0].y); return; } for (int i = 1; i > T; while (T--) { cin >> n >> m; cin >> node[0].x >> node[0].y; cin >> k; for (int i = 1; i > node[i].x >> node[i].y; t = 0; ans = inf; memset(vis, 0, sizeof vis); dfs(0, k); // cout << f << endl; // ans += abs(node[f].x - node[0].x) + abs(node[f].y - node[0].y); cout << "The shortest path has length " << ans << endl; } return 0; }
作者:TTP1128



牛客

需要 登录 后方可回复, 如果你还没有账号请 注册新账号