1. 程式人生 > >HDU 5017 Ellipsoid(退火模擬)

HDU 5017 Ellipsoid(退火模擬)

Ellipsoid

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 658    Accepted Submission(s): 196
Special Judge

點選開啟題目
Problem Description Given a 3-dimension ellipsoid(橢球面)

your task is to find the minimal distance between the original point (0,0,0) and points on the ellipsoid. The distance between two points (x1
,y1,z1) and (x2,y2,z2) is defined as
Input There are multiple test cases. Please process till EOF.

For each testcase, one line contains 6 real number a,b,c(0 < a,b,c,< 1),d,e,f(0 ≤ d,e,f < 1), as described above. It is guaranteed that the input data forms a ellipsoid. All numbers are fit in double.

Output For each test contains one line. Describes the minimal distance. Answer will be considered as correct if their absolute error is less than 10-5
.
Sample Input 1 0.04 0.01 0 0 0
Sample Output 1.0000000
Source  

參照了網上的程式,自己寫了一遍。

利用退貨模擬搜尋最優解;
這是一個不斷搜尋最優解的過程,知道最後達到要求的精度的時候不再搜尋了就;
程式碼:
#include <iostream>
#include <math.h>
#include <stdio.h>
#include <string.h>
#define INF 0x3f3f3f3f
#define N 8            
using namespace std;
double a,b,c,d,e,f;
const double r=0.99,eps=1e-9;///r是降溫速度,eps是誤差
int dir[][2]={{0,1},{0,-1},{1,0},{-1,0},{1,1},{1,-1},{-1,1},{-1,-1}};///搜尋的八個方向(也可以是四個)
double dis(double x,double y,double z)
{
    return sqrt(x*x+y*y+z*z);
}
double getz(double x,double y)///根據x,y獲取z的值(二元一次方程)
{
    double A=c;
    double B=d*y+e*x;
    double C=a*x*x+b*y*y+f*x*y-1;
    double detal=B*B-4*A*C;
    if(detal<eps)
        return INF;
    return (sqrt(detal)-B)/(2*A);
}
double slove()
{
    int i;
    double step=1;
    double x=0,y=0,z;
    while(step>eps)///步長開始降溫
    {
        z=getz(x,y);
        for(i=0; i<N; i++)
        {
            double nx=x+step*dir[i][0];
            double ny=y+step*dir[i][1];
            double nz=getz(nx,ny);///獲取下一個x,y,z;
            if(dis(nx,ny,nz)<dis(x,y,z))///找最優解
                x=nx,y=ny,z=nz;
        }
        step*=r;///降溫
    }
    return dis(x,y,z);
}
int main()
{
    double ans;
    while(~scanf("%lf%lf%lf%lf%lf%lf",&a,&b,&c,&d,&e,&f))
    {
        ans=slove();
        printf("%.8lf\n",ans);
    }
    return 0;
}


相關推薦

HDU 5017 Ellipsoid退火模擬

Ellipsoid Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 658    Accepted Submis

HDU 5017 Ellipsoid模擬退火

Sample Output 1.0000000 【思路分析】   求橢球面上的點到原點的最短距離。網上很多說是模擬退火,個人感覺此題區域性最優解就是全域性最優解,即結果是一個單峰函式,頂多算個爬山演算法。 程式碼如下:#include <iostream> #include <cstd

HDU 5017 Ellipsoid西安網路賽K題

HDU 5017 Ellipsoid 題目連結 思路:模擬退火大法好! 程式碼: #include <cstdio> #include <cstring> #include <cmath> #include <algorit

HDU 1107 武林模擬

模擬題,不說啥了,心碎了。一下午就扣這一題了。原因是 題意沒給明白,只有不懂門派中有2人站在同一點上才會發生戰鬥!!!!! #include <stdio.h> #include <string.h> #include <a

hdu 5017 Ellipsoid 模擬退火演算法 西安網路賽

For each test contains one line. Describes the minimal distance. Answer will be considered as correct if their absolute error is less than 10-5.比賽的時候想到的是用拉

