# 3.18 UR17

T1：

T2：

T3：

T1:

f[i]=min(f[i],f[i&a[i]]+a[i])，复杂度$$O(n*max_w)$$

#include <iostream>
#include <cstdlib>
#include <cstring>
#include <cstdio>
#include <algorithm>
#include <cmath>
using namespace std;
#define int long long
#define mmin(a,b) (a<b?a:b)
int n,f[601000],a[601000];
bool pd[601000];
signed main()
{
//	freopen("ex_treeand3.in","r",stdin);
scanf("%lld",&n);
for(int i=1;i<=n;i++) scanf("%lld",&a[i]);
int all=0,zz=0;
for(int i=1;i<=n;i++) all|=a[i];
zz=all;
for(int i=1;i<=n;i++) zz&=a[i];
sort(a+1,a+n+1);
for(int i=1;i<=n;i++)
{
pd[all^a[i]]=1;
a[i]^=zz;
}
all^=zz;
pd[0]=1;
for(int i=all;i>=0;i--)
{
if(zz&i) continue;
for(int j=0;j<=18;j++)
{
if(zz&(1<<j)) continue;
pd[i]|=pd[i|(1<<j)];
}
}
memset(f,30,sizeof(f));
f[0]=0;
for(int i=1;i<=all;i++)
{
if(i&zz) continue;
for(int j=i;j;j=(j-1)&i)
{
if((i^j)>f[i]) break;
if(pd[j]) f[i]=mmin(f[i],f[i^j]+(i^j));
}
}
printf("%lld",f[all]+zz*n);
return 0;
}


T2:

i和j的最小距离必须小于等于二分的答案v。

f[i][j]可以有f[k][j]转移过来，k为j的某个端点，j为与i相连的直线；或f[k][j]，k与i相连。

#include <iostream>
#include <cstdlib>
#include <cstring>
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <vector>
#include <queue>
using namespace std;
#define fang(x) ((x)*(x))
#define pai pair<int,int>
struct point{
double x,y;
int id;
}q[1010];
point operator + (const point &s1,const point &s2) {return (point){s1.x+s2.x,s1.y+s2.y};}
point operator - (const point &s1,const point &s2) {return (point){s1.x-s2.x,s1.y-s2.y};}
double operator * (const point &s1,const point &s2) {return s1.x*s2.y-s1.y*s2.x;}
struct line{
point a,b;
}l[1010];
int n,f[1010][1010],du[1010],stx,sty;
double eps=1e-7;
void bfs(double);
vector<int> ve[1010];
double getdisxian(point,line);
double getdisdian(point,point);
point getpoint(line,line);
bool check(double);
signed main()
{
//	freopen("in.txt","r",stdin);
scanf("%d%d%d",&n,&stx,&sty);
for(int i=1;i<=n;i++) scanf("%lf%lf",&q[i].x,&q[i].y),q[i].id=i;
for(int i=1;i<n;i++)
{
int x,y;
scanf("%d%d",&x,&y);
l[i].a=q[x]; l[i].b=q[y];
if(q[x].x>q[y].x) swap(l[i].a,l[i].b);
du[x]++; du[y]++;
ve[x].push_back(i);
ve[y].push_back(i);
}
double le=0,ri=2e6;
while(ri-le>eps)
{
double mid=(le+ri)*0.5;
bfs(mid);
if(check(mid)==1) ri=mid;
else le=mid;
}
printf("%.7f",le);
return 0;
}
void bfs(double xian)
{
memset(f,0,sizeof(f));
if(getdisdian(q[stx],q[sty])>xian) return ;
queue<pai> que;
//	cout<<getdisxian(q[8],l[5])<<endl;
for(int i=1;i<=n;i++)
{
if(du[i]!=1) continue;
for(int j=1;j<=n;j++)
{
if(du[j]==1)
{
if(getdisdian(q[i],q[j])>xian)
{
f[i][ve[j][0]]=-1;
continue;
}
if(getdisxian(q[i],l[ve[j][0]])>xian)
{
f[i][ve[j][0]]=-1;
continue;
}
f[i][ve[j][0]]=1;
que.push((pai){i,ve[j][0]});
}
}
}
int sz;
while(que.empty()==0)
{
pai x=que.front(); que.pop();
sz=ve[x.first].size();
for(int i=0;i<sz;i++)
{
int xi=ve[x.first][i];
if(f[l[x.second].a.id][xi]==0)
{
if(getdisxian(l[x.second].a,l[xi])<xian)
{
f[l[x.second].a.id][xi]=1;
que.push((pai){l[x.second].a.id,xi});
}
else
{
f[l[x.second].a.id][xi]=0;
}
}
if(f[l[x.second].b.id][xi]==0)
{
if(getdisxian(l[x.second].b,l[xi])<xian)
{
f[l[x.second].b.id][xi]=1;
que.push((pai){l[x.second].b.id,xi});
}
else
{
f[l[x.second].b.id][xi]=0;
}
}
}
}
}
double getdisdian(point a,point b)
{
return sqrt(fang(a.x-b.x)+fang(a.y-b.y));
}
double getdisxian(point a,line b)
{
point c;
if(fabs(b.a.y-b.b.y)<eps) c.x=a.x,c.y=a.y+1;
else
{
double k;
if(fabs(b.a.x-b.b.x)<eps) k=0;
else k=(b.a.y-b.b.y)/(b.a.x-b.b.x),k=-1.0/k;
c.x=a.x+1; c.y=a.y+k;
}
point d=getpoint((line){a,c},b);
if(d.x<=b.b.x&&d.x>=b.a.x) return getdisdian(a,d);
else
{
if(d.x<b.a.x) return getdisdian(a,b.a);
else return getdisdian(a,b.b);
}
}
point getpoint(line a,line b)
{
point p;
double dot1=(b.a-a.a)*(b.b-a.a),dot2=(b.b-a.b)*(b.a-a.b);
p.x=(a.a.x*dot2+a.b.x*dot1)/(dot1+dot2);
p.y=(a.a.y*dot2+a.b.y*dot1)/(dot1+dot2);
return p;
}
bool check(double xian)
{
if(getdisdian(q[stx],q[sty])>xian) return false;
for(int i=0;i<ve[stx].size();i++)
if(f[sty][ve[stx][i]]==1)
return true;
for(int i=0;i<ve[sty].size();i++)
if(f[stx][ve[sty][i]]==1)
return true;
return false;
}


T3：

posted @ 2018-03-24 21:44  hzoi_wangxh  阅读(279)  评论(0编辑  收藏  举报