• bzoj3143 [Hnoi2013]游走


    Description

    一个无向连通图,顶点从1编号到N,边从1编号到M。
    小Z在该图上进行随机游走,初始时小Z在1号顶点,每一步小Z以相等的概率随机选 择当前顶点的某条边,沿着这条边走到下一个顶点,获得等于这条边的编号的分数。当小Z 到达N号顶点时游走结束,总分为所有获得的分数之和。
    现在,请你对这M条边进行编号,使得小Z获得的总分的期望值最小。

    Input

    第一行是正整数N和M,分别表示该图的顶点数 和边数,接下来M行每行是整数u,v(1≤u,v≤N),表示顶点u与顶点v之间存在一条边。 输入保证30%的数据满足N≤10,100%的数据满足2≤N≤500且是一个无向简单连通图。

    Output

    仅包含一个实数,表示最小的期望值,保留3位小数。

    Sample Input

    3 3
    2 3
    1 2
    1 3

    Sample Output

    3.333

    HINT

    边(1,2)编号为1,边(1,3)编号2,边(2,3)编号为3。

    正解:高斯消元。

    如果直接计算每条边出现的次数,很不好算。但是如果我们计算点的出现次数,那就很方便了。我们先计算出每个点的度,然后把n直接删掉,因为n这个点不会对答案产生任何贡献。设每个点的期望出现次数为q[i],度数为d[i]。那么对于每个点而言,q[i]=∑q[j]/d[j],其中i与j连通。特别地,q[1]=1+∑q[j]/d[j]。然后我们用高斯消元求出每个点的期望次数以后,就能算出每条边的期望次数。设每条边期望次数为f[i],那么f[i]=q[u]/d[u]+q[v]/d[v],这个应该很显然。然后直接将边的出现次数从大到小排序取i,求个和就行了。

     1 //It is made by wfj_2048~
     2 #include <algorithm>
     3 #include <iostream>
     4 #include <complex>
     5 #include <cstring>
     6 #include <cstdlib>
     7 #include <cstdio>
     8 #include <vector>
     9 #include <cmath>
    10 #include <queue>
    11 #include <stack>
    12 #include <map>
    13 #include <set>
    14 #define inf (1e18)
    15 #define eps (1e-10)
    16 #define il inline
    17 #define RG register
    18 #define ll long long
    19 #define File(s) freopen(s".in","r",stdin),freopen(s".out","w",stdout)
    20 
    21 using namespace std;
    22 
    23 struct edge{ int nt,to; }g[300010];
    24 struct node{ int u,v; }e[300010];
    25 
    26 double a[510][510],tot[300010],ans;
    27 int head[510],d[510],n,m,num;
    28 
    29 il int gi(){
    30     RG int x=0,q=1; RG char ch=getchar(); while ((ch<'0' || ch>'9') && ch!='-') ch=getchar();
    31     if (ch=='-') q=-1,ch=getchar(); while (ch>='0' && ch<='9') x=x*10+ch-48,ch=getchar(); return q*x;
    32 }
    33 
    34 il void insert(RG int from,RG int to){ g[++num]=(edge){head[from],to},head[from]=num; return; }
    35 
    36 il void gauss(){
    37     for (RG int i=1;i<=n;++i){
    38     RG double maxs=-1.0; RG int id=i;
    39     for (RG int j=i;j<=n;++j) if (fabs(a[j][i])+eps>maxs) maxs=fabs(a[j][i]),id=j;
    40     if (id!=i) for (RG int j=1;j<=n+1;++j) swap(a[i][j],a[id][j]);
    41     RG double t=a[i][i]; for (RG int j=i;j<=n+1;++j) a[i][j]/=t;
    42     for (RG int j=1;j<=n;++j){
    43         if (i==j) continue; t=a[j][i];
    44         for (RG int k=1;k<=n+1;++k) a[j][k]-=t*a[i][k];
    45     }
    46     }
    47     return;
    48 }
    49 
    50 il void work(){
    51     cin>>n>>m,n--;
    52     for (RG int i=1;i<=m;++i){
    53     scanf("%d%d",&e[i].u,&e[i].v);
    54     d[e[i].u]++,d[e[i].v]++;
    55     insert(e[i].u,e[i].v),insert(e[i].v,e[i].u);
    56     }
    57     for (RG int x=1;x<=n;++x){
    58     for (RG int i=head[x];i;i=g[i].nt) if (g[i].to!=n+1) a[x][g[i].to]=1.0/d[g[i].to];
    59     a[x][x]=-1.0;
    60     }
    61     a[1][n+1]=-1.0; gauss();
    62     for (RG int i=1;i<=m;++i) tot[i]=a[e[i].u][n+1]/d[e[i].u]+a[e[i].v][n+1]/d[e[i].v];
    63     sort(tot+1,tot+m+1); for (RG int i=1;i<=m;++i) ans+=tot[i]*(m-i+1); printf("%0.3lf",ans);
    64     return;
    65 }
    66 
    67 int main(){
    68     File("walk");
    69     work();
    70     return 0;
    71 }
  • 相关阅读:
    第一篇:数据仓库概述
    第五篇:数据库系统的实现
    第四篇:SQL
    Django框架ORM常用参数汇总_模型层
    Django框架ORM常用字段汇总_模型层
    Boost.Asio的使用技巧
    Boost库之asio io_service以及run、run_one、poll、poll_one区别
    Boost::asio io_service 实现分析
    boost asio中io_service类的几种使用
    boost::io_service解读
  • 原文地址:https://www.cnblogs.com/wfj2048/p/6533606.html
Copyright © 2020-2023  润新知