Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<stdio.h>
- int Xs,Ys;
- struct pos{
- int x,y;
- };
- int const N = 12;
- int n;
- int dist[N][N];
- int abs(int x){
- return (x < 0 ? x*-1 : x);
- }
- int cd(pos a,pos b){
- return abs(a.x - b.x) + abs(a.y - b.y);
- }
- int min(int a,int b){
- return (a < b ? a : b);
- }
- int dp[N][(1<<N)];
- int cal(int now , int bit){
- if(bit == (1<<(n+1))-1){
- return dist[now][0];
- }
- if(dp[now][bit] != -1)return dp[now][bit];
- int ans = 1e9;
- for(int nxt = 0 ; nxt <= n ; nxt ++){
- if(!(bit&(1<<nxt)) && nxt != now){
- ans = min(ans , cal(nxt , (bit|(1<<nxt))) + dist[now][nxt]);
- }
- }
- return dp[now][bit] = ans;
- }
- int main()
- {
- for(int i = 0 ; i < N ; i ++)for(int j = 0 ; j < (1<<N) ; j ++)dp[i][j] = -1;
- scanf("%d %d",&Xs,&Ys);
- pos all[15];
- scanf("%d %d",&all[0].x ,&all[0].y);
- scanf("%d",&n);
- for(int i = 1 ; i <= n ; i++){
- scanf("%d %d",&all[i].x , &all[i].y);
- }
- for(int i = 0 ; i <= n ; i ++){
- for(int j = 0 ; j <= n ; j ++){
- dist[i][j] = cd(all[i],all[j]);
- }
- }
- printf("%d",cal(0,1));
- return 0;
- }
Add Comment
Please, Sign In to add comment