Sunday, 3 June 2012

UVa 10496 Collecting Beepers Solution

#include<iostream>
#include<list>
#include<string>
#include<cstring>
#include<sstream>
#include<cctype>
#include<string.h>
#include<algorithm>
#include<cmath>
#include<stack>
#include<fstream>
#include<cstdlib>
#include<vector>
#include<map>
#include<utility>
#include<iomanip>
#include<queue>

using namespace std;

#define INF (1<<29)
#define SET(a) memset(a,-1,sizeof(a))
#define ALL(a) a.begin(),a.end()
#define CLR(a) memset(a,0,sizeof(a))
#define FILL(a,v) memset(a,v,sizeof(a))
#define PB push_back
#define FOR(i,n) for(int i = 0;i<n;i++)
#define PI acos(-1.0)
#define EPS 1e-9
#define MP(a,b) make_pair(a,b)
#define READ(f) freopen(f, "r", stdin)
#define WRITE(f) freopen(f, "w", stdout)
#define LL long long

struct Z{
    int x,y;
}beeper[12];

int memo[12][1<<11],beepersNum;

int func(int prev, int bitmask)
{
    if(bitmask==(1<<beepersNum)-1)
        return abs(beeper[prev].x-beeper[0].x)+abs(beeper[prev].y-beeper[0].y);

    if(memo[prev][bitmask]!=-1)  return memo[prev][bitmask];

    int ret=1000000;

    for(int i=0;i<beepersNum;i++)
        if((bitmask & (1<<i))==0)
            ret=min(ret, func(i, bitmask | (1<<i))+abs(beeper[i].x-beeper[prev].x)+abs(beeper[i].y-beeper[prev].y));

    return memo[prev][bitmask]=ret;
}

int main()
{
    int tc,faltu;
    cin>>tc;
    while(tc--)
    {
        cin>>faltu>>faltu;
        cin>>beeper[0].x>>beeper[0].y;
        cin>>beepersNum;
        beepersNum++;

        for(int i=1;i<beepersNum;i++)
            cin>>beeper[i].x>>beeper[i].y;

        SET(memo);

        cout<<"The shortest path has length "<<func(0,1)<<endl;
    }
return 0;
}

No comments:

Post a Comment