Page Rank
Time Limit: 3000/1500 MS (Java/Others) Memory Limit: 100000/100000 K (Java/Others)Total Submission(s): 346 Accepted Submission(s): 105
For simplicity, in this problem PageRank vector q is defined as q = Gq, Where , S is the destination-by-source stochastic matrix, U is all one matrix, n is the number of nodes and α is the weight between 0 and 1 (here we use 0.85).
For the example on the right, we have:
Denote the current PageRank vector and the next PageRank vector by q cur and q next respectively. The process is to compute the iterative powering for finding the first eigenvector.
The computation ends until for some small ε(10 -10).
For each case, there are multiple lines. The first line contains an integer N(N<=3000), which represents the number of pages. Then a N*N zero-one matrix follows. The element E ij (0 <= i, j < N) on the matrix represents whether the i-th page has a hyper link to the j-th page.
4
0111
0011
0001
0100
题目大意:
说实话,这个题目最难的应该是题意了把,这个题目读了很多次都没有读懂,首先读一下题目大意,然后大体了解一下背景,主要是那个公式:然后,我说一下公式的每隔变量都代表啥:
G =α∗S+(1−α)∗1n∗U
,其中
α
是一个常数 等于
0.85
,然后
S
是一个
解题思路:
其实读懂了题目就很简单了,关键是题目的意思,就是按照题目的意思进行模拟就行了,处理到
My Code:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
using namespace std;
const int MAXN = 3e3+5;
const double eps = 1e-10;
double a[MAXN][MAXN];
char str[MAXN][MAXN];
int n;
bool Judge(double qcur[], double qnext[]){
double sum = 0;
for(int i=0; i<n; i++)
sum += fabs(qcur[i]-qnext[i]);
if(sum < eps)
return 0;
return 1;
}
double qcur[MAXN], qnext[MAXN];
int main()
{
while(~scanf("%d",&n)){
for(int i=0; i<n; i++)
scanf("%s",str[i]);
memset(a, 0, sizeof(a));
memset(qcur, 0, sizeof(qcur));
memset(qnext, 0, sizeof(qnext));
double tmp = 0.85;///阿尔法
for(int i=0; i<n; i++){
double cnt = 0;
for(int j=0; j<n; j++)
if(str[i][j] == '1')
cnt++;
for(int j=0; j<n; j++)
if(str[i][j] == '1')
a[j][i] = tmp/cnt;
}
for(int i=0; i<n; i++)
for(int j=0; j<n; j++)
a[i][j] += 0.15/n;
for(int i=0; i<n; i++)
for(int j=0; j<n; j++)
qcur[i] += a[i][j];
for(int i=0; i<n; i++)
for(int j=0; j<n; j++)
qnext[i] += qcur[j] * a[i][j];
while(Judge(qcur, qnext)){
for(int i=0; i<n; i++)
qcur[i] = qnext[i];
memset(qnext, 0, sizeof(qnext));
for(int i=0; i<n; i++)
for(int j=0; j<n; j++)
qnext[i] += qcur[j] * a[i][j];
}
for(int i=0; i<n-1; i++)
printf("%.2f ",qcur[i]);
printf("%.2f\n",qcur[n-1]);
}
return 0;
}