zoukankan      html  css  js  c++  java
  • POJ 2157 How many ways??

    How many ways??

    Time Limit: 1000ms
    Memory Limit: 32768KB
    This problem will be judged on HDU. Original ID: 2157
    64-bit integer IO format: %I64d      Java class name: Main
     
    春天到了, HDU校园里开满了花, 姹紫嫣红, 非常美丽. 葱头是个爱花的人, 看着校花校草竞相开放, 漫步校园, 心情也变得舒畅. 为了多看看这迷人的校园, 葱头决定, 每次上课都走不同的路线去教室, 但是由于时间问题, 每次只能经过k个地方, 比方说, 这次葱头决定经过2个地方, 那他可以先去问鼎广场看看喷泉, 再去教室, 也可以先到体育场跑几圈, 再到教室. 他非常想知道, 从A 点恰好经过k个点到达B点的方案数, 当然这个数有可能非常大, 所以你只要输出它模上1000的余数就可以了. 你能帮帮他么?? 你可决定了葱头一天能看多少校花哦
     

    Input

    输入数据有多组, 每组的第一行是2个整数 n, m(0 < n <= 20, m <= 100) 表示校园内共有n个点, 为了方便起见, 点从0到n-1编号,接着有m行, 每行有两个整数 s, t (0<=s,t<n) 表示从s点能到t点, 注意图是有向的.接着的一行是两个整数T,表示有T组询问(1<=T<=100),
    接下来的T行, 每行有三个整数 A, B, k, 表示问你从A 点到 B点恰好经过k个点的方案数 (k < 20), 可以走重复边。如果不存在这样的走法, 则输出0
    当n, m都为0的时候输入结束
     

    Output

    计算每次询问的方案数, 由于走法很多, 输出其对1000取模的结果
     

    Sample Input

    4 4
    0 1
    0 2
    1 3
    2 3
    2
    0 3 2
    0 3 3
    3 6
    0 1
    1 0
    0 2
    2 0
    1 2
    2 1
    2
    1 2 1
    0 1 3
    0 0

    Sample Output

    2
    0
    1
    3

    Source

     
    解题:矩阵大法好。。。。。。。
     
     1 #include <iostream>
     2 #include <cstdio>
     3 #include <cstring>
     4 #include <cmath>
     5 #include <algorithm>
     6 #include <climits>
     7 #include <vector>
     8 #include <queue>
     9 #include <cstdlib>
    10 #include <string>
    11 #include <set>
    12 #include <stack>
    13 #define LL long long
    14 #define pii pair<int,int>
    15 #define INF 0x3f3f3f3f
    16 using namespace std;
    17 const int Mod = 1000;
    18 struct Matrix{
    19     int m[30][30];
    20     Matrix(){
    21         memset(m,0,sizeof(Matrix));
    22     }
    23 };
    24 int n;
    25 Matrix mul(const Matrix &x,const Matrix &y){
    26     Matrix z;
    27     for(int k = 1; k <= n; k++)
    28         for(int i = 1; i <= n; i++)
    29             for(int j = 1; j <= n; j++)
    30                 z.m[i][j] = (z.m[i][j] + x.m[i][k]*y.m[k][j])%Mod;
    31     return z;
    32 }
    33 Matrix fastPow(Matrix x,int index){
    34     Matrix y;
    35     for(int i = 0; i <= n; i++) y.m[i][i] = 1;
    36     while(index){
    37         if(index&1) y = mul(x,y);
    38         index >>= 1;
    39         x = mul(x,x);
    40     }
    41     return y;
    42 }
    43 int main() {
    44     int m,x,y,t;
    45     while(scanf("%d %d",&n,&m),n||m){
    46         Matrix a,b;
    47         while(m--){
    48             scanf("%d %d",&x,&y);
    49             a.m[x+1][y+1] = 1;
    50         }
    51         scanf("%d",&t);
    52         while(t--){
    53             scanf("%d %d %d",&x,&y,&m);
    54             b = a;
    55             b = fastPow(a,m);
    56             printf("%d
    ",b.m[x+1][y+1]);
    57         }
    58     }
    59     return 0;
    60 }
    View Code
  • 相关阅读:
    ubuntu python3 安装pip
    Windows远程桌面连接ubuntu 16
    Python 高级编程——单例模式
    学习资料推荐
    经典 测试开发面试题 (随时更新)
    mac上生成go文件失败报错,gRpc-- protoc-gen-go: program not found or is not executable
    小白从零开始学编程(五)--python数据类型--字符串
    小白从零开始学编程(三)--python基本概念
    小白从零开始学编程(二)--python虚拟环境和编辑器
    小白从零开始学编程--python安装与环境搭建
  • 原文地址:https://www.cnblogs.com/crackpotisback/p/3994176.html
Copyright © 2011-2022 走看看