Uvalive 4043 Ants Ant (Two-figure best fit)

Source: Internet
Author: User

Test instructions: N ants n tree, Ant and tree to pair, with a pair of successful pairing between a line segment, requires that all segments can not intersect. The tree that the ants match is output sequentially.

Idea: This topic is really a skill ah, if you know the demand is the overall optimal, then it is easy to do. Instead of using greed to select the nearest tree for each ant, it may be 80% or intersect.

The overall optimal performance allows each segment to be disjoint, proving that:

Suppose a1-b1 intersects with A2-B2. Then dis (A1,B1) +dis (A2,B2) >=dis (A1,B2) +dis (A2,B1). If we decide that the optimal match is based on the shortest overall distance to match, then Dis (A1,B1) +dis (A2,B2) must be less than dis (A1,B2) +dis (A2,B1), otherwise, with the optimal contradiction. Generalization to the entire graph is the matching graph of any two points are optimal, otherwise we can certainly in a better way to replace them. And the overall optimal depends on the KM algorithm.

Tips: To choose the overall weight is the smallest, only need to set the edge to the opposite number of distance to run the KM algorithm can be.

1#include <bits/stdc++.h>2 using namespacestd;3 Const intn= the;4 intAntx[n], anty[n], treex[n], treey[n];5 DoubleG[n][n];//Distance6 7InlineDoubleDisintAintb)8 {9     returnsqrt ((Treex[a]-antx[b]) * (Treex[a]-antx[b]) + (Treey[a]-anty[b]) * (treey[a]-anty[b]));Ten } One  A intN; - DoubleLx[n], ly[n], slack[n]; - intGirl[n]; the intS[n], t[n]; -  - BOOLDFS (intx) - { +s[x]=true; -      for(intI=1; i<=n; i++) +     { A         if(T[i])Continue; at         Doubletmp=lx[x]+ly[i]-G[x][i]; -         if(tmp<1e-6) -         { -t[i]=true; -             if(girl[i]==0||DFS (Girl[i])) -             { ingirl[i]=x; -                 return true; to             } +         } -         Else if(slack[i]>tmp) theslack[i]=tmp; *     } $     return false;Panax Notoginseng } -  the voidKM (intN) + { A      for(intI=1; i<=n; i++)//Initialization Work the     { +girl[i]=0; -lx[i]=-1e19; $ly[i]=0.0; $          for(intj=1; j<=n; J + +) -lx[i]=Max (Lx[i], g[i][j]); -     } the      for(intI=1; i<=n; i++)//for each tree -     {Wuyi          for(intj=1; j<=n; J + +) slack[j]=1e19; the          while(1) -         { Wumemset (S),0,sizeof(S)); -memset (T,0,sizeof(T)); About             if(DFS (i)) Break;//find the matching ants. $  -  -             DoubleD=1e19; -              for(intj=1; j<=n; J + +)//Find minimum D A             { +                 if(! T[J] && d>Slack[j]) theD=Slack[j]; -             } $  the              for(intj=1; j<=n; J + +)//Update Tree the             { the                 if(S[j]) thelx[j]-=D; -             } in  the              for(intj=1; j<=n; J + +)//Update Ants the             { About                 if(T[j]) ly[j]+=D; the                 Elseslack[j]-=D; the             } the         } +     } - } the Bayi intMain () the { theFreopen ("Input.txt","R", stdin); -     intk=0; -      while(~SCANF ("%d",&N)) the     { the         if(k) printf ("\ n"); thek++; the          for(intI=1; i<=n; i++) -scanf"%d%d", &antx[i], &anty[i]);//Ant the          for(intI=1; i<=n; i++) thescanf"%d%d", &treex[i], &treey[i]);//Apple Tree the 94  the          for(intI=1; i<=n; i++) the              for(intj=1; j<=n; J + +) theg[i][j]=-dis (i,j);98  About KM (n); -          for(intI=1; i<=n; i++)101printf"%d\n", Girl[i]);//ans for girl102     }103     return 0;104}
AC Code

Uvalive 4043 Ants Ant (Two-figure best fit)

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.