poj1292


注意:

1.判斷少了

2.精度問題(g++編譯器要注意)

B - Will Indiana Jones Get There?
Time Limit:1000MS     Memory Limit:10000KB     64bit IO Format:%I64d & %I64u

Description

Indiana Jones is in a deserted city, annihilated during a war. Roofs of all houses have been destroyed and only portions of walls are still standing. The ground is so full of mines that the only safe way to move around the city is walking over the remaining walls. The mission of our hero is to save a person who is trapped in the city. In order to move between two walls which are not connected Indiana Jones thought of taking with him a wooden board which he could place between the two walls and then cross from one to the other. 

Initial positions of Indiana Jones and the trapped person are both on some section of the walls.Besides, walls are either in the direction South-North or West-East. 
You will be given a map of the city remains. Your mission is to determine the minimum length of the wooden board Indiana Jones needs to carry in order to get to the trapped person.

Input

Your program should process several test cases. Each test case starts with an integer N indicating the number of wall sections remaining in the city (2 <= N <= 1000). Each of the next N lines describes a wall section. The first wall section to appear is the section where Indiana Jones stands at the beginning.The second section to appear is the section where the trapped person stands. Each wall section description consists of three integers X,Y and L (-10000 <= X ,Y ,L <= 10000), where X an Y define either the southernmost point of a wall section (for South-North sections) or the westernmost point (for West-East wall sections). The value of L determines the length and direction of the wall: if L >= 0,the section is West-East, with length L; if L < 0, the section is North-South, with length |L|. The end of input is indicated by N = 0.

Output

For each test case in the input your program should produce one line of output, containing a real value representing the length of the wooden board Indiana Jones must carry. The length must be printed as a real number with two-digit precision, and the last decimal digit must be rounded. The input will not contain test cases where differences in rounding are significant.

Sample Input

14
1 1 5
6 8 2
7 2 -2
5 3 3
2 5 2
2 3 2
2 3 -2
4 3 -2
0 7 1
1 8 2
3 6 -2
4 7 2
6 6 1
6 6 -2
3
-10 0 20
-5 1 10
50 50 100
0

Sample Output

1.41
1.00

思路:

求出任意兩線段間距離,再prim

    #include<cstdio>  
    #include<algorithm>  
    #include<vector>  
    #include<cstdlib>  
    #include<cmath>  
    #include<cstring>  
    using namespace std;  
    const int maxn=1001,INF=1000000000;  
    struct line{  
      int x;  
      int y;  
      int l;  
      bool tp;   
    }l[maxn];  
    double a[maxn][maxn],d[maxn];  
    int p[maxn];  
    double dist(int x1,int y1,int x2,int y2){  
      return (double)sqrt(double((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2)));  
    }  
    double getdist(int u,int v){  
      int x1=l[u].x,y1=l[u].y,x2=l[v].x,y2=l[v].y,l1=l[u].l,l2=l[v].l;  
      if (l[u].tp==l[v].tp){  
         if(l[u].tp==1){  
           if (x1+l1>=x2 && x1<=x2 || (x1+l1>=x2+l2 && x2+l2>=x1) ||(x2+l2>=x1 && x2<=x1 )|| (x2+l2>=x1+l1 && x1+l1>=x2))  
             return abs(y1-y2);  
           else return fmin(fmin(dist(x1,y1,x2,y2),dist(x1+l1,y1,x2,y2)),fmin(dist(x1,y1,x2+l2,y2),dist(x1+l1,y1,x2+l2,y2)));  
         }  
         if(l[u].tp==0){  
           if (y1+l1>=y2 && y1<=y2 || (y1+l1>=y2+l2 && y2+l2>=y1)||(y2+l2>=y1 && y2<=y1 )|| (y2+l2>=y1+l1 && y1+l1>=y2))  
             return abs(x1-x2);  
           else return fmin(fmin(dist(x1,y1,x2,y2),dist(x1,y1+l1,x2,y2)),fmin(dist(x1,y1+l1,x2,y2+l2),dist(x1,y1,x2,y2+l2)));  
         }  
      }else{  
         if (l[u].tp==0){  
            if (y1<=y2 && y2<=y1+l1 && x1>=x2 && x1<=x2+l2)return 0;  
            if (y1<=y2 && y2<=y1+l1)  
               return fmin(abs(x1-x2),abs(x1-(x2+l2)));  
            if (x1>=x2 && x1<=x2+l2)  
               return fmin(abs(y1-y2),abs((y1+l1)-y2));  
            return fmin(fmin(dist(x1,y1,x2,y2),dist(x1,y1+l1,x2,y2)),fmin(dist(x1,y1,x2+l2,y2),dist(x1,y1+l1,x2+l2,y2)));  
              
         }else {  
            if (y2<=y1 && y1<=y2+l2 &&x2>=x1 && x2<=x1+l1 )return 0;  
            if (y2<=y1 && y1<=y2+l2)  
               return fmin(abs(x1-x2),abs((x1+l1)-x2));    
            if (x2>=x1 && x2<=x1+l1)  
               return fmin(abs(y1-y2),abs(y1-(y2+l2)));  
            else return fmin(fmin(dist(x1,y1,x2,y2),dist(x1+l1,y1,x2,y2)),fmin(dist(x1+l1,y1,x2,y2+l2),dist(x1,y1,x2,y2+l2)));  
         }  
      }  
      return 0;  
    }  
    int main(){  
        int i,j,k,L,m,n,x,y;  
      
        while(scanf("%d",&n) && n){  
          for (i=1;i<=n;i++){  
            scanf("%d%d%d",&x,&y,&L);  
            l[i].x=x;l[i].y=y;l[i].l=abs(L);l[i].tp=(L>=0);  
          }  
          for (i=1;i<=n-1;i++)  
            for (j=i+1;j<=n;j++){  
              double d1=getdist(i,j);  
              a[i][j]=d1;a[j][i]=d1;  
              //printf("%d %d %lf\n",i-1,j-1,d1);  
            }  
          for (i=1;i<=n;i++){  
            d[i]=INF;a[i][i]=0;  
            p[i]=0;  
          }  
          d[1]=0;  
          double ans=-INF;  
          for (i=1;i<=n;i++){  
            double Min=INF;  
            for (j=1;j<=n;j++)  
              if (!p[j] && d[j]<Min){  
                 Min=d[j];  
                 k=j;  
              }  
            p[k]=1;  
            ans=fmax(ans,Min);  
            if (k==2)break;  
            for (j=1;j<=n;j++)  
              if (!p[j] && d[j]>a[k][j])d[j]=a[k][j];   
          }  
          int t=(int)(ans*1000+5)/10;  
          printf("%d.%d",t/100,t%100);  
          if (t%10==0)printf("0");  
          puts("");  
        }  
        return 0;  
    }  


發佈了32 篇原創文章 · 獲贊 0 · 訪問量 6946
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章