POJ 2157 How many ways??
How many ways??
This problem will be judged on HDU. Original ID: 2157
64-bit integer IO format: %I64d Java class name: Main
Input
接下来的T行, 每行有三个整数 A, B, k, 表示问你从A 点到 B点恰好经过k个点的方案数 (k < 20), 可以走重复边。如果不存在这样的走法, 则输出0
当n, m都为0的时候输入结束
Output
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
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <climits>
#include <vector>
#include <queue>
#include <cstdlib>
#include <string>
#include <set>
#include <stack>
#define LL long long
#define pii pair<int,int>
#define INF 0x3f3f3f3f
using namespace std;
const int Mod = ;
struct Matrix{
int m[][];
Matrix(){
memset(m,,sizeof(Matrix));
}
};
int n;
Matrix mul(const Matrix &x,const Matrix &y){
Matrix z;
for(int k = ; k <= n; k++)
for(int i = ; i <= n; i++)
for(int j = ; j <= n; j++)
z.m[i][j] = (z.m[i][j] + x.m[i][k]*y.m[k][j])%Mod;
return z;
}
Matrix fastPow(Matrix x,int index){
Matrix y;
for(int i = ; i <= n; i++) y.m[i][i] = ;
while(index){
if(index&) y = mul(x,y);
index >>= ;
x = mul(x,x);
}
return y;
}
int main() {
int m,x,y,t;
while(scanf("%d %d",&n,&m),n||m){
Matrix a,b;
while(m--){
scanf("%d %d",&x,&y);
a.m[x+][y+] = ;
}
scanf("%d",&t);
while(t--){
scanf("%d %d %d",&x,&y,&m);
b = a;
b = fastPow(a,m);
printf("%d\n",b.m[x+][y+]);
}
}
return ;
}
POJ 2157 How many ways??的更多相关文章
- HDU 2157 How many ways?? 【矩阵经典8】
任意门:http://acm.hdu.edu.cn/showproblem.php?pid=2157 How many ways?? Time Limit: 2000/1000 MS (Java/Ot ...
- POJ 2157 Evacuation Plan [最小费用最大流][消圈算法]
---恢复内容开始--- 题意略. 这题在poj直接求最小费用会超时,但是题意也没说要求最优解. 根据线圈定理,如果一个跑完最费用流的残余网络中存在负权环,那么顺着这个负权环跑流量为1那么会得到更小的 ...
- HDU 2157 How many ways?? (邻接矩阵快速幂)
http://acm.hdu.edu.cn/showproblem.php?pid=2157 题意 : 给定一个有向图,问从A点恰好走k步(允许重复经过边)到达B点的方案数mod p的值 从这道题 ...
- HDU 2157 How many ways??(简单线性DP | | 矩阵快速幂)
题目链接 http://acm.hdu.edu.cn/showproblem.php?pid=2157 这道题目很多人的题解都是矩阵快速幂写的,矩阵快速幂倒是麻烦了许多了.先给DP的方法 dp[i][ ...
- HDU 2157 How many ways??:矩阵快速幂【i到j共经过k个节点的方法数】
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2157 题解: 给你一个有向图,n个节点m条边,问你从i到j共经过k个节点的方法数(不算i点). 题解: ...
- Hdu 2157 How many ways??(DP||矩阵乘法)
How many ways?? Time Limit:1000 MS Memory Limit: 32768 K Problem Description 春天到了, HDU校园里开满了花, 姹紫嫣红, ...
- 【HDOJ】2157 How many ways??
矩阵乘法,用DP做各种wa,后来发现原因了. #include <stdio.h> #include <string.h> typedef struct { ][]; } ma ...
- HDU 2157 - How many ways??
给图,图中任意可达的两点间步数为1 问从图中A点走到B点步数为k的有几条路 祭出离散数学图论那章中的 邻接矩阵A. 设S=Ak 则 S[a][b] 为 a到b,步数为k的不同路的条数 剩下的就是矩阵快 ...
- hdu 2157 How many ways?? (可达矩阵)
题意:给你一个有向图,从A 点到 B点恰好经过k个点的方案数 (k < 20), 可以走重复边 思路:利用离散数学中的可达矩阵,可达矩阵的K次幂便是从i到j走K步能到达的方案数 代码: #inc ...
随机推荐
- 在windows下怎样更新vundle?
本文出自Svitter的blog 更新Vundle的时候.不管是输出BundleInstall.还是PluginInstall! 都会调用系统的git,所以必须安装git才干达到目的更新插件. git ...
- Hibernate 自定义方言
自定义一个方言类——Hibernate Dialect 标签: hibernatesqlserverjdbcmysql数据库java 2012-07-04 18:46 2847人阅读 评论(1) 收藏 ...
- Android与JS互相调用以及注意
近期项目中常常使用Html5而Android与JS调用常常会用到,这里记录一下,測试系统5.0以上. 这里先贴一下源代码 Activity: package jwzhangjie.com.webvie ...
- Codeforces Round #349 (Div. 2) C. Reberland Linguistics DP+set
C. Reberland Linguistics First-rate specialists graduate from Berland State Institute of Peace a ...
- [CQOI 2007] 涂色
[题目链接] https://www.lydsy.com/JudgeOnline/problem.php?id=1260 [算法] 区间DP [代码] #include<bits/stdc++. ...
- 【POJ 2976】 Dropping Tests
[题目链接] http://poj.org/problem?id=2976 [算法] 0/1分数规划 [代码] #include <algorithm> #include <bits ...
- Hdu-5992 2016ACM/ICPC亚洲区青岛站 K.Finding Hotels KDtree
题面 题意:二维平面上有很多点,每个点有个权值,现在给你一个点(很多组),权值v,让你找到权值小于等于v的点中离这个点最近的,相同的输出id小的 题解:很裸的KDtree,但是查询的时候有2个小限制, ...
- systemd实践: 依据情况自动重启服务
systemd服务异常自动重启很好用,但有的时候希望某些服务只在特定情况下进行重启,其他时候不要自动重启(比如OOM,需要人工介入). 本文抛砖引玉,旨在能够让读者对systemd的重启机制有一定了解 ...
- Python的filter与map内置函数
简单的记录下这两个函数的功能: list(filter(lambda x : x % 2, range(10))) 上例是返回了0-10之间的所有基数组成的列表.filter()有2个参数,第一个参数 ...
- 2015 多校赛 第一场 1001 (hdu 5288)
Description OO has got a array A of size n ,defined a function f(l,r) represent the number of i (l&l ...