• uva216-枚举-简单题


    题意:n个计算机通过电缆连接,怎么连接使用的电缆最少

    mmp,死wa不过,memset(vis,0,sizeof(per)),太不小心了

    #include <iostream>
    #include <memory.h>
    #include <stdio.h>
    #include<math.h>
    using namespace std;
    
    struct Node
    {
        int x;
        int y;
        Node()
        {
            x = 0;
            y = 0;
        }
    };
    const int N = 9;
    Node per[N];
    int res[N];
    int n;
    double final = 0x7FFFFFFF;
    int vis[N];
    double px(Node* n1, Node* n2)
    {
        return sqrt(
                0.0 + (n1->x - n2->x) * (n1->x - n2->x)
                        + (n1->y - n2->y) * (n1->y - n2->y)) + 16.00;
    }
    
    void dfs(int a[], int cur)
    {
        if (cur == n)
        {
            double sum = 0;
            for (int i = 0; i < n - 1; i++)
                sum += px(per + a[i], per + a[i + 1]);
            if (sum < final)
            {
                final = sum;
                memcpy(res, a, sizeof(res));
            }
            return;
        }
        for (int i = 0; i < n; i++)
        {
            if (vis[i])
                continue;
            vis[i] = 1;
            a[cur] = i;
            dfs(a, cur + 1);
            vis[i] = 0;
        }
    }
    int main()
    {
        //freopen("d:\1.txt", "r", stdin);
        int t = 1;
        while (cin >> n && n)
        {
            memset(per, 0, sizeof(per));
            memset(vis, 0, sizeof(vis));
            memset(res,0,sizeof(res));
            final = 0x7FFFFFFF;
            int s, e;
            for (int i = 0; i < n; i++)
            {
                cin >> s >> e;
                Node p;
                p.x = s;
                p.y = e;
                per[i] = p;
            }
            int a[N];
            dfs(a, 0);
            cout << "**********************************************************"
                    << endl;
            cout << "Network #" << t << endl;
            for (int i = 0; i < n - 1; i++)
            {
                double tt = px(per + res[i], per + res[i + 1]);
                printf(
                        "Cable requirement to connect (%d,%d) to (%d,%d) is %.2lf feet.
    ",
                        per[res[i]].x, per[res[i]].y, per[res[i + 1]].x,
                        per[res[i + 1]].y, tt);
            }
            printf("Number of feet of cable required is %.2f.
    ", final);
            t++;
        }
    
        return 0;
    }
  • 相关阅读:
    集成学习(一):概述
    机器学习:处理非平衡数据集的办法
    支撑向量机 SVM(一)
    集成学习(五):xgboost 学习总结
    数组的实现(重载[]、=、==、!=运算符重载)
    运算符重载总结
    运算符重载进阶
    运算符重载入门demo
    类模板的简单使用
    static成员变量和static成员函数例程
  • 原文地址:https://www.cnblogs.com/shuiyonglewodezzzzz/p/7716318.html
Copyright © 2020-2023  润新知