码迷,mamicode.com
首页 > 其他好文 > 详细

UvaLive 6600 Spanning trees in a secure lock pattern 矩阵行列式

时间:2014-08-28 17:01:00      阅读:350      评论:0      收藏:0      [点我收藏+]

标签:矩阵

链接:https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=4611

题意:给一个N*N个点的矩阵(N<=6),每个点只能和周围八个点相连,问有多少种生成树的方式。

思路:题里给的很明白,就是列一个每个点的边的矩阵,然后求子矩阵的行列式就可以了,因为N只有6,所以打表就可以了。

打表代码:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <ctype.h>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
#define eps 1e-8
#define INF 0x7fffffff
#define PI acos(-1.0)
#define seed 31//131,1313
typedef long long LL;
typedef unsigned long long ULL;
using namespace std;
#define MOD 1000
#define maxn 40
#define maxm 40
struct Matrix
{
    int n,m;
    double a[maxn][maxm];
    void change(int c,int d)
    {
        n=c;
        m=d;
        for(int i=0; i<n; i++)
            for(int j=0; j<m; j++)
                a[i][j]=0;
    }
    void Copy(const Matrix &x)
    {
        n=x.n;
        m=x.m;
        for(int i=0; i<n; i++)
            for(int j=0; j<m; j++)
                a[i][j]=x.a[i][j];
    }
    void build(int n)
    {
        change(n*n,n*n);
        for(int i=0; i<n*n; i++)
        {
            if(i%n!=0)
            {
                a[i][i-1]=-1;
                a[i-1][i]=-1;
                a[i][i]++;
                a[i-1][i-1]++;
            }
            if(i%n!=0&&i/n!=0)
            {
                a[i][i-n-1]=-1;
                a[i-n-1][i]=-1;
                a[i][i]++;
                a[i-n-1][i-n-1]++;
            }
            if(i%n!=0&&i/n!=n-1)
            {
                a[i][i+n-1]=-1;
                a[i+n-1][i]=-1;
                a[i][i]++;
                a[i+n-1][i+n-1]++;
            }
            if(i/n!=n-1)
            {
                a[i][i+n]=-1;
                a[i+n][i]=-1;
                a[i][i]++;
                a[i+n][i+n]++;
            }
        }
    }
    double det()
    {
        for(int i=1; i<n; i++)
        {
            for(int j=0; j<i; j++)
                if(a[i][j]!=0)
                {
                    for(int k=j+1; k<m; k++)
                        a[i][k]-=(a[j][k]*a[i][j]/a[j][j]);
                    a[i][j]=0;
                }
        }
        double ans=1;
        for(int i=0; i<n-1; i++)
            ans*=a[i][i];
        return ans;
    }
};
int main()
{
    int t;
    scanf("%d",&t);
    Matrix A;
    A.build(t);
    printf("%.0f\n",A.det());
    return 0;
}
AC代码:

int main()
{
    char ss[10][40]={"1","16","17745","1064918960","3271331573452806","504061943351319050000000"};
    int T;
    scanf("%d",&T);
    while(T--)
    {
        int a;
        scanf("%d",&a);
        puts(ss[a-1]);
    }
}


UvaLive 6600 Spanning trees in a secure lock pattern 矩阵行列式

标签:矩阵

原文地址:http://blog.csdn.net/ooooooooe/article/details/38899517

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!