# 题意与分析

$e_i = \sum_{j=1}^n p_{i,j}e_{j} , 2\le i \le n$

# 代码

#include <bits/stdc++.h>

using namespace std;

vector<pair<int, int>> edges;
vector<int> G[505];

double arr[505][505];
int main() {
int n, m;
while (cin >> n >> m) {
for (int i = 1, u, v; i <= m; ++i) {
cin >> u >> v;
if (u > v)
swap(v, u);
edges.push_back(make_pair(u, v));
G[u].push_back(v);
G[v].push_back(u);
}
memset(arr, 0, sizeof(arr));
for (int i = 1; i <= n; ++i)
arr[i][i] = -1.0;
arr[1][n + 1] = -1.0;
for (int i = 1; i < n; i++) {
for (int j = 0; j < G[i].size(); ++j)
arr[G[i][j]][i] += 1.0 / (int)G[i].size();
}
for (int i = 1; i <= n; ++i) {
int idx = i;
for (int j = i + 1; j <= n; ++j)
if (fabs(arr[j][i]) > fabs(arr[idx][i]))
idx = j;
assert(fabs(arr[idx][i]) > 1e-10);
if (idx != i)
for (int j = i; j <= n + 1; ++j)
swap(arr[i][j], arr[idx][j]);
for (int j = 1; j <= n; ++j)
if (i != j) {
double t = arr[j][i] / arr[i][i];
for (int k = i; k <= n + 1; ++k)
arr[j][k] -= arr[i][k] * t;
}
}
/*
for (int i = 1; i <= n; ++i) {
for (int j = 1; j <= n + 1; ++j) {
cout << arr[i][j] << " ";
}
cout << endl;
}
*/
double e_node[505];
double e_edge[500 * 500 / 2 + 5];
memset(e_edge, 0, sizeof(e_edge));
memset(e_node, 0, sizeof(e_node));
for (int i = 1; i <= n; ++i) {
e_node[i] = arr[i][n + 1] / arr[i][i];
}
for (int i = 0; i < edges.size(); ++i) {
e_edge[i] += e_node[edges[i].first] / G[edges[i].first].size();
if (edges[i].second != n)
e_edge[i] += e_node[edges[i].second] / G[edges[i].second].size();
}
sort(e_edge, e_edge + m);
double ans = 0;
for (int i = 0; i < m; ++i)
ans += e_edge[i] * (m - i);
cout << fixed << setprecision(3) << ans << endl;
}
return 0;
}

posted @ 2019-04-19 12:45  SamHX  阅读(60)  评论(0编辑  收藏