HDU 5097 Page Rank(矩阵模拟)——2014上海全国邀请赛——题目重现(感谢上海大学提供题目)

传送门

Page Rank

Time Limit: 3000/1500 MS (Java/Others)    Memory Limit: 100000/100000 K (Java/Others)
Total Submission(s): 346    Accepted Submission(s): 105


Problem Description
Evaluation and rank of web pages is a hot topic for many internet companies and researchers. PageRank is a link analysis tool and it assigns a numerical weighting to each element of a hyperlinked set of documents, such as the World Wide Web, with the purpose of "measuring" its relative importance within the set. The algorithm may be applied to any collection of entities with reciprocal quotations and references. The numerical weight that it assigns to any given element E is referred to as the PageRank of E and denoted by . Other factors like Author Rank can contribute to the importance of an entity.

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).
 

Input
The input contains many test cases.

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.
 

Output
Output one line with the eigenvector. The numbers should be separated by a space and be correctly rounded to two decimal places.
 

Sample Input
  
  
4
0111
0011
0001
0100

题目大意:

说实话,这个题目最难的应该是题意了把,这个题目读了很多次都没有读懂,首先读一下题目大意,然后大体了解一下背景,主要是那个公式:然后,我说一下公式的每隔变量都代表啥: G =αS+(1α)1nU ,其中 α 是一个常数 等于 0.85 ,然后 S 是一个 nn 的矩阵,怎么算的呢,首先题目输入中给了我们一个 nn 的01矩阵,然后我们通过这个矩阵来构造出 S 来,首先将输入的矩阵中每一行的 1 的个数 cnt 求出来,然后在 S 中就是 1/cnt ,然后将行列转置,输入矩阵 1 对应的行和列,就是 S 矩阵中对应的列和行,然后值就是 1/cnt ,限制再来说一下 n ,n是输入的时候给定的, U 是一个全是 1 的矩阵,当时我是怎么都没读出来这个意思 U is all one matrix 我理解的就是 这个是一个矩阵,唉,这英语水平绝了,我们知道了这个之后就是让我们求一个特征向量,然后满足 qnext=Gqcur 最后当这个 q 收敛了就不用算了。

解题思路:

其实读懂了题目就很简单了,关键是题目的意思,就是按照题目的意思进行模拟就行了,处理到 qnextqcur<eps就行了(其实也可以就是循环 20 次就行了)。

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;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值