NOJ1635看望朋友

#include <string.h>
#include <iostream>
using namespace std;
#define MAX_VERTEX_NUM 101
#define INFINITY 0x11111111
#define TRUE 1
#define FALSE 0

typedef struct{
    //int info;
}VertexType;

typedef struct{
    int val;
    //int info;
}ArcType,ArcMatrix[MAX_VERTEX_NUM][MAX_VERTEX_NUM];

typedef struct{
    int vexnum;
    VertexType vexs[MAX_VERTEX_NUM];
    ArcMatrix arcs;
}MGraph;

typedef int DistancMatrix[MAX_VERTEX_NUM][MAX_VERTEX_NUM];

void ShortestPath_FLOYD(MGraph G, DistancMatrix &D) {
    // 用Floyd算法求有向網G中各對頂點v和w之間的最短路徑P[v][w]及其
    // 帶權長度D[v][w]。若P[v][w][u]爲TRUE,則u是從v到w當前求得最
    // 短路徑上的頂點。
    int v,w,u;
    for (v=0; v<G.vexnum; ++v)        // 各對結點之間初始已知路徑及距離
    {
        for (w=0; w<G.vexnum; ++w) {
            D[v][w] = G.arcs[v][w].val;
            //printf("%d,%d  %d/n",v,w,D[v][w]);
        }//for
        //D[v][v] = 0;    // BUG 修正 劉友繼 2010-05-02 22:04
    }
    for (u=0; u<G.vexnum; ++u)
        for (v=0; v<G.vexnum; ++v)
            for (w=0; w<G.vexnum; ++w)
                if (D[v][u]+D[u][w] < D[v][w]) {  // 從v經u到w的一條路徑更短
                    D[v][w] = D[v][u]+D[u][w];
                }//if
} // ShortestPath_FLOYD

MGraph g;
DistancMatrix d;//各個點間的距離 

int main()
{
    int n,k;
    int a,b;
    cin>>n;
    g.vexnum=n;
       for(int i=0;i<n;i++)
           for(int j=0;j<n;j++)
              cin>>g.arcs[i][j].val;
    ShortestPath_FLOYD(g,d);   
    cin>>k;
    while(k--)
    {   
        cin>>a>>b;
        cout<<d[a-1][b-1]<<endl;
    } system("pause");
    return 0;
}
轉自:http://blog.csdn.net/xyzhk01/article/details/5678447
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章