#include #include #include #define INPUT_FILE "ise.in" using namespace std; struct Point { float x,y; float price; }; float dist( Point a, Point b ) { return sqrt( (b.x-a.x)*(b.x-a.x) + (b.y-a.y)*(b.y-a.y) ); } float calcsuppcost( float **costarr,int store, unsigned long waresen, unsigned warecnt ) { float best = 0.0f; bool initi = true; for( unsigned long i = 0; i < warecnt; ++i ) { if( (waresen & (1ul << i)) != 0 ) { if( (best > costarr[store][i]) || initi ) best = costarr[store][i]; initi = false; } } return best; } float calcbuildcost( long waresen, Point *wares, unsigned warecnt ) { float tot = 0.0f; for( unsigned int i = 0; i < warecnt; ++i ) { if( (waresen & (1ul << i)) != 0 ) { tot += wares[i].price; } } return tot; } int main( int argc, char **argv ) { ifstream infile(INPUT_FILE); int runcnt; infile >> runcnt; for( int runiter = 1; runiter <= runcnt; ++runiter ) { int storecnt, warecnt; infile >> storecnt >> warecnt; //allocate Point *stores = new Point[storecnt]; Point *wares = new Point[warecnt]; //read in for( int i = 0; i < storecnt; ++i ) { infile >> (stores[i].x) >> (stores[i].y); } for( int i = 0; i < warecnt; ++i ) { infile >> (wares[i].x) >> (wares[i].y) >> (wares[i].price); } float **costarr = new float*[storecnt]; //init for( int i = 0; i < storecnt; ++i ) { costarr[i] = new float[warecnt]; for( int j = 0; j < warecnt; ++j ) { costarr[i][j] = dist( stores[i], wares[j] ); } } unsigned long end = (1 << (warecnt)); unsigned long iter = 1ul; float best = -1.0f; bool initi = true; for( ; iter < end; ++iter ) { float currcost = calcbuildcost( iter, wares, warecnt ); //calc best store supp cost for( int i = 0; (i < storecnt) && ((currcost < best) || initi); ++i ) currcost += calcsuppcost( costarr, i, iter, warecnt ); if( (currcost < best) || initi ) best = currcost; initi = false; } cout << "Data Set " << runiter << ":\n"; fprintf( stdout, "%.2f\n", best ); delete [] stores; delete [] wares; } return 0; }