【POJ2420】A star not a tree?

蒟蒻开始学模拟退火……

起初一直不肯学,因为毕竟玄学算法……

哎呀玄学怎么就没用呢?对不对?

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<cstdlib>
#include<cmath>
#include<ctime>
using namespace std;
int n;
double xx,yy,ans,T;
struct Point{double x,y;}p[110];
inline double sqr(double x){return x*x;}
inline double dis(double x,double y,Point p){
    return sqrt(sqr(x-p.x)+sqr(y-p.y));
}
inline double getsum(double x,double y){
    double ret=0;
    for(int i=1;i<=n;i++)ret+=dis(x,y,p[i]);
    return ret;
}
int main(){
    srand(time(NULL));
    while(~scanf("%d",&n)){
        xx=yy=0;ans=1e20;T=100000;
        for(int i=1;i<=n;i++){
            scanf("%lf%lf",&p[i].x,&p[i].y);
            xx+=p[i].x;yy+=p[i].y;
        }
        xx/=n;yy/=n;ans=getsum(xx,yy);
        double tmp,x,y;
        while(T>=0.02){
            x=y=0;
            for(int i=1;i<=n;i++){
                x+=(p[i].x-xx)/dis(xx,yy,p[i]);
                y+=(p[i].y-yy)/dis(xx,yy,p[i]);
            }
            tmp=getsum(xx+x*T,yy+y*T);
            if(tmp<ans){ans=tmp;xx+=x*T;yy+=y*T;}
            else if(log((tmp-ans)/T)<(rand()%10000)/10000.0)
            {ans=tmp;xx+=x*T;yy+=y*T;}
            T*=0.9;
        }
        printf("%.0lf\n",ans);
    }
}
时间: 2024-08-26 02:47:20

【POJ2420】A star not a tree?的相关文章

【DataStructure】Description and Introduction of Tree

[Description] At ree is a nonlinear data structure that models a hierarchical organization. The characteristic eatures are that each element may have several successors (called its "children") and every element except one (called the "root&

【LeetCode】 Maximum Depth of Binary Tree

Maximum Depth of Binary Tree  Given a binary tree, find its maximum depth. The maximum depth is the number of nodes along the longest path from the root node down to the farthest leaf node. 递归基础,不解释. class Solution { public: int getMax(TreeNode *root

【HDOJ】1325 Is It A Tree?

并查集.需要考虑入度. 1 #include <stdio.h> 2 #include <string.h> 3 4 #define MAXNUM 10005 5 6 int bin[MAXNUM]; 7 int degree[MAXNUM]; 8 int nums[MAXNUM]; 9 10 int find(int x) { 11 int r = x; 12 13 while (bin[r] != r) 14 r = bin[r]; 15 16 return r; 17 } 1

【BZOJ1803】Spoj1487 Query on a tree III 主席树+DFS序

[BZOJ1803]Spoj1487 Query on a tree III Description You are given a node-labeled rooted tree with n nodes. Define the query (x, k): Find the node whose label is k-th largest in the subtree of the node x. Assume no two nodes have the same labels. Input

【LeetCode】Minimum Depth of Binary Tree 二叉树的最小深度 java

[LeetCode]Minimum Depth of Binary Tree Given a binary tree, find its minimum depth. The minimum depth is the number of nodes along the shortest path from the root node down to the nearest leaf node. 递归和非递归,此提比较简单.广度优先遍历即可.关键之处就在于如何保持访问深度. 下面是4种代码: 1

【BZOJ4530】大融合(Link-Cut Tree)

[BZOJ4530]大融合(Link-Cut Tree) 题面 讨厌权限题!!! Loj链接 题目描述 小强要在 N个孤立的星球上建立起一套通信系统.这套通信系统就是连接 N个点的一个树.这个树的边是一条一条添加上去的.在某个时刻,一条边的负载就是它所在的当前能够联通的树上路过它的简单路径的数量. 例如,在上图中,现在一共有五条边.其中,(3,8)这条边的负载是 6,因为有六条简单路径 2?3?8, 2?3?8?7, 3?8, 3?8?7, 4?3?8, 4?3?8?72-3-8,?2-3-8-

【BZOJ2843】极地旅行社(Link-Cut Tree)

[BZOJ2843]极地旅行社(Link-Cut Tree) 题面 BZOJ 题解 \(LCT\)模板题呀 没什么好说的了.. #include<iostream> #include<cstdio> #include<cstdlib> #include<cstring> #include<cmath> #include<algorithm> #include<set> #include<map> #includ

【POJ 2420】A Star not a Tree?

A Star not a Tree? Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 3929   Accepted: 1952 Description Luke wants to upgrade his home computer network from 10mbs to 100mbs. His existing network uses 10base2 (coaxial) cables that allow you

【LeetCode】98. Validate Binary Search Tree

Difficulty:Medium  More:[目录]LeetCode Java实现 Description https://leetcode.com/problems/validate-binary-search-tree/ Given a binary tree, determine if it is a valid binary search tree (BST). Assume a BST is defined as follows: The left subtree of a nod