HDU - 4803 Poor Warehouse Keeper 貪心模擬

Jenny is a warehouse keeper. He writes down the entry records everyday. The record is shown on a screen, as follow:  There are only two b

HDU的一些二分和三分的一些題目大部分模擬

大家如果對於二分和三分沒有把握的話可以多練練,我其他的部落格文章裡面也有關於二分和三分的一些解釋。Can you solve this equation?(HDU 2199)   Strange fuction(HDU 2899)    Pie(HDU 1969)    To

HDU 1047(大數相加)陣列模擬

  這個題目啊,其實紮實的做的話應該並不難,然而我想看是統一用字串,還是再轉化成陣列,然後看了別人的題解,傻冒一樣的像別人一樣謝了getchar();真是傻冒了,我用的scanf("%s",str),

CodeForces 321A Ciel and Robot數學模擬

ont amp force 等於 return printf print -- 題意 題目鏈接:http://codeforces.com/problemset/problem/321/A 題意:在一個二維平面中,開始時在(0,0)點,目標點是(a。b),問能不能通過反

HDU 4386 Quadrilateral數學啊

algo data 長度 please sca wan post play dsm 題目鏈接:http://acm.hdu.edu.cn/showproblem.php?pid=4386 Problem Description   One day the li

HDU 6053 TrickGCD分塊

%d space 復雜 cstring 前綴 == str 結果 logs 【題目鏈接】 http://acm.hdu.edu.cn/showproblem.php?pid=6053 【題目大意】   給出一個數列每個位置可以取到的最大值,   問這個

hdu 6112 今夕何夕(模擬

courier uri sin 包含 title chmod mon log space 今夕何夕 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Tot

Codeforces 848B Rooter's Song分類+模擬

continue ++i begin printf oot log http define bool 題目鏈接 Rooter‘s Song 題意 有n個舞者站在x軸上或y軸上,每個人有不同的出發時間。x軸上的舞者垂直x軸正方向移動,y軸上的舞者垂直y軸正方向移動。 當

大魚吃小魚簡單模擬

一個 入棧 nco http spa println code tar pre 題目:http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1289 從左往右將數字壓入棧裏(想象成一個水平向右的棧),如果

刷題總結——mathNOIP模擬

情況 math temp for names namespace ios 個數 std 題目: 給定兩個數字n,求有多少個數字b滿足a^b和b^a同余於2^n,其中n<=30,a<=10^9, 題解: 挺巧妙的一道題··

刷題總結——做運動NOIP模擬

time 復雜度 bsp i++ top oid reg 第一次 現在 題目: 給定一個無向圖,節點數n<=50000,m<=1000000,每條邊有兩個值t和c,邊的長度為t*c···現在要求再t盡量小的情況下,

【UVA】1594 Ducci Sequence模擬

i++ mar freopen esp abs mat ret code == 題目 題目 ? ? 分析 真的快瘋了,中午交了一題WA了好久,最後發現最後一個數據不能加\n,於是這次學乖了,最後一組不輸出\n,於是WA了好幾發,最後從Udebug發現最後一組是要輸出的!!

hdu 5033 buiding單調棧

com atan namespace 頂上 嚴格 include query 關系 位置 hdu 5033 buiding(單調棧) 某年某月某天,馬特去了一個小鎮。這個小鎮如此狹窄,以至於他可以把小鎮當作一個樞紐。在鎮上有一些摩天大樓,其中一棟位於xi,高度為hi。所有

淺談JavaScript的事件事件模擬

指定 事件 func edt 創建 over asset pat 鼠標   事件經常由操作或者通過瀏覽器功能觸發,通過JavaScript也可以觸發元素的事件。通過JavaScript觸發事件,也稱為事件的模擬。 DOM中事件模擬   可以document的create

hdu 5521 Meeting最短路

names n) can style air ima pid hid con 題目鏈接:http://acm.hdu.edu.cn/showproblem.php?pid=5521 題意:有1-n共n個點,給出m個塊(完全圖),並知道塊內各點之間互相到達花費時