hdu_2224_The shortest path(dp)
题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=2224
题意:双调欧几里德旅行商经典问题,找一条最短回路使得该路经过所有的点
题解:dp[i][j]=dp[i-1][j]+dis(i,i-1),dp[i][i-1]=Min(dp[i][i-1],dp[i-1][j]+dis(i,j));,注意这里题目的数据给的是从左往右的,所以不需要排序
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 #include<cstdio> 2 #include<cmath> 3 #define FFC(i,a,b) for(int i=a;i<=b;i++) 4 const int maxn=210; 5 double dp[maxn][maxn],inf=1e9; 6 struct node{double x,y;}g[maxn]; 7 double Min(double a,double b){return a>b?b:a;} 8 double dis(int i,int j){return sqrt((g[i].x-g[j].x)*(g[i].x-g[j].x)+(g[i].y-g[j].y)*(g[i].y-g[j].y));} 9 double fuck(int n){ 10 FFC(i,1,n)dp[i][i-1]=inf; 11 dp[1][1]=0,dp[2][1]=dis(2,1); 12 FFC(i,3,n)FFC(j,1,i-2) 13 dp[i][j]=dp[i-1][j]+dis(i,i-1),dp[i][i-1]=Min(dp[i][i-1],dp[i-1][j]+dis(i,j)); 14 return dp[n][n-1]+dis(n,n-1); 15 } 16 int main(){ 17 int n; 18 while(~scanf("%d",&n)){ 19 FFC(i,1,n)scanf("%lf%lf",&g[i].x,&g[i].y); 20 printf("%.2lf\n",fuck(n)); 21 } 22 return 0; 23 }