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萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章