hdu2544最短路

来源:互联网 发布:nginx编写第三方模块 编辑:程序博客网 时间:2024/05/17 22:11

 题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2544

 

 代码: Dijkstra算法

 

//N<=100,M<=10000    1<=A,B<=N,1<=C<=1000

#define MAX 105
#define INF 0x11111111
#define TRUE 1
#define FALSE 0

typedef struct
{

    //int info;
}VertexType;

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

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

typedef
int
ShortPathTable[MAX];


void
ShortestPath_DIJ(MGraph &G,int v0, ShortPathTable &D)
{

    // 用Dijkstra算法求有向网G的v0顶点到其余顶点v的最短路径P[v]
    // 及其带权长度D[v]。
    // 若P[v][w]为TRUE,则w是从v0到v当前求得最短路径上的顶点。
    // final[v]为TRUE当且仅当v∈S,即已经求得从v0到v的最短路径。
    int i=0, v,w,min;
    bool
final[MAX];
    for
(
v=0; v<G.vexnum; ++v)
    {

        final[v] = FALSE;
        D[v] = G.arcs[v0][v].val;
    }

    D[v0] = 0; final[v0] = TRUE;        // 初始化,v0顶点属于S集
    //--- 开始主循环,每次求得v0到某个v顶点的最短路径,并加v到S集 ---
    for (i=1; i<G.vexnum; ++i)           // 其余G.vexnum-1个顶点
    {
        min = INF;                  // 当前所知离v0顶点的最近距离
        for (w=0; w<G.vexnum; ++w)
            if
(!
final[w])                             // w顶点在V-S中
                if (D[w]<min) { v = w; min = D[w]; } // w顶点离v0顶点更近
        final[v] = TRUE;                       // 离v0顶点最近的v加入S集
        for (w=0; w<G.vexnum; ++w)             // 更新当前最短路径及距离
            if (!final[w] && (min+G.arcs[v][w].val<D[w])) {
                // 修改D[w]和P[w], w∈V-S
                D[w] = min + G.arcs[v][w].val;
            }
//if
    }//for
} // ShortestPath_DIJ



#include <string.h>
#include <iostream>
using namespace std;

MGraph g;
ShortPathTable d;

int
main()
{

    long
m,n;
    long
a,b,c,v0=0;
    int
i,j,k;
    while
(
cin>>n>>m)
    {

        if
(
m==0&&n==0)break;
        g.vexnum = n;
        memset(g.arcs,INF,sizeof(g.arcs));
        memset(d,INF,sizeof(d));
        for
(
i=0;i<m;i++)
        {

            cin>>a>>b>>c;
            a--;b--;
            g.arcs[a][b].val = c;
            g.arcs[b][a].val = c;
        }
          
        ShortestPath_DIJ(g,v0,d);
        cout<<d[n-1]<<endl;
    }

    return
0;
}

 

//代码二:floyd算法 

//N<=100,M<=10000    1<=A,B<=N,1<=C<=1000
// Floyd.cpp : Defines the entry point for the console application.
//
#define MAX 105
#define INF 0x11111111
#define TRUE 1
#define FALSE 0

typedef struct
{
    //int info;
}VertexType;

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

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

typedef
int DistancMatrix[MAX][MAX];

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;
        }
//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


#include <string.h>
#include <iostream>
using namespace std;

MGraph g;
DistancMatrix d;//各个点间的距离

int
main()
{

    long
m,n;
    long
a,b,c,v0=0;
    int
i,j,k;
    while
(cin>>n>>m)
    {

        if
(m==0&&n==0)break;
        g.vexnum = n;
        memset(g.arcs,INF,sizeof(g.arcs));
        memset(d,INF,sizeof(d));
        for
(
i=0;i<m;i++)
        {

            cin>>a>>b>>c;
            a--;b--;
            g.arcs[a][b].val = c;
            g.arcs[b][a].val = c;
        }
          
        ShortestPath_FLOYD
(g,d);
        cout<<d[0][n-1]<<endl;
    }

    return
0;
}

原创粉丝点击