Freud Floyd to find the smallest ring

Source: Internet
Author: User

Template :

#include <bits/stdc++.h>using namespacestd;Const intMAXN = the;Const intINF =0xffffff0;inttemp,map[maxn][maxn],dist[maxn][maxn],pre[maxn][maxn],ans[maxn*3];voidSolve (intIintJintk) {Temp=0;//backtracking, storage of the smallest ring     while(I! =j) {Ans[temp++] =J; J=Pre[i][j]; } ans[temp++] =i; Ans[temp++] =K;}voidFloyd (intN) {     for(inti =1; I <= N; ++i) for(intj =1; J <= N; ++j) {Dist[i][j]=Map[i][j]; PRE[I][J]=i; }    intMincircle = INF;//Minimum Ring     for(intK =1; K <= N; ++k) { for(inti =1; I <= N; ++i) { for(intj =1; J <= N; ++j) {if(I! = J && Dist[i][j]! = INF && map[i][k]! = INF && map[k][j]! =INF&& Dist[i][j] + map[i][k] + Map[k][j] <mincircle) {mincircle= Min (mincircle, dist[i][j] + map[i][k] +Map[k][j]);   Solve (I,J,K); //Back storage Minimum ring                }            }        }         for(inti =1; I <= N; ++i) { for(intj =1; J <= N; ++j) {if(Dist[i][k]! = INF && dist[k][j]! = INF &&Dist[i][k]+ Dist[k][j] <Dist[i][j]) {Dist[i][j]= Dist[i][k] +Dist[k][j]; PRE[I][J]= Pre[k][j];//Record point I point J on the path, J Front Dot                }            }        }    }    if(mincircle = = INF)//There is no ring{printf ("No solution.\n"); return; }    //If the minimum ring is found to be negative, the original image must have a negative ring     for(inti =0; i < temp; ++i)//Output Minimum ring        if(I! = temp-1) printf ("%d", Ans[i]); Elseprintf ("%d\n", Ans[i]);}
View Code

Example :

BZOJ1027: [JSOI2007] alloy

Idea: given two point setsAAndBPleaseAA subset of the smallestSMakeBAll the points in theSInside the convex package. EnumerationAPoint set two pointsI,J(Ican be equal toJ) ifBAll points in the point set are in the vectori->jThe left or segment of theIJ, connect ai->jOne-way edges, and thenFloydThe smallest ring can be obtained.

#include <bits/stdc++.h>#defineEPS 1e-8using namespacestd;structnode{Doublex, Y, Z;} a[510],b[510];Doublemulti (node P1,node p2,node p0) {Doublex1=p1.x-p0.x; Doubley1=p1.y-p0.y; Doublex2=p2.x-p0.x; Doubley2=p2.y-p0.y; returnx1*y2-x2*Y1;}DoubleDis (node p1,node p2) {returnsqrt ((p1.x-p2.x) * (p1.x-p2.x) + (P1.Y-P2.Y) * (p1.y-p2.y));}intd[510][510];intMain () {intn,m; scanf ("%d%d",&m,&N);  for(intI=1; i<=m;i++) scanf ("%LF%LF%LF",&a[i].x,&a[i].y,&a[i].z);  for(intI=1; i<=n;i++) scanf ("%LF%LF%LF",&b[i].x,&b[i].y,&b[i].z); intI,j,k;DoubleT; memset (d,0x3f,sizeof(d));  for(i=1; i<=m;i++)         for(j=1; j<=m;j++)        {             for(k=1; k<=n;k++) {T=multi (a[j],b[k],a[i]); if(t<-eps) Break; if(Fabs (t) <eps && Dis (a[i],b[k]) >dis (A[i],a[j])) Break; }            if(k==n+1) d[i][j]=1; }     for(k=1; k<=m;k++)         for(i=1; i<=m;i++)if(i!=k) for(j=1; j<=m;j++)if(j!=k)if(D[i][j]>d[i][k]+d[k][j]) d[i][j]=d[i][k]+D[k][j]; intans=m+1;  for(i=1; i<=m;i++)if(Ans>d[i][i]) ans=D[i][i]; if(ans==m+1) printf ("-1\n");Elseprintf"%d\n", ans); return 0;}
View Code

Freud Floyd to find the smallest ring

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.