Rokua P1433 eating cheese Label:dfs && pruning ex

Source: Internet
Author: User
Tags cmath

Title Description

There are n pieces of cheese in the room. A little mouse to eat them all, ask at least how many distance to run? The mouse begins at (0,0) point.

Input/output format

Input format:

First row one number n (n<=15)

The next 2 real numbers for each line represent the coordinates of the block I cheese.

Distance between two points formula =sqrt ((X1-X2) * (X1-X2) + (y1-y2) * (Y1-Y2))

Output format:

A number that represents the minimum distance to run and retains 2 decimal places.

Input and Output Sample input example # #:
41 11-1-1 1-1-1
Sample # # of output:
7.41
Code
1#include <iostream>2#include <cstring>3#include <cstdio>4#include <vector>5#include <cmath>6#include <map>7#include <algorithm>8 #defineINF 0x3f3f3f3f9 using namespacestd;Ten  Onemap<int,Double>m; Avector<int> g[ -]; -vector<Double> c[ -]; - intn,vis[ -]; the Doubleans=INF; -  - structcc{ -     Doublex, y; +}nod[ -]; -  + DoubleDisintAintb) { A     returnsqrt ((nod[a].x-nod[b].x) * (nod[a].x-nod[b].x) + (NOD[A].Y-NOD[B].Y) * (nod[a].y-nod[b].y)); at } -  - intLook_up_table (DoubleLen) { -     Long LongH=0; -      for(intI=1; i<=n;i++){ -         if(Vis[i]) h=h*Ten+i; in     } -     if(M.count (H)) { to         if(Len>m[h])return 0; +m[h]=Len; -         return 1; the     } *     Else{ $m[h]=Len;Panax Notoginseng         return 1; -     } the } +  A  the voidSearchintXintDepDoubleLen) { +     if(Len>=ans)return; -     if(!look_up_table (len))return; $     if(dep==N) { $ans=min (len,ans); -         return; -     } thevis[x]=1; - //puts ("$");Wuyi      for(intI=0; I<g[x].size (); i++){ the         intnow=G[x][i]; -         if(!vis[now]) search (now,dep+1, len+c[x][i]); Wu     } -      Aboutvis[x]=0; $ } -  - intMain () { - //freopen ("01.in", "R", stdin); Ascanf"%d",&N); +      for(intI=1; i<=n;i++){ theCin>>nod[i].x>>nod[i].y; -         //scanf ("%lf", &nod[i].x,&nod[i].y); $     } thenod[0].x=nod[0].y=0.0; the      for(intI=0; i<=n;i++){ the          for(intj=i+1; j<=n;j++){ the G[i].push_back (j); - c[i].push_back (DIS (i,j)); in              the G[j].push_back (i); the c[j].push_back (DIS (i,j)); About         } the     } theSearch0,0,0.0); theprintf"%.2LF", ans); +     return 0; -}
90 points of map>_<

It's a map, and it's unexpected.

Hash does not seem to be possible, because their data are not over, there is no submission

As for the evaluation of the time I think the direct STL is good, hash try it

I was speechless when I saw the answer.

Backtracking and pruning.

1#include <iostream>2#include <cstring>3#include <cstdio>4#include <vector>5#include <cmath>6#include <map>7#include <algorithm>8 #defineINF 0x3f3f3f3f9 using namespacestd;Ten  One intn,vis[ -]; A Doubleans=INF; -  - structcc{ the     Doublex, y; -}nod[ -]; -  - DoubleDisintAintb) { +     returnsqrt ((nod[a].x-nod[b].x) * (nod[a].x-nod[b].x) + (NOD[A].Y-NOD[B].Y) * (nod[a].y-nod[b].y)); - } +  A voidSearchintXintDepDoubleLen) { at     if(Len>=ans)return; -     if(dep==N) { -ans=min (len,ans); -         return; -     } -      in      for(intI=1; i<=n;i++){ -         if(i==x)Continue; to         if(!Vis[i]) { +vis[i]=1; -Search (i,dep+1, len+dis (x,i)); thevis[i]=0; *         } $     }Panax Notoginseng      - } the  + intMain () { A //freopen ("01.in", "R", stdin); thescanf"%d",&N); +      for(intI=1; i<=n;i++) cin>>nod[i].x>>nod[i].y; -Search0,0,0.0); $printf"%.2LF", ans); $     return 0; -}

Finished?

All right, computer scum, 13-point data.

Rokua P1433 eating cheese Label:dfs && pruning ex

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.