这也是个TSP问题。。不过他的路径是单向的2条路径,所以并不需要状压。。
设其中一个路径到了i另一个到了j的代价为d[i][j]且假设i<j,可以得到前j个点已经走过了。。
那么可以让j一直先走,然后问题就是i什么时候才走呢?可以让i直接走到j+1的位置,然后此时的状态就变成了d[j][j+1],即2个路径在状态表现出来的地位上发生了互换,那么此时就轮到i一直走了。。
所以可以得到转移如下:
d[i][j]=d[i][j-1]+dis(j,j-1) i<j-1
=min{d[k][i]+dis(k,j)} i==j-1
复杂度是O(n^2)。。
/**
* ┏┓ ┏┓
* ┏┛┗━━━━━━━┛┗━━━┓
* ┃ ┃
* ┃ ━ ┃
* ┃ > < ┃
* ┃ ┃
* ┃... ⌒ ... ┃
* ┃ ┃
* ┗━┓ ┏━┛
* ┃ ┃ Code is far away from bug with the animal protecting
* ┃ ┃ 神兽保佑,代码无bug
* ┃ ┃
* ┃ ┃
* ┃ ┃
* ┃ ┃
* ┃ ┗━━━┓
* ┃ ┣┓
* ┃ ┏┛
* ┗┓┓┏━┳┓┏┛
* ┃┫┫ ┃┫┫
* ┗┻┛ ┗┻┛
*/
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
#include<queue>
#include<cmath>
#include<map>
#include<stack>
#include<set>
#define inc(i,l,r) for(int i=l;i<=r;i++)
#define dec(i,l,r) for(int i=l;i>=r;i--)
#define link(x) for(edge *j=h[x];j;j=j->next)
#define mem(a) memset(a,0,sizeof(a))
#define ll long long
#define eps 1e-12
#define succ(x) (1<<x)
#define lowbit(x) (x&(-x))
#define sqr(x) ((x)*(x))
#define mid (x+y>>1)
#define NM 205
#define nm 1000005
#define pi 3.1415926535897931
using namespace std;
const ll inf=100000000000007;
ll read(){
ll x=0,f=1;char ch=getchar();
while(!isdigit(ch)){if(ch=='-')f=-1;ch=getchar();}
while(isdigit(ch))x=x*10+ch-'0',ch=getchar();
return f*x;
}
int a[NM],b[NM],n;
double d[NM][NM],ans;
double dis(int x,int y){return sqrt(sqr(a[x]-a[y])+sqr(b[x]-b[y]));}
int main(){
while(~scanf("%d",&n)){
inc(i,1,n)inc(j,1,n)d[i][j]=inf;
ans=inf;
inc(i,1,n)a[i]=read(),b[i]=read();
d[1][1]=0;
inc(j,2,n)d[1][j]=d[1][j-1]+dis(j-1,j);
inc(i,2,n)inc(j,i+1,n)if(i>=j-1)
inc(k,1,i-1)d[i][j]=min(d[i][j],d[k][i]+dis(k,j));
else d[i][j]=d[i][j-1]+dis(j-1,j);
//inc(i,1,n){inc(j,i+1,n)printf("%.2lf ",d[i][j]);putchar('\n');}
inc(i,1,n-1)ans=min(ans,d[i][n]+dis(i,n));
printf("%.2lf\n",ans);
}
return 0;
}
The shortest path
Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 1574 Accepted Submission(s): 813
Problem Description
There are n points on the plane, Pi(xi, yi)(1 <= i <= n), and xi < xj (i<j). You begin at P1 and visit all points then back to P1. But there is a constraint:
Before you reach the rightmost point Pn, you can only visit the points those have the bigger x-coordinate value. For example, you are at Pi now, then you can only visit Pj(j > i). When you reach Pn, the rule is changed, from now on you can only visit the points those have the smaller x-coordinate value than the point you are in now, for example, you are at Pi now, then you can only visit Pj(j < i). And in the end you back to P1 and the tour is over.
You should visit all points in this tour and you can visit every point only once.
Before you reach the rightmost point Pn, you can only visit the points those have the bigger x-coordinate value. For example, you are at Pi now, then you can only visit Pj(j > i). When you reach Pn, the rule is changed, from now on you can only visit the points those have the smaller x-coordinate value than the point you are in now, for example, you are at Pi now, then you can only visit Pj(j < i). And in the end you back to P1 and the tour is over.
You should visit all points in this tour and you can visit every point only once.
Input
The input consists of multiple test cases. Each case begins with a line containing a positive integer n(2 <= n <= 200), means the number of points. Then following n lines each containing two positive integers Pi(xi, yi), indicating the coordinate of the i-th point in the plane.
Output
For each test case, output one line containing the shortest path to visit all the points with the rule mentioned above.The answer should accurate up to 2 decimal places.
Sample Input
3 1 1 2 3 3 1
Sample Output
6.47 Hint: The way 1 - 3 - 2 - 1 makes the shortest path.
Author
8600
Recommend
Statistic | Submit | Discuss | Note