HDU 2807解题报告

The Shortest Path

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


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 2 1 1 2 2 1 1 1 1 2 2 4 4 1 1 3 3 2 1 1 2 2 1 1 1 1 2 2 4 3 1 1 3 0 0
 

Sample Output
1 Sorry
 

Source
 

Recommend
lcy   |   We have carefully selected several similar problems for you:  2224 1217 2803 2802 2544 

            这道题是比较单纯的一道题,暴力可解。不过写不好容易超时。

        题意是每个城市用一个矩阵来表示,如果A*B=C,这说明A和C之间存在一条有向边。然后建图,用floyd算法求出所有点之间的最短路径。给定两个点x,y,判断这两点之间是否存在道路,如果存在输出这两点之间的最短路径。判断两点之间是否存在道路,这个属于图的传递闭包,是用floyd算法求出的。但是我们用floyd算法求解最短路径时,如果算出x和y之间的距离为INF(无穷大),那么也就可以说明两点之间不存在道路,不可到达。也就不需要求传递闭包。

       在暴力求解的过程中需要注意的是在进行A*B=C的判断时要保证A,B,C互不相同。而且计算出A*B以后,要逐个比较,看其是否与A*B所得矩阵相等。不能一旦找到以后就break,因为有可能有多个矩阵都等于A*B。这几点要注意。

       参考代码:

       

#include<cstdio>
#include<iostream>
#include<cmath>
#include<cstring>
#include<algorithm>
#include<string>
#include<vector>
#include<map>
#include<set>
#include<stack>
#include<queue>
#include<ctime>
#include<cstdlib>
#include<iomanip>
#include<utility>
#define pb push_back
#define mp make_pair
#define CLR(x) memset(x,0,sizeof(x))
#define _CLR(x) memset(x,-1,sizeof(x))
#define REP(i,n) for(int i=0;i<n;i++)
#define Debug(x) cout<<#x<<"="<<x<<" "<<endl
#define REP(i,l,r) for(int i=l;i<=r;i++)
#define rep(i,l,r) for(int i=l;i<r;i++)
#define RREP(i,l,r) for(int i=l;i>=r;i--)
#define rrep(i,l,r) for(int i=1;i>r;i--)
#define read(x) scanf("%d",&x)
#define put(x) printf("%d\n",x)
#define ll long long
#define lson l,m,rt<<1
#define rson m+1,r,rt<<11
using namespace std;
const int INF=(1<<30);
int n,m,t;
int g[100][100];

struct mat
{
    int d[100][100];
} A[100];

int main()
{
    while(~scanf("%d%d",&n,&m)&&n+m)
    {
        REP(i,1,n)
            rep(j,0,m)
                rep(k,0,m)
                    scanf("%d",&A[i].d[j][k]);
        REP(i,1,n)   //初始化邻接矩阵,对于同一个点时,距离为0(注意本题没有自环的情形,因为A,B,C互不相等,所以同一个点到同一个点距离初始化为0)
        {
            REP(j,i,n)
            {
                if(i==j) g[i][j]=0;
                else  g[i][j]=g[j][i]=INF;
            }
        }
        REP(i,1,n)
        {
            REP(j,1,n)
            {
                if(i!=j)
                {
                    mat m1;   
                    rep(l1,0,m)  //矩阵乘法求解A*B
                    {
                        rep(l2,0,m)
                        {
                            m1.d[l1][l2]=0;
                            rep(l3,0,m)
                            {
                                m1.d[l1][l2]+=A[i].d[l1][l3]*A[j].d[l3][l2];
                            }
                        }
                    }
                    REP(k,1,n)   //进行矩阵的比较
                    {
                        if(i!=k&&j!=k)
                        {
                            bool flag=1;
                            rep(l1,0,m)
                            {
                                rep(l2,0,m)
                                {
                                    if(m1.d[l1][l2]!=A[k].d[l1][l2])
                                    {
                                        flag=0;
                                        break;
                                    }
                                }
                                if(!flag)
                                    break;
                            }
                            if(flag)
                                g[i][k]=1;
                        }
                    }
                }
            }
        }
        REP(k,1,n)       //floyd算法求最短路径
            REP(i,1,n)
            {
                if(g[i][k]==INF)
                    continue;
                REP(j,1,n)
                    if(g[i][k]<INF&&g[k][j]<INF)
                        g[i][j]=min(g[i][j],g[i][k]+g[k][j]);
            }
        scanf("%d",&t);
        while(t--)
        {
            int u,v;
            scanf("%d%d",&u,&v);
            if(g[u][v]<INF)
                printf("%d\n",g[u][v]);
            else
                printf("Sorry\n");
        }
    }
}



发布了69 篇原创文章 · 获赞 4 · 访问量 3万+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章