HDU2807The Shortest Path(SPFA或Floyd)
来源:互联网 发布:走路赚钱的软件 编辑:程序博客网 时间:2024/05/22 01:31
The Shortest Path
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 2648 Accepted Submission(s): 835
Problem Description
There are N cities in the country. Each city is represent by a matrix size of M*M. If city A, B and C satisfy that A*B = C, we say that there is a road from A to C with distance 1 (but that does not means there is a road from C to A).
Now the king of the country wants to ask me some problems, in the format:
Is there is a road from city X to Y?
I have to answer the questions quickly, can you help me?
Now the king of the country wants to ask me some problems, in the format:
Is there is a road from city X to Y?
I have to answer the questions quickly, can you help me?
Input
Each test case contains a single integer N, M, indicating the number of cities in the country and the size of each city. The next following N blocks each block stands for a matrix size of M*M. Then a integer K means the number of questions the king will ask, the following K lines each contains two integers X, Y(1-based).The input is terminated by a set starting with N = M = 0. All integers are in the range [0, 80].
Output
For each test case, you should output one line for each question the king asked, if there is a road from city X to Y? Output the shortest distance from X to Y. If not, output "Sorry".
Sample Input
3 21 12 21 11 12 24 411 33 21 12 21 11 12 24 311 30 0
Sample Output
1Sorry
Source
HDU 2009-4 Programming Contest
<pre name="code" class="cpp">#include<stdio.h>#include<queue>#include<string.h>using namespace std;const int N = 100;const int inf = 9999999;struct matrix{ int m[N][N];};int n,m,map[N][N],dis[N][N];matrix M[N];void init(){ for(int i=0;i<n;i++) for(int j=0;j<n;j++) map[i][j]=0,dis[i][j]=inf;}matrix multiply(const matrix &x,const matrix &y){ matrix temp; memset(temp.m,0,sizeof(temp.m)); for(int i=0; i<m; i++) { for(int j=0; j<m; j++) { if(x.m[i][j]==0) continue; for(int k=0; k<m; k++) { if(y.m[j][k]==0) continue; temp.m[i][k]+=x.m[i][j]*y.m[j][k]; } } } return temp;}bool judge(const matrix &x,const matrix &y){ for(int i=0;i<m;i++) for(int j=0;j<m;j++) if(x.m[i][j]!=y.m[i][j]) return false; return true;}void buildMap(){ init(); if(n==0||m==0) return ; for(int i=0;i<n;i++) for(int j=0;j<n;j++) if(i!=j) { matrix temp=multiply(M[i],M[j]); for(int e=0;e<n;e++) if(e!=i&&e!=j&&judge(temp,M[e])) map[i][e]=1; }}void spfa(){ int inq[N]={0},s; queue<int>q; for(int i=0;i<n;i++) { dis[i][i]=0; q.push(i); while(!q.empty()) { s=q.front(); q.pop(); inq[s]=0; for(int j=0;j<n;j++) if(map[s][j]&&dis[i][j]>dis[i][s]+1) { dis[i][j]=dis[i][s]+1; if(inq[j]==0) inq[j]=1,q.push(j); } } }}int main(){ int Q,a,b; while(scanf("%d%d",&n,&m)>0&&n+m!=0) { for(int i=0;i<n;i++) for(int j=0;j<m;j++) for(int e=0;e<m;e++) scanf("%d",&M[i].m[j][e]); buildMap(); spfa(); scanf("%d",&Q); while(Q--) { scanf("%d%d",&a,&b); a--; b--; if(dis[a][b]!=inf) printf("%d\n",dis[a][b]); else printf("Sorry\n"); } } return 0;}
0 0
- HDU2807The Shortest Path(SPFA或Floyd)
- hdu2807The Shortest Path floyd
- POJ 3631 SHORTEST PATH(FLOYD改装)
- hdu 3631 Shortest Path(floyd插点法)
- hdu 3631 Shortest Path(Floyd)
- hdu3631 Shortest Path (floyd 变形)
- hdu 3631 Shortest Path(Floyd)
- POJ 5636 Shortest Path(floyd)
- hdu 3631 Shortest Path (floyd)
- HDU 5636 Shortest Path(Floyd)
- HDU3631:Shortest Path(Floyd)
- HDU3631 Shortest Path 【Floyd】
- HDU3631:Shortest Path(Floyd)
- HDU5636 -- Shortest Path -- floyd
- HDU2807:The Shortest Path(Floyd)
- 【HDU】3631 Shortest Path 【floyd】
- hdu 3631 Shortest Path (floyd)
- HDU 5636:Shortest Path floyd
- 多线程
- 老老实实学WCF] 第十篇 消息通信模式(下) 双工
- CodeCup 练习题
- GCD常用方法
- Windows MSVC vs MinGW
- HDU2807The Shortest Path(SPFA或Floyd)
- [老老实实学WCF] 第八篇 实例化
- #听音乐学Algorithm#1【算法在计算中的作用】
- 如何在Dev GridControl中添加颜色可变的ProgressBar(GridControl ProgressBar Change Color)
- Linux(CentOs6.6)系统学习笔记系列之二
- [老老实实学WCF] 第七篇 会话
- POJ 1850-Code(组合数学)
- [老老实实学WCF] 第六篇 元数据交换
- 从wsgi规范说起