社区讨论

dfs 70求调,不明白为什么会有WA

P1433吃奶酪参与者 2已保存回复 4

讨论操作

快速查看讨论及其快照的属性,并进行相关操作。

当前回复
4 条
当前快照
1 份
快照标识符
@m53dq7xe
此快照首次捕获于
2024/12/25 12:12
去年
此快照最后确认于
2025/11/04 12:23
4 个月前
查看原帖
逻辑应该没啥问题啊,emmmm,难道是double的比较出问题了???
CPP
#include <bits/stdc++.h>
using namespace std;
typedef pair<int,int> PII;
vector<PII>q;
const int N =  410;
const int F = 200;
double ans = 1e9;
int n;
bool st[20];
void dfs(int x,int y,int u,double res)
{
    
    if(res >= ans)return;
    if(u == n)
    {
        ans = min(ans,res);
        return;
    }
   
    for(int i = 0; i < n; i ++)
    {
        if(!st[i])
        {
            st[i] = true;
            double dist = sqrt((q[i].first-x)*(q[i].first-x) + (q[i].second-y)*(q[i].second-y));
            dfs(q[i].first,q[i].second,u+1,res + dist);
            st[i] = false;
        }
    }
}

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr),cout.tie(nullptr);
    cin>>n;
    for(int i = 0; i < n; i ++)
    {
        int x,y;cin>>x>>y;
        q.push_back({x,y});
    }
    //这里的排序本来是没打算的,但是超时了一些点我就加上了,想看看有什么效果,但是没有任何效果捏
    sort(q.begin(),q.end(),[&](const PII a,const PII b){
        return a.first*a.first+a.second*a.second < b.first*b.first+b.second*b.second;
    });
    dfs(0,0,0,0);
    cout << fixed << setprecision(2) << ans;
}

回复

4 条回复,欢迎继续交流。

正在加载回复...