题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=4824
题意:中文,不解释
题解:双调欧几里德旅行商问题,具体可看dp双调欧几里德旅行商,这里注意的是起点为0,0。
1 #include<cstdio> 2 #define FFC(i,a,b) for(int i=a;i<=b;i++) 3 const int maxn=1011,inf=1e9; 4 int dp[maxn][maxn],t,n; 5 struct node{int x,y;}g[maxn]; 6 int abs(int a){return a>0?a:-a;} 7 int Min(int a,int b){return a>b?b:a;} 8 int dis(int i,int j){ 9 int s1=abs(g[i].x-g[j].x)*400+abs(g[i].y-g[j].y),s2; 10 int max=g[i].y>g[j].y?g[i].y:g[j].y,min=Min(g[i].y,g[j].y); 11 s2=abs(g[i].x-g[j].x)*400+360-max+min; 12 return s1>s2?s2:s1; 13 } 14 int fuck(int n){ 15 FFC(i,1,n)dp[i][i-1]=inf; 16 dp[1][1]=0,dp[2][1]=dis(2,1); 17 FFC(i,3,n)FFC(j,1,i-2) 18 dp[i][j]=dp[i-1][j]+dis(i-1,i),dp[i][i-1]=Min(dp[i][i-1],dp[i-1][j]+dis(j,i)); 19 return dp[n][n-1]+dis(n,n-1); 20 } 21 int main(){ 22 scanf("%d",&t); 23 g[1].x=g[1].y=0; 24 while(t--){ 25 scanf("%d",&n); 26 FFC(i,2,n+1)scanf("%d%d",&g[i].x,&g[i].y); 27 printf("%d ",fuck(n+1)+n*10); 28 } 29 return 0; 30 }