• 主页
  • Java SE
  • J2ME
  • Web开发
  • Java EE
  • Eclipse
keyhwt 05月01日
求大神把下面代码转化为java语言
#include <iostream>
#include <cstdio>
#include <cstring>
#define N 500
#define M 850

using namespace std;

struct point{int x,y;point(const int &A,const int &B){x=A;y=B;}point(){}};
typedef point Vector;
Vector operator - (const point &a,const point &b){return Vector(a.x-b.x,a.y-b.y);}
inline long long Cross(Vector A,Vector B){return 1LL*A.x*B.y-1LL*A.y*B.x;}
int n,m,x[N],y[N],v[N],top;
long long F[M],ans;
long long rk[M]; //记录本次DP第i个题目的优先级
point rc[M]; //记录到达获得i收益的点(x,y)

inline int read()
{
int x=0;char ch=getchar();
while(ch<'0'||ch>'9')ch=getchar();
while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
return x;
}

point DP()
{
point now;
now.x = now.y = 4000000;
for (int i=1;i<=top;i++) F[i] = 1LL << 62;
F[0] = 0LL;
for (int i=1;i<=n;i++)
for (int j=top;j>=v[i];j--)
if (F[j] > F[j-v[i]] + rk[i]) {
rc[j].x = rc[ j-v[i] ].x + x[i];
rc[j].y = rc[ j-v[i] ].y + y[i];
F[j] = F[j-v[i]] + rk[i];
}

int k = m;
for (int i=m;i<=top;i++) if (F[k] > F[i]) k = i;
now = rc[k];
ans = min(ans,1LL*now.x*now.y);
return now;
}

void solve(point A,point B)
{
for (int i=1;i<=n;i++) rk[i] = 1LL * x[i] * (A.y - B.y) + y[i] * (B.x - A.x); //更新关键词
point C = DP();
if(Cross(B-A,C-A)>=0) return ; //寻找不到直线AB左下方的点
solve(A,C);solve(C,B); //分成两部分递归
}

int main()
{
ans = 1LL << 62;
while (~scanf("%d%d",&n,&m))
{
for (int i=1;i<=n;i++) {v[i] = read();x[i] = read(); y[i] = read();}
for (int i=1;i<=n;i++) top += v[i];

for (int i=1;i<=n;i++) rk[i] = 1LL*x[i]; //DP关键词为横坐标
point A = DP(); //寻找最靠近y轴的点

for (int i=1;i<=n;i++) rk[i] = 1LL*y[i]; //DP关键词为纵坐标
point B = DP(); //寻找最靠近x轴的点

solve(A,B); //递归寻找在A,B左下方的点

printf("%I64d\n",ans);
}
return 0;
}
...全文
4 1 收藏 回复
写回复
回复

还没有回复,快来抢沙发~

发动态
发帖子
Java
创建于2007-09-28

1.7w+

社区成员

8.1w+

社区内容

Java相关技术讨论
社区公告
暂无公告