dfs 70求调,不明白为什么会有WA
  • 板块P1433 吃奶酪
  • 楼主kendas
  • 当前回复2
  • 已保存回复2
  • 发布时间2024/12/25 12:11
  • 上次更新2024/12/25 19:15:35
查看原帖
dfs 70求调,不明白为什么会有WA
1428433
kendas楼主2024/12/25 12:11

逻辑应该没啥问题啊,emmmm,难道是double的比较出问题了???

#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;
}
2024/12/25 12:11
加载中...