题目链接:http://61.187.179.132/JudgeOnline/problem.php?id=3143
题意:一个无向连通图,顶点从1编号到n,边从1编号到m。小Z在该图上进行随机游走,初始时小Z在1号顶点,每一步小Z以相等的概率随机选择当前顶点的某条边,沿着这条边走到下一个顶点,获得等于这条边的编号的分数。当小Z 到达n号顶点时游走结束,总分为所有获得的分数之和。 现在,请你对这m条边进行编号,使得小Z获得的总分的期望值最小。
思路:若得到经过每条边的次数期望,那么只要贪心地给每条边赋权值即可。现在,我们先求每个点被经过的期望f[i],那么:
进而,由于从每个点到达与其相邻点的概率都是一样的,那么对于边e(i,j):
#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-10
#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=1000000007;
const i64 inf=((i64)1)<<60;
const double dinf=1000000000000000000.0;
const int INF=100000000;
const int N=505;
double a[N][N],ans[N];
int n,m,d[N];
vector<double> E;
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(a[j][i])) break;
if(j>n) continue;
for(k=1;k<=n+1;k++) swap(a[i][k],a[j][k]);
for(j=i+1;j<=n;j++)
{
x=a[j][i]/a[i][i];
if(!sgn(x)) continue;
for(k=i;k<=n+1;k++) a[j][k]-=x*a[i][k];
}
}
for(i=n;i>=1;i--)
{
ans[i]=a[i][n+1];
for(j=i+1;j<=n;j++) ans[i]-=ans[j]*a[i][j];
ans[i]/=a[i][i];
}
}
vector<int> V[N],S,T;
int main()
{
RD(n,m);
int i,j,x,y;
FOR1(i,m)
{
RD(x,y); S.pb(x); T.pb(y);
d[x]++;
V[x].pb(y);
d[y]++;
V[y].pb(x);
}
FOR1(i,n) FOR0(j,SZ(V[i]))
{
y=V[i][j];
a[i][y]+=1.0/d[y];
}
FOR1(i,n-1) a[n][i]=0;
FOR1(i,n) a[i][i]+=-1;
a[1][n+1]=-1;
Gauss();
FOR0(i,m)
{
x=S[i]; y=T[i];
E.pb(ans[x]/d[x]+ans[y]/d[y]);
}
sort(E.begin(),E.end());
double Ans=0;
FOR0(i,m) Ans+=(m-i)*E[i];
printf("%.3lf\n",Ans);
}