zoj3537 Cake Premium 삼각형 분할(DP)
먼저 볼록 다각형인지 직접 볼록 다각형인지 판단하면 된다. 다음에 dp[i][j]는 i에서 j로 끝나는 다각형을 표시하고 모두 삼각형으로 자르는 데 가장 적은 비용을 들이면 방정식 dp[i][j]=min(dp[i][k]+dp[k][k][j]+cost[i][k][j])(k=i+1...j-1)을 옮긴다.여기 기억화 검색이라고 적어서 잘 이루어지도록...
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <string>
#include <cmath>
#include <vector>
typedef double type;
using namespace std;
const double PI=acos(-1.0);
const double eps=1e-10;
typedef long long ll;
const ll inf=(1LL<<40);
struct Point
{
type x,y;
Point(){}
Point(type a,type b)
{
x=a;
y=b;
}
Point (int a,int b)
{
x=a; y=b;
}
void read()
{
scanf("%lf%lf",&x,&y);
}
void print()
{
printf("%.6lf %.6lf
",x,y);
}
};
typedef Point Vector;
Vector operator + (Vector A,Vector B)
{
return Vector(A.x+B.x,A.y+B.y);
}
Vector operator - (Point A,Point B)
{
return Vector(A.x-B.x,A.y-B.y);
}
Vector operator * (Vector A,type p)
{
return Vector(A.x*p,A.y*p);
}
Vector operator / (Vector A,type p)
{
return Vector(A.x/p,A.y/p);
}
bool operator < (const Point &a,const Point &b)
{
return a.x<b.x || (a.x==b.x && a.y<b.y);
}
int dcmp(double x)
{
if (fabs(x)<eps) return 0;
else return x<0?-1:1;
}
bool operator == (const Point& a,const Point b)
{
return dcmp(a.x-b.x)==0 && dcmp(a.y-b.y)==0;
}
//atan2(x,y) : (x,y) , x 。
type Dot(Vector A,Vector B)
{
return A.x*B.x+A.y*B.y;
}
type Cross(Vector A,Vector B)
{
return A.x*B.y-A.y*B.x;
}
type Length(Vector A)
{
return sqrt(Dot(A,A));
}
type Angle(Vector A,Vector B)
{
return acos(Dot(A,B))/Length(A)/Length(B);
}
type Area2(Point A,Point B,Point C)
{
return Cross(B-A,C-A);
}
Vector Rotate(Vector A,double rad)
{
return Vector(A.x*cos(rad)-A.y*sin(rad),A.x*sin(rad)+A.y*cos(rad));
}
Vector Normal(Vector A)// , 90 ,
{
double L=Length(A);
return Vector(-A.y/L,A.x/L);
}
Point GetLineIntersection(Point P,Vector v,Point Q,Vector w)
{
Vector u=P-Q;
double t=Cross(w,u)/Cross(v,w);
return P+v*t;
}
double DistanceToLine(Point P,Point A,Point B)
{
Vector v1=B-A,v2=P-A;
return fabs(Cross(v1,v2))/Length(v1);
}
double DistanceToSegment(Point P,Point A,Point B)
{
if (A==B) return Length(P-A);
Vector v1=B-A,v2=P-A,v3=P-B;
if (dcmp(Dot(v1,v2))<0) return Length(v2);
else if (dcmp(Dot(v1,v3))>0) return Length(v3);
else return fabs(Cross(v1,v2))/Length(v1);
}
Point GetLineProjection(Point P,Point A,Point B)//P AB
{
Vector v=B-A;
return A+v*(Dot(v,P-A)/Dot(v,v));
}
bool SegmentProperIntersection(Point a1,Point a2,Point b1,Point b2)
{
double c1=Cross(a2-a1,b1-a1),c2=Cross(a2-a1,b2-a1),
c3=Cross(b2-b1,a1-b1),c4=Cross(b2-b1,a2-b1);
return dcmp(c1)*dcmp(c2)<0 && dcmp(c3)*dcmp(c4)<0;
}
bool OnSegment(Point p,Point a1,Point a2)
{
return dcmp(Cross(a1-p,a2-p))==0 && dcmp(Dot(a1-p,a2-p))<0;
}
double ConvexPolygonArea(Point* p,int n)//
{
double area=0;
for (int i=1; i<n-1; i++)
area+=Cross(p[i]-p[0],p[i+1]-p[0]);
return area/2.0;
}
double PolygonArea(Point* p,int n)//
{
double area=0;
for (int i=1; i<n-1; i++)
area+=Cross(p[i]-p[0],p[i+1]-p[0]);
return area/2.0;
}
struct Line
{
Point p;
Vector v;
double ang;
Line(){};
Line(Point PP,Vector vv)
{
p=PP;
v=vv;
ang=atan2(v.y,v.x);
}
bool operator< (const Line& L)const
{
return ang<L.ang;
}
Point point(double t)
{
return p+v*t;
}
};
struct Circle
{
Point c;
double r;
Circle()
{
}
Circle(Point cc,double rr)
{
c=cc;
r=rr;
}
Point point(double a)
{
return Point(c.x+cos(a)*r,c.y+sin(a)*r);
}
};
int getLineCircleIntersection(Line L,Circle C,double& t1,double &t2,vector<Point>& sol)
{
double a=L.v.x, b=L.p.x-C.c.x, c=L.v.y, d=L.p.y-C.c.y;
double e=a*a+c*c,f=2*(a*b+c*d), g=b*b+d*d-C.r*C.r;
double delta=f*f-4*e*g;//
if (dcmp(delta)<0) return 0;//
if (dcmp(delta)==0)
{
t1=t2=-f/(2*e);
sol.push_back(L.point(t1));
return 1;
}//
t1=(-f-sqrt(delta))/(2*e); sol.push_back(L.point(t1));
t2=(-f+sqrt(delta))/(2*e); sol.push_back(L.point(t2));
return 2;
}
double angle(Vector v)//
{
return atan2(v.y,v.x);
}
int getCircleCircleIntersection(Circle C1,Circle C2,vector<Point>& sol)
{
double d = Length(C1.c-C2.c);
if (dcmp(d)==0)
{
if (dcmp(C1.r-C2.r)==0) return -1;
return 0;
}
if (dcmp(C1.r+C2.r-d)<0) return 0;
if (dcmp(fabs(C1.r-C2.r)-d)>0) return 0;
double a=angle(C2.c-C1.c);
double da= acos((C1.r*C1.r+d*d-C2.r*C2.r)/(2*C1.r*d));
Point p1=C1.point(a-da),p2=C1.point(a+da);
sol.push_back(p1);
if (p1==p2) return 1;
sol.push_back(p2);
return 2;
}
int getTangents(Point p,Circle C,Vector* v)
{
Vector u=C.c-p;
double dist=Length(u);
if (dist<C.r) return 0;
else if (dcmp(dist-C.r)==0)
{
v[0]=Rotate(u,PI/2);
return 1;
}
else
{
double ang=asin(C.r/dist);
v[0]=Rotate(u,-ang);
v[1]=Rotate(u,+ang);
return 2;
}
}
int getTangents(Circle A,Circle B,Point* a,Point* b)
{
int cnt=0;
if (A.r<B.r)
{
swap(A,B); swap(a,b);
}
int d2=(A.c.x-B.c.x)*(A.c.x-B.c.x)+(A.c.y-B.c.y)*(A.c.y-B.c.y);
int rdiff=A.r-B.r;
int rsum=A.r+B.r;
if (d2<rdiff*rdiff) return 0;
double base=atan2(B.c.y-A.c.y,B.c.x-A.c.x);
if (d2==0 && A.r==B.r) return -1;
if (d2==rdiff*rdiff)
{
a[cnt]=A.point(base); b[cnt]=B.point(base); cnt++;
return 1;
}
double ang=acos((A.r-B.r)/sqrt((double)d2));
a[cnt]=A.point(base+ang); b[cnt]=B.point(base+ang); cnt++;
a[cnt]=A.point(base-ang); b[cnt]=B.point(base-ang); cnt++;
if (d2==rsum*rsum)
{
a[cnt]=A.point(base); b[cnt]=B.point(PI+base); cnt++;
}
else if (d2>rsum*rsum)
{
double ang=acos((A.r+B.r)/sqrt((double)d2));
a[cnt]=A.point(base+ang); b[cnt]=B.point(PI+base+ang); cnt++;
a[cnt]=A.point(base-ang); b[cnt]=B.point(PI+base-ang); cnt++;
}
return cnt;
}
int isPointInpolygon(Point p,Point* poly,int n)
{
int wn=0;
for (int i=0; i<n; i++)
{
if (OnSegment(p,poly[i],poly[(i+1)%n]) || p==poly[i] || p==poly[(i+1)%n]) return -1;
int k=dcmp(Cross(poly[(i+1)%n]-poly[i],p-poly[(i+1)%n]));
int d1=dcmp(poly[i].y-p.y);
int d2=dcmp(poly[(i+1)%n].y-p.y);
if (k>0 && d1<=0 && d2>0) wn++;
if (k<0 && d2<=0 && d1>0) wn--;
}
if (wn!=0) return 1;
else return 0;
}
int ConvexHull(Point *p, int n,Point *ch)
{
sort(p,p+n);
int m=0;
for (int i=0; i<n; i++)
{
while(m>1 && Cross(ch[m-1]-ch[m-2],p[i]-ch[m-2])<=0) m--;
ch[m++]=p[i];
}
int k=m;
for (int i=n-2; i>=0; i--)
{
while(m>k && Cross(ch[m-1]-ch[m-2],p[i]-ch[m-2])<=0) m--;
ch[m++]=p[i];
}
if (n>1) m--;
return m;
}
double rotating_calipers(Point *p,int n)
{
int i,q=1;
double ans=0;
for (int i=0; i<n-1; i++)
{
while(Cross(p[q+1]-p[i+1],p[i]-p[i+1])>Cross(p[q]-p[i+1],p[i]-p[i+1]))
{
q=(q+1)%n;
}
ans=max(ans,max(Length(p[i]-p[q]),Length(p[i+1]-p[q])));
}
return ans*ans;
}
bool OnLeft(Line L,Point p)
{
return Cross(L.v,p-L.p)>0;
}
//
Point GetIntersection(Line a,Line b)
{
Vector u=a.p-b.p;
double t=Cross(b.v,u)/Cross(a.v,b.v);
return a.p+a.v*t;
}
int HalfplaneIntersection(Line* L,int n,Point* poly)
{
sort(L,L+n);
int first,last;
Point *p=new Point[n];
Line *q=new Line[n];
q[first=last=0] = L[0];
for (int i=1; i<n; i++)
{
while(first<last && !OnLeft(L[i],p[last-1])) last--;
while(first<last && !OnLeft(L[i],p[first])) first++;
q[++last]=L[i];
if (fabs(Cross(q[last].v,q[last-1].v))<eps)
{
last--;
if (OnLeft(q[last],L[i].p)) q[last]=L[i];
}
if (first<last) p[last-1]=GetIntersection(q[last-1],q[last]);
}
while(first<last && !OnLeft(q[first],p[last-1])) last--;
if (last-first<=1) return 0;//
p[last]=GetIntersection(q[last],q[first]); //
int m=0;
for (int i=first; i<=last; i++) poly[m++]=p[i];
return m;
}
Point p[330];
Point ch[330];
int n,m,k;
ll dp[330][330];
int x[330],y[330];
int cost[330][330];
bool vis[330][330];
int mod;
ll slove(int s,int t)
{
if (dp[s][t]<inf) return dp[s][t];
ll res=inf;
for (int k=s+1; k+1<=t; k++)
{
res=min(res,slove(s,k)+slove(k,t)+cost[s][k]+cost[k][t]);
}
vis[s][t]=true;
return dp[s][t]=res;
}
int main()
{
// freopen("in.txt","r",stdin);
int tt;
while(~scanf("%d%d",&n,&mod))
{
for (int i=0; i<n; i++)
{
scanf("%d%d",&x[i],&y[i]);
p[i]=Point(x[i],y[i]);
}
int size=ConvexHull(p,n,ch);
if (size<n)
{
puts("I can't cut.");
}
else
{
for (int i=0; i<n; i++)
for (int j=i+2; j<n; j++)
{
cost[i][j]=abs(ch[i].x+ch[j].x)*abs(ch[i].y+ch[j].y);
cost[i][j]%=mod;
cost[j][i]=cost[i][j];
}
// memset(vis,false,sizeof vis);
memset(dp,0x3f,sizeof dp);
for (int i=0; i<n; i++)
{
int j=(i+1)%n;
dp[i][j]=0;
}
ll ans=slove(0,n-1);
cout<<ans<<endl;
}
}
return 0;
}
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
다양한 언어의 JSONJSON은 Javascript 표기법을 사용하여 데이터 구조를 레이아웃하는 데이터 형식입니다. 그러나 Javascript가 코드에서 이러한 구조를 나타낼 수 있는 유일한 언어는 아닙니다. 저는 일반적으로 '객체'{}...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.