首页 技术 正文
技术 2022年11月20日
0 收藏 632 点赞 3,603 浏览 2023 个字

纪念首道期望题(虽说绿豆蛙的归宿才是,但是我打的深搜总觉得不正规)。

我们求出每条边的期望经过次数,然后排序,经过多的序号小,经过少的序号大,这样就可以保证最后的值最小。

对于每一条边的期望经过次数,其实是从它起点和终点来的。设f[]为每个点经过的期望,out[]为每个点的出度

设一条边,起点为u,终点为v。那么它的期望经过次数为f[u]/out[u]+f[v]/out[v]

这样问题就转化为求每个点的期望经过次数了

对于起点,一开始经过一次,也可能从其他点走过来.

f[1]=1+sigma(f[j]/out(j),j和1有边)

f[i]=sigma(f[j]/out(j),j和i有边)  //i>=2

这是n个变量n个方程的方程组,高斯消元解方程组,O(n^3)

#include<iostream>#include<cstdlib>#include<cstdio>#include<cstring>#include<cmath>#include<algorithm>using namespace std;#define pos(i,a,b) for(int i=(a);i<=(b);i++)#define pos2(i,a,b) for(int i=(a);i>=(b);i--)#define N 510int n,m;int read(){    int su=0;    char ch=getchar();    while(ch<'0'||ch>'9')       ch=getchar();    while(ch<='9'&&ch>='0'){su=su*10+ch-'0';ch=getchar();    }    return su;}double out[N];double a[N][N],b[N],x[N];int lian[N][N];void swap(double &xx,double &yy){     double temp;     temp=xx;     xx=yy;     yy=temp;}void gauss(){    double t;    int im,num=1;    for(int k=1;k<n;k++,num++) {        im=k;        pos(i,k+1,n)           if(fabs(a[i][k])>fabs(a[im][k]))             im=i;        if(im!=k){           pos(i,k,n)             swap(a[num][i],a[im][i]);           swap(b[num],b[im]);        }        if(!a[num][k]){           num--;           continue;        }        pos(i,num+1,n){           if(!a[num][k])             continue;           t=a[i][k]/a[num][k];           pos(j,k,n+1)              a[i][j]-=t*a[k][j];           b[i]-=t*b[k];        }    }    pos2(i,n,1){       pos2(j,n,i+1)         b[i]-=a[i][j]*x[j];       x[i]=b[i]/a[i][i];    }}struct qian{int from,to;double e;}cun[N*N];bool aaa(const qian &a,const qian &b){return a.e<b.e;}double ans;int messi(){freopen("walk.in","r",stdin);freopen("walk.out","w",stdout);n=read();m=read();pos(i,1,m){int x,y;x=read();y=read();lian[x][y]=lian[y][x]=1;out[x]+=1.0;out[y]+=1.0;cun[i].from=x;cun[i].to=y;}out[n]=0.0;a[1][1]=-1.0;b[1]=-1.0;pos(i,2,n){if(lian[i][1]==1&&out[i]){a[1][i]=1.0/out[i];}}pos(i,2,n){a[i][i]=-1.0;pos(j,1,n){if(j!=i&&lian[j][i]==1&&out[j]){a[i][j]=1.0/out[j];}}}gauss();pos(i,1,m){if(out[cun[i].from]!=0&&out[cun[i].to]!=0)cun[i].e=x[cun[i].from]/out[cun[i].from]+x[cun[i].to]/out[cun[i].to];else{if(out[cun[i].from]==0)  cun[i].e=x[cun[i].to]/out[cun[i].to];else  cun[i].e=x[cun[i].from]/out[cun[i].from];}}sort(cun+1,cun+m+1,aaa);pos(i,1,m){ans+=cun[i].e*(double)(m-i+1);}printf("%0.3lf",ans);return 0;}int hallmeow=messi();int main(){;}

  

下一篇: 数据结构-->栈
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,084
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,559
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,408
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,181
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,818
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,901