NOJ1635看望朋友

来源:互联网 发布:越狱软件大全 编辑:程序博客网 时间:2024/05/22 14:59
#include <string.h>#include <iostream>using namespace std;#define MAX_VERTEX_NUM 101#define INFINITY 0x11111111#define TRUE 1#define FALSE 0typedef 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_FLOYDMGraph 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
原创粉丝点击