function d=distOne2Many(x,S,lambda,sqrtFlag)
if nargin==3, sqrtFlag='on';end

[m,n]=size(S);

if strcmp(sqrtFlag,'on'),
    if ~isempty(lambda),
        x = x.*lambda;x=x/sum(x);
        lamMat = spones(S) * spdiags(lambda',0,n,n);
        S=S.*lamMat; sums=sum(S,2); 
        ind=find(sums==0);sums(ind)=1;
        S=spdiags(1./sums,0,m,m) * S;
    end
    X = spones(S) * spdiags(x',0,n,n);
    d = real(acos(full(sum(sqrt(S.*X),2))));
elseif strcmp(sqrtFlag,'off'),
    X = spones(S) * spdiags(x',0,n,n);
    d = real(acos(full(sum(S.*X,2))));
end