您的位置:首页 > 其它

[HNOI2011]XOR和路径

2016-06-01 20:43 453 查看
拿这题+JLOI的装备购买学了下高斯消元。

这道题的话,非常神奇一个地方在于它的状态。

单独考虑二进制的每一位,那么每一条边的权值就只有0、1之分了。

设f(x)表示从x走到n是1的概率,那么就有f(x)=⎧⎩⎨⎪⎪⎪⎪⎪⎪⎪⎪⎪⎪0∑(u,v)∈E∩x==u{f(v)1−f(v),w(u,v)=0,w(u,v)=1degree(x),x=n,x≠n

这是因为如果我们在x,那么我们向与x相连的每条边走的概率是相等的。

但是我们实际上并不好求从每条边走来的概率,所以如果我们的状态表示的是从1到x是1的概率的话实际上是难以转移的。

非常科学的消元法当然是列主元消元法,但是我写这道题的时候用了一些非常奇怪不过非常简单的做法。就是我们倒着消行列,每次就直接认为第i列主元就在第i行。这样的话值域当然可能爆,也可能第i列第i行的直接是0不能当主元,所以不是很科学。。不过这个东西应该非常非常难卡,数据肯定不会卡的。

代码:

#include<cstdio>
#include<iostream>
using namespace std;
#include<algorithm>
#include<cstring>
#include<cmath>
typedef double LLF;
const int N=100+5,M=10000+5;
int next[M<<1],succ[M<<1],w[M<<1],ptr
,etot=1;
int deg
;
void addedge(int from,int to,int wt){
next[etot]=ptr[from],ptr[from]=etot,succ[etot]=to,w[etot++]=wt;
}
LLF eps=1e-15;
LLF mat

;
int main(){
int n,m;
scanf("%d%d",&n,&m);
int u,v,wt;
while(m--){
scanf("%d%d%d",&u,&v,&wt);
addedge(u,v,wt);
++deg[u];
if(u!=v){
addedge(v,u,wt);
++deg[v];
}
}
LLF ans=0;
for(int o=1;o<=1e9;o<<=1){
//printf("----%d---\n",o);
memset(mat,0,sizeof(mat));
mat

=1;
for(int i=n;--i;){
mat[i][i]=deg[i];
for(int j=ptr[i];j;j=next[j])
if(w[j]&o){
mat[i][n+1]+=1;
mat[i][succ[j]]+=1;
}
else mat[i][succ[j]]-=1;
}

for(int i=n,j;i;--i)
for(j=i;--j;)
if(fabs(mat[j][i])>eps){
LLF tmp=mat[j][i]/mat[i][i];
for(int k=i;k;--k)mat[j][k]-=mat[i][k]*tmp;
mat[j][n+1]-=mat[i][n+1]*tmp;
}

ans+=o*mat[1][n+1]/mat[1][1];

//printf("ans=%f\n",(double)(mat
[n+1]/mat

));
}
printf("%.3f\n",ans);
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: