UVA 1001 Say Cheese (Shortest way, Floyd)

Source: Internet
Author: User

Test instructions: A female mouse wants to find her male mouse toy (cqww? , and the toy is left in a wide 3-dimensional space (which can actually be imagined as a plane) on a certain point, and the female mouse at another point, she can go directly to the location of the toy, but time is the Euclidean distance traveled by *10s. There is another way, is by drilling holes, the hole is the ball, and how to walk in the hole is not time-consuming. How long does it take for the mother mouse to find her toy?

Idea: first to see clearly test instructions first! Find the hole as much as possible, if the radius of the hole is larger, then you can save time. If the mouse and the toy are in the same hole, it is not time-consuming to find it.

In fact, is to seek single source shortest, just calculate the length between two points to take into account the radius. and pay attention to the two-hole connection, then switching between two holes does not take time. Output note to use round (double).

Here is the Dijkstra implementation, relatively fast:

1#include <bits/stdc++.h>2 #defineLL Long Long3 #definePII pair<int,int>4 #definePDI Pair<double,int>5 #defineINF 0x7f7f7f7f6 using namespacestd;7 Const intn= the;8 intX[n], y[n], z[n], r[n];9 DoubleDist[n][n];Ten DoubleSdist[n]; One intVis[n]; A  -LL Dijkstra (intN) - { thememset (Vis,0,sizeof(Vis)); -      for(intI=0; i<=n; i++) sdist[i]=1e29; -priority_queue<pdi,vector<pdi>,greater<pdi> >que; -Que.push (Make_pair (0.0,0)); +sdist[0]=0.0; -      while(!que.empty ()) +     { A         intx=que.top (). Second;que.pop (); at         if(Vis[x])Continue; -vis[x]=1; -          for(intI=0; i<=n; i++) -         { -             if(sdist[i]>sdist[x]+Dist[x][i]) -             { insdist[i]=sdist[x]+Dist[x][i]; - Que.push (Make_pair (sdist[i],i)); to             } +         } -     } the     returnRound (sdist[n]*Ten)+0.5; * } $ Panax Notoginseng  -  the intMain () + { AFreopen ("Input.txt","R", stdin); the     intN; +     intj=0; -      while(SCANF ("%d", &n), n>=0) $     { $Memset (R,0,sizeof(R)); -          for(intI=1; i<=n; i++) scanf ("%d %d%d%d", &x[i], &y[i], &z[i], &r[i]); -  thescanf" %d%d%d", &x[0],&y[0],&z[0]);//starting point -scanf" %d%d%d", &x[n+1],&y[n+1],&z[n+1]);//EndWuyi  the         //find the distance between 22 -          for(intI=0; i<=n+1; i++) Wu         { -              for(intj=0; j<=n+1; J + +) About             { $dist[i][j]=0.0; -                 if(j!=i) -                 { -Dist[i][j]=sqrt (Pow (x[i]-x[j),2) +pow (Y[i]-y[j],2) +pow (Z[i]-z[j],2))-r[i]-R[j]; A                     if(dist[i][j]<1e-7) dist[i][j]=0; +                 } the             } -         } $  theprintf"Cheese%d:travel time =%lld sec\n", ++j, Dijkstra (n+1)); the  the     } the     return 0; -}
AC Code

Here is the Floyd implementation, relatively short:

1#include <bits/stdc++.h>2 #defineLL Long Long3 #definePII pair<int,int>4 #defineINF 0x7f7f7f7f5 using namespacestd;6 Const intn= the;7 intX[n], y[n], z[n], r[n];8 DoubleDist[n][n];9 Ten intMain () One { AFreopen ("Input.txt","R", stdin); -     intN; -     intj=0; the      while(SCANF ("%d", &n), n>=0) -     { -Memset (R,0,sizeof(R)); -          for(intI=1; i<=n; i++) scanf ("%d %d%d%d", &x[i], &y[i], &z[i], &r[i]); +  -scanf" %d%d%d", &x[0],&y[0],&z[0]);//starting point +scanf" %d%d%d", &x[n+1],&y[n+1],&z[n+1]);//End A  at         //find the distance between 22 -          for(intI=0; i<=n+1; i++) -         { -              for(intj=0; j<=n+1; J + +) -             { -dist[i][j]=0.0; in                 if(j!=i) -                 { toDist[i][j]=sqrt (Pow (x[i]-x[j),2) +pow (Y[i]-y[j],2) +pow (Z[i]-z[j],2))-r[i]-R[j]; +                     if(dist[i][j]<1e-7) dist[i][j]=0; -                 } the             } *         } $         //FloydPanax Notoginseng          for(intk=0; k<=n+1; k++) -              for(intI=0; i<=n+1; i++) the                  for(intj=0; j<=n+1; J + +) +Dist[i][j]=min (dist[i][j],dist[i][k]+dist[k][j]); Aprintf"Cheese%d:travel time =%lld sec\n", ++j, (LL) (Round (dist[0][n+1]*Ten)+0.5) ); the  +     } -     return 0; $}
AC Code

UVA 1001 Say Cheese (Shortest way, Floyd)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.