当前位置: 首页 > news >正文

BZOJ 2337 XOR和路径(高斯消元)

题目链接:http://61.187.179.132/JudgeOnline/problem.php?id=2337

题意:给定一个带权无向图。从1号点走到n号点。每次从当前点随机(等概率)选择一条相邻边走下去。每条到达n的路径的值为走过的边权的抑或。求期望。

思路:将权值按照二进制位一位一位进行。设f[i]表示从i节点走到n节点的期望。i的度数为d[i]。那么若一条边(i,j)的权值为0,则f[i]+=f[j]/d[i];否则f[i]+=(1-f[j])/d[i]。

 

#include <iostream>
#include <cstdio>
#include <string.h>
#include <algorithm>
#include <cmath>
#include <vector>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <map>
#include <ctype.h>
#include <time.h>
        
        
#define abs(x) ((x)>=0?(x):-(x))
#define i64 long long
#define u32 unsigned int
#define u64 unsigned long long
#define clr(x,y) memset(x,y,sizeof(x))
#define CLR(x) x.clear()
#define ph(x) push(x)
#define pb(x) push_back(x)
#define Len(x) x.length()
#define SZ(x) x.size()
#define PI acos(-1.0)
#define sqr(x) ((x)*(x))
#define MP(x,y) make_pair(x,y)
#define EPS 1e-15
        
        
#define FOR0(i,x) for(i=0;i<x;i++)
#define FOR1(i,x) for(i=1;i<=x;i++)
#define FOR(i,a,b) for(i=a;i<=b;i++)
#define FORL0(i,a) for(i=a;i>=0;i--)
#define FORL1(i,a) for(i=a;i>=1;i--)
#define FORL(i,a,b)for(i=a;i>=b;i--)
        
        
#define rush() int CC;for(scanf("%d",&CC);CC--;)
#define Rush(n)  while(scanf("%d",&n)!=-1)
using namespace std;
        
        
void RD(int &x){scanf("%d",&x);}
void RD(i64 &x){scanf("%lld",&x);}
void RD(u64 &x){scanf("%I64u",&x);}
void RD(u32 &x){scanf("%u",&x);}
void RD(double &x){scanf("%lf",&x);}
void RD(int &x,int &y){scanf("%d%d",&x,&y);}
void RD(i64 &x,i64 &y){scanf("%lld%lld",&x,&y);}
void RD(u32 &x,u32 &y){scanf("%u%u",&x,&y);}
void RD(double &x,double &y){scanf("%lf%lf",&x,&y);}
void RD(double &x,double &y,double &z){scanf("%lf%lf%lf",&x,&y,&z);}
void RD(int &x,int &y,int &z){scanf("%d%d%d",&x,&y,&z);}
void RD(i64 &x,i64 &y,i64 &z){scanf("%lld%lld%lld",&x,&y,&z);}
void RD(u32 &x,u32 &y,u32 &z){scanf("%u%u%u",&x,&y,&z);}
void RD(char &x){x=getchar();}
void RD(char *s){scanf("%s",s);}
void RD(string &s){cin>>s;}
        
        
void PR(int x) {printf("%d\n",x);}
void PR(int x,int y) {printf("%d %d\n",x,y);}
void PR(i64 x) {printf("%lld\n",x);}
void PR(i64 x,i64 y) {printf("%lld %lld\n",x,y);}
void PR(u32 x) {printf("%u\n",x);}
void PR(u64 x) {printf("%llu\n",x);}
void PR(double x) {printf("%.3lf\n",x);}
void PR(double x,double y) {printf("%.5lf %.5lf\n",x,y);}
void PR(char x) {printf("%c\n",x);}
void PR(char *x) {printf("%s\n",x);}
void PR(string x) {cout<<x<<endl;}
   
void upMin(int &x,int y) {if(x>y) x=y;}
void upMin(i64 &x,i64 y) {if(x>y) x=y;}
void upMin(double &x,double y) {if(x>y) x=y;}
void upMax(int &x,int y) {if(x<y) x=y;}
void upMax(i64 &x,i64 y) {if(x<y) x=y;}
void upMax(double &x,double y) {if(x<y) x=y;}
        
const int mod=100000007;
const i64 inf=((i64)1)<<60;
const double dinf=1000000000000.0;
const int INF=1000000000;
const int N=105;


double g[N][N],ans[N];
int n,m,d[N];
vector<pair<int,int> > V[N];


int sgn(double x)
{
    if(x>EPS) return 1;
    if(x<-EPS) return -1;
    return 0;
}


void Gauss()
{
    int i,j,k;
    double x;
    for(i=1;i<=n;i++)
    {
        for(j=i;j<=n;j++) if(sgn(g[j][i])) break;
        if(j>n) continue;
        for(k=1;k<=n+1;k++) swap(g[i][k],g[j][k]);
        for(k=i+1;k<=n;k++)
        {
            if(sgn(g[k][i])==0) continue;
            x=g[k][i]/g[i][i];
            for(j=i;j<=n+1;j++) g[k][j]-=g[i][j]*x;
        }
    }
    for(i=n;i>=1;i--)
    {
        ans[i]=g[i][n+1];
        for(j=i+1;j<=n;j++) ans[i]-=ans[j]*g[i][j];
        ans[i]/=g[i][i];
    }
}


int main()
{
    RD(n,m);
    int i,j,k,x,y,w;
    FOR1(i,m)
    {
        RD(x,y,w);
        d[x]++; 
        V[x].pb(MP(y,w));
        if(x==y) continue;
        d[y]++;
        V[y].pb(MP(x,w));
    }
    double p=0,temp;
    for(i=0;i<=30;i++)
    {
        clr(g,0);
        FOR1(j,n) 
        {
            temp=0; g[j][j]=1;
            FOR0(k,SZ(V[j]))
            {
                y=V[j][k].first;
                w=V[j][k].second;
                if(w&(1<<i)) temp+=1.0/d[j],g[j][y]+=1.0/d[j];
                else g[j][y]-=1.0/d[j];
            }
            g[j][n+1]=temp;
        }
        FOR1(j,n+1) g[n][j]=0;
        g[n][n]=1;
        Gauss();
        p+=(1<<i)*ans[1];
    }
    PR(p);
}

相关文章:

  • Adas术语简称
  • extern c 谈
  • 转载 一堂价值39万元的课,把她看完,你一定会有所获!
  • 关于centos联网的问题
  • 第二章 Java内存区域与内存溢出异常
  • java 用进程调用外部命令并获取返回结果
  • 购物狂欢节的背后,是谁在让你在吃土?
  • Hadoop之HDFS中NameNode的工作机制
  • 目录操作常用命令
  • 史上最全的Python开发秘籍,学完这些年薪百万不是问题
  • 设计模式编写表单验证
  • spark hive python依赖第三方包
  • Asp 输出编码为UTF-8格式的XML内容的注意事项
  • [Leetcode] Permutations II
  • Acticles about Interface!
  • ES6 学习笔记(一)let,const和解构赋值
  • HTML-表单
  • HTTP中的ETag在移动客户端的应用
  • Lucene解析 - 基本概念
  • open-falcon 开发笔记(一):从零开始搭建虚拟服务器和监测环境
  • Python实现BT种子转化为磁力链接【实战】
  • XForms - 更强大的Form
  • 快速体验 Sentinel 集群限流功能,只需简单几步
  • 腾讯大梁:DevOps最后一棒,有效构建海量运营的持续反馈能力
  • 提醒我喝水chrome插件开发指南
  • linux 淘宝开源监控工具tsar
  • # Apache SeaTunnel 究竟是什么?
  • #if #elif #endif
  • #Linux(make工具和makefile文件以及makefile语法)
  • %3cscript放入php,跟bWAPP学WEB安全(PHP代码)--XSS跨站脚本攻击
  • (07)Hive——窗口函数详解
  • (1)(1.13) SiK无线电高级配置(五)
  • (4)Elastix图像配准:3D图像
  • (Matlab)遗传算法优化的BP神经网络实现回归预测
  • (独孤九剑)--文件系统
  • (全注解开发)学习Spring-MVC的第三天
  • (五)c52学习之旅-静态数码管
  • (一)RocketMQ初步认识
  • (转)机器学习的数学基础(1)--Dirichlet分布
  • (转)全文检索技术学习(三)——Lucene支持中文分词
  • **PHP二维数组遍历时同时赋值
  • .bat批处理(六):替换字符串中匹配的子串
  • .NET CLR基本术语
  • .NET Core 中插件式开发实现
  • .NET下ASPX编程的几个小问题
  • .net中应用SQL缓存(实例使用)
  • @Autowired多个相同类型bean装配问题
  • @cacheable 是否缓存成功_让我们来学习学习SpringCache分布式缓存,为什么用?
  • [ IO.File ] FileSystemWatcher
  • [AX]AX2012 AIF(四):文档服务应用实例
  • [BT]BUUCTF刷题第9天(3.27)
  • [BZOJ 1032][JSOI2007]祖码Zuma(区间Dp)
  • [C#]使用PaddleInference图片旋转四种角度检测
  • [C#C++]类CLASS
  • [C/C++]数据结构 循环队列