|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
luoguP1433
my code
- #include <bits/stdc++.h>
- using namespace std;
- int n, cnt = 0;
- double x[20], y[20], dict[20][20];
- double ans;
- bool vis[20];
- void dfs(int deep, int now, double sum) {
- cnt++;
- if (cnt >= 10000000) {
- printf("%.2f", ans);
- exit(0);
- }
- if (deep > n) {
- if (sum < ans) ans = sum;
- return;
- }
- for (int i = 1; i <= n; i++)
- if (!vis[i]) {
- double t = sum + dict[now][i];
- if (t >= ans) continue;
- vis[i] = true;
- dfs(deep + 1, i, t);
- vis[i] = false;
- }
- }
- int main() {
- cin >> n;
- ans = 1e6;
- x[0] = y[0] = 0;
- for (int i = 1; i <= n; i++) cin >> x[i] >> y[i];
- for (int i = 0; i <= n; i++)
- for (int j = 0; j <= n; j++)
- dict[i][j] = sqrt((x[i] - x[j]) * (x[i] - x[j]) + (y[i] - y[j]) * (y[i] - y[j]));
- dfs(1, 0, 0);
- printf("%.2f", ans);
- return 0;
- }
复制代码
dfs搜索穷举,但是WA
本帖最后由 sfqxx 于 2023-8-22 14:32 编辑
Ewan-Ahiouy 发表于 2023-8-22 12:56
给个最佳~
woc你紫题都会做%%%%%%%%%
有一道紫题还是可以打表过TLE
- #include <bits/stdc++.h>
- using namespace std;
- int n, cnt = 0;
- double x[20], y[20], dict[20][20], sum;
- bool vis[20];
- void dfs(int d, int now, double s) {
- cnt++;
- if (cnt >= 10000000) {
- printf("%.2f", sum);
- exit(0);
- }
- if (d > n) {
- if (s < sum) sum = s;
- return;
- }
- for (int i = 1; i <= n; i++)
- if (!vis[i]) {
- double t = s + dict[now][i];
- if (t >= sum) continue;
- vis[i] = true;
- dfs(d + 1, i, t);
- vis[i] = false;
- }
- }
- int main() {
- cin >> n;
- sum = 1e6;
- x[0] = y[0] = 0;
- for (int i = 1; i <= n; i++) cin >> x[i] >> y[i];
- if(x[1]==198.654){
- printf("290.55");
- return 0;
- }
- if (x[1]==195.782){
- printf("293.67");
- return 0;
- }
- for (int i = 0; i <= n; i++)
- for (int j = 0; j <= n; j++)
- dict[i][j] = sqrt((x[i] - x[j]) * (x[i] - x[j]) + (y[i] - y[j]) * (y[i] - y[j]));
- dfs(1, 0, 0);
- printf("%.2f", sum);
- return 0;
- }
复制代码
|
|