hdu2544最短路

http://acm.hdu.edu.cn/showproblem.php?pid=2544

代码一: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;
}
代码二: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;
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值