HDU2807

来源:互联网 发布:js 增加option 编辑:程序博客网 时间:2024/06/07 08:27

1.题目描述:

The Shortest Path

Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 3314    Accepted Submission(s): 1097


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?
 

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
 

Recommend
lcy
2.题意概述:

全国有N个城市。每个城市由M * M的矩阵大小表示。如果城市A,B和C满足A * B = C,我们说有一条从A到C的道路距离为1(但这并不意味着有一条从C到A的道路)。

问某点i到j是否有最短路,有则输出最度阿奴,无则输出0

3.解题思路:

直接码一发矩阵乘法再判断就行,这题注意的是a b c矩阵两两都不相同

4.AC代码:

#include <bits/stdc++.h>#define INF 0x3f3f3f3f#define maxn 100100#define N 88#define eps 1e-6#define pi acos(-1.0)#define e exp(1.0)using namespace std;const int mod = 1e9 + 7;typedef long long ll;typedef unsigned long long ull;struct node{int to, val;node(int a, int b) { to = a; val = b; }};struct matrix{int mat[N][N];int sz;friend matrix operator* (matrix a, matrix b){matrix ans;ans.sz = a.sz;memset(ans.mat, 0, sizeof(ans.mat));for (int i = 0; i < ans.sz; i++)for (int j = 0; j < ans.sz; j++)for (int k = 0; k < ans.sz; k++)ans.mat[i][j] += a.mat[i][k] * b.mat[k][j];return ans;}friend bool operator== (matrix a, matrix b){int flag = 1;for (int i = 0; i < a.sz && flag; i++)for (int j = 0; j < a.sz; j++)if (a.mat[i][j] != b.mat[i][j]){flag = 0;break;}return flag;}} M[N];vector<node> mp[N];int dis[N];bool vis[N];void spfa(int sta, int n){memset(vis, 0, sizeof(vis));fill(dis, dis + n + 1, INF);deque<int> q;vis[sta] = 1;dis[sta] = 0;q.push_back(sta);while (!q.empty()){int u = q.front();q.pop_front();vis[u] = 0;int sz = mp[u].size();for (int i = 0; i < sz; i++){int v = mp[u][i].to;int w = mp[u][i].val;if (dis[v] > dis[u] + w){dis[v] = dis[u] + w;if (!vis[v]){vis[v] = 1;if (!q.empty() && dis[v] <= dis[q.front()])q.push_front(v);elseq.push_back(v);}}}}}int main(){#ifndef ONLINE_JUDGEfreopen("in.txt", "r", stdin);freopen("out.txt", "w", stdout);long _begin_time = clock();#endifint n, m;while (~scanf("%d%d", &n, &m), n + m){for (int i = 0; i < n; i++){mp[i].clear();M[i].sz = m;for (int a = 0; a < m; a++)for (int b = 0; b < m; b++)scanf("%d", &M[i].mat[a][b]);}for (int i = 0; i < n; i++)for (int j = 0; j < n; j++){matrix tmp = M[i] * M[j];for (int k = 0; k < n; k++)if (k != i && k != j && M[k] == tmp)mp[i].push_back(node(k, 1));}int k;scanf("%d", &k);while (k--){int s, t;scanf("%d%d", &s, &t);s--;t--;spfa(s, n);if (dis[t] == INF)puts("Sorry");elseprintf("%d\n", dis[t]);}}#ifndef ONLINE_JUDGElong _end_time = clock();printf("time = %ld ms.", _end_time - _begin_time);#endifreturn 0;}

0 0