码迷,mamicode.com
首页 > 其他好文 > 详细

POJ - 2420 A Star not a Tree?

时间:2017-11-11 13:02:31      阅读:187      评论:0      收藏:0      [点我收藏+]

标签:lis   tran   move   平面   tun   ati   sans   i++   nas   

Luke wants to upgrade his home computer network from 10mbs to 100mbs. His existing network uses 10base2 (coaxial) cables that allow you to connect any number of computers together in a linear arrangement. Luke is particulary proud that he solved a nasty NP-complete problem in order to minimize the total cable length. 
Unfortunately, Luke cannot use his existing cabling. The 100mbs system uses 100baseT (twisted pair) cables. Each 100baseT cable connects only two devices: either two network cards or a network card and a hub. (A hub is an electronic device that interconnects several cables.) Luke has a choice: He can buy 2N-2 network cards and connect his N computers together by inserting one or more cards into each computer and connecting them all together. Or he can buy N network cards and a hub and connect each of his N computers to the hub. The first approach would require that Luke configure his operating system to forward network traffic. However, with the installation of Winux 2007.2, Luke discovered that network forwarding no longer worked. He couldn‘t figure out how to re-enable forwarding, and he had never heard of Prim or Kruskal, so he settled on the second approach: N network cards and a hub.

Luke lives in a loft and so is prepared to run the cables and place the hub anywhere. But he won‘t move his computers. He wants to minimize the total length of cable he must buy.

Input

The first line of input contains a positive integer N <= 100, the number of computers. N lines follow; each gives the (x,y) coordinates (in mm.) of a computer within the room. All coordinates are integers between 0 and 10,000.

Output

Output consists of one number, the total length of the cable segments, rounded to the nearest mm.

Sample Input

4
0 0
0 10000
10000 10000
10000 0

Sample Output

28284

题目大意(pearfish16):

给出平面上N<=100)个点,你需要找到一个这样的点,使得这个点到N个点的距离之和尽可能小。输出这个最小的距离和(四舍五入到最近的整数)。

Input

第一行N,接下来N行每行两个整数,表示N个点

Output

一行一个正整数,最小的距离和。

做法:

模拟退火算法。

一群喝醉了的兔子去爬山,清醒时一定会向上爬,但是喝醉了就有一定几率爬到更低的上山,如此,只要它们喝醉的时间足够长,累不死,冻不死,饿不死,有的兔子醒来时可能发现自己已经爬到珠穆拉玛峰。

相比与只会往上跳的登山算法,模拟退火算法引入了向下跌这一概念,这就避免了陷入局部的最优解的情况。

随机算法真的好神奇。

这次用的是java~

  1 import java.text.DecimalFormat;
  2 import java.util.Random;
  3 import java.util.Scanner;
  4 class date{
  5     public int x,y;
  6     double dis;
  7 }
  8 public class Main{
  9     static Scanner in=new Scanner(System.in);
 10     static date a[];
 11     static date b[];
 12     static int n;
 13     static final int def=-0x3f3f3f,inf=0x3f3f3f;
 14     static final int m=10;
 15     static Random inrand;
 16     static int czx,czy;
 17     static int maxx=def,minx=inf,maxy=def,miny=inf;
 18     static double getdis(int x,int y) {
 19         double xx=x,yy=y,dis=0,dx,dy;
 20         for(int i=0;i<n;i++) {
 21             dx=(xx-a[i].x);
 22             dy=(yy-a[i].y);
 23             dis+=Math.sqrt(dx*dx+dy*dy);
 24         }
 25         return dis;
 26     }
 27     static int getrandom(int type) {
 28         int t;
 29         if(type==1) {
 30         t=inrand.nextInt(czx+1);
 31         t=t+minx;
 32         }
 33         else {
 34         t=inrand.nextInt(czy+1);
 35         t=t+miny;
 36         }
 37         return t;
 38     }
 39     public static void main(String[] args) {
 40         // TODO Auto-generated method stub
 41         inrand=new Random(System.currentTimeMillis());
 42         n=in.nextInt();
 43         a=new date[n+10];
 44         for(int i=0;i<n;i++) {
 45             a[i]=new date();
 46             a[i].x=in.nextInt();
 47             a[i].y=in.nextInt();
 48             if(a[i].x>maxx) maxx=a[i].x;
 49             if(a[i].y>maxy) maxy=a[i].y;
 50             if(a[i].x<minx) minx=a[i].x;
 51             if(a[i].y<miny) miny=a[i].y;
 52         }
 53         czx=maxx-minx;czy=maxy-miny;
 54         //System.out.println("czx:"+czx+" czy:"+czy+" minx:"+minx+" maxx:"+maxx+" miny:"+miny+" maxy:"+maxy);
 55         b=new date[m+10];
 56         for(int i=0;i<m;i++) {
 57             b[i]=new date();
 58             b[i].x=getrandom(1);
 59             b[i].y=getrandom(0);
 60             b[i].dis=getdis(b[i].x,b[i].y);
 61         }
 62         double temp=Math.max(czx, czy)+1;
 63         double cj,sj,ans;
 64         date np=new date();
 65         while(temp>0.2) {
 66             for(int i=0;i<m;i++) {
 67                 for(int j=0;j<m*2;j++) {
 68                     np.x=b[i].x+(int)(Math.sin(inrand.nextDouble()*2*3.1415926)*temp);
 69                     np.y=b[i].y+(int)(Math.cos(inrand.nextDouble()*2*3.1415926)*temp);
 70                     if(np.x<0||np.x>10000||np.y<0||np.y>10000) continue;
 71                     np.dis=getdis(np.x,np.y);
 72                     //if(i==0)
 73                     //System.out.println("temp: "+temp+" x:"+np.x+" y:"+np.y+" dis"+np.dis);
 74                     if(np.dis<b[i].dis) {
 75                         b[i].x=np.x;b[i].y=np.y;b[i].dis=np.dis;
 76                     }
 77                     else {
 78                     //    cj=np.dis-b[i].dis;
 79                     //    cj=Math.exp(cj/temp);
 80                         sj=inrand.nextDouble()*10000;
 81                         cj=0.01;
 82                         cj=cj*10000;
 83                         /*if(i==0)
 84                             System.out.println("sj: "+sj+" cj: "+cj);*/
 85                         if(sj<=cj) {
 86                             b[i].x=np.x;b[i].y=np.y;b[i].dis=np.dis;
 87                         }
 88                     }
 89                 }
 90             }
 91             temp*=0.9;
 92         }
 93         ans=1e9;
 94         for(int i=0;i<m;i++) {
 95             if(b[i].dis<ans) ans=b[i].dis;
 96         }
 97         DecimalFormat mat=new DecimalFormat(".");
 98         String sans=new String(mat.format(ans));
 99         ans=Double.parseDouble(sans);
100         int pans=(int)ans;
101         System.out.println(pans);
102     }
103 }

 

POJ - 2420 A Star not a Tree?

标签:lis   tran   move   平面   tun   ati   sans   i++   nas   

原文地址:http://www.cnblogs.com/xfww/p/7818457.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!