Pagini recente » Cod sursa (job #2234839) | Cod sursa (job #2983418) | Cod sursa (job #612924) | Cod sursa (job #2215574) | Cod sursa (job #430176)
Cod sursa(job #430176)
#include <algorithm>
#include <fstream>
#include <vector>
using namespace std;
ifstream fin ("bibel.in");
ofstream fout ("bibel.out");
#define INF 0x3f3f3f3f
#define pb push_back
#define mp make_pair
#define MAX (1<<17)
#define sc second
#define fs first
#define DIM 20
int best[MAX][DIM],dst[DIM][DIM];
vector <pair<int,int> > v,ant;
vector <int> cnt;
int n,m,t,rez;
inline int dist (const pair <int,int> &a,const pair <int,int> &b)
{
return (a.fs-b.fs)*(a.fs-b.fs)+(a.sc-b.sc)*(a.sc-b.sc);
}
inline void solve ()
{
int i,j,k;
n=v.size ();
m=(1<<n);
for (i=0; i<n; ++i)
for (j=0; j<n; ++j)
dst[i][j]=dist(v[i],v[j]);
for (i=0; i<m; ++i)
for (j=0; j<n; ++j)
best[i][j]=INF;
for (i=0; i<n; ++i)
for (j=0; j<(int)ant.size (); ++j)
best[1<<i][i]=min(best[1<<i][i],cnt[j]+dist (v[i],ant[j]));
for (i=0; i<m; ++i)
for (j=0; j<n; ++j)
if (i&(1<<j))
for(k=0; k<n; ++k)
if(i&(1<<k))
best[i][j]=min (best[i][j],best[i^(1<<j)][k]+dst[k][j]);
rez=INF;
ant.clear ();
cnt.clear ();
for (i=0; i<n; ++i)
{
rez=min (rez,best[(1<<n)-1][i]);
ant.push_back(v[i]);
cnt.push_back(best[(1<<n)-1][i]);
}
v.clear ();
fout<<rez<<"\n";
}
int main ()
{
int tip,x,y;
ant.pb (mp (0,0));
cnt.pb (0);
for (fin>>tip; tip!=2; fin>>tip)
{
if (tip==1)
solve ();
else
{
fin>>x>>y;
v.pb (mp (x,y));
}
}
return 0;
}