(图6-21)Shortest Path [3]--C语言

Write a program to not only find the weighted shortest distances, but also count the number of different minimum paths from any vertex to a given source vertex in a digraph. It is guaranteed that all the weights are positive.(编写一个程序,不仅可以找到加权的最短距离,还可以计算有向图中从任何顶点到给定源顶点的不同最小路径的数量。保证所有权重都是正的。

函数接口定义:

void ShortestDist( MGraph Graph, int dist[], int count[], Vertex S );

where MGraph is defined as the following:

typedef struct GNode *PtrToGNode;
struct GNode{
int Nv;
int Ne;
WeightType G[MaxVertexNum][MaxVertexNum];
};
typedef PtrToGNode MGraph;

The shortest distance from V to the source S is supposed to be stored in dist[V]. If V cannot be reached from S, store -1 instead. The number of different minimum paths from V to the source S is supposed to be stored in count[V] and count[S]=1.(从V到源S的最短距离应该存储在dist[V]中。如果无法从S到达V,则存储-1。从V到源S的不同最小路径的数量被假定存储在count[V]和count[S]=1中。

裁判测试程序样例:

#include <stdio.h>
#include <stdlib.h>

typedef enum {false, true} bool;
#define INFINITY 1000000
#define MaxVertexNum 10  /* maximum number of vertices */
typedef int Vertex;      /* vertices are numbered from 0 to MaxVertexNum-1 */
typedef int WeightType;

typedef struct GNode *PtrToGNode;
struct GNode{
    int Nv;
    int Ne;
    WeightType G[MaxVertexNum][MaxVertexNum];
};
typedef PtrToGNode MGraph;

MGraph ReadG(); /* details omitted */

void ShortestDist( MGraph Graph, int dist[], int count[], Vertex S );

int main()
{
    int dist[MaxVertexNum], count[MaxVertexNum];
    Vertex S, V;
    MGraph G = ReadG();

    scanf("%d", &S);
    ShortestDist( G, dist, count, S );

    for ( V=0; V<G->Nv; V++ )
        printf("%d ", dist[V]);
    printf("\n");
    for ( V=0; V<G->Nv; V++ )
        printf("%d ", count[V]);
    printf("\n");

    return 0;
}

/* Your function will be put here */

输入样例:

在这里插入图片描述

8 11
0 4 5
0 7 10
1 7 30
3 0 40
3 1 20
3 2 100
3 7 70
4 7 5
6 2 1
7 5 3
7 2 50
3

输出样例:

40 20 100 0 45 53 -1 50 
1 1 4 1 1 3 0 3 

代码:

int vis[MaxVertexNum];
void ShortestDist( MGraph Graph, int dist[], int count[], Vertex S )
{
    for(int i=0;i<Graph->Nv;i++)//初始化
    {
        vis[i]=0;
        dist[i]=Graph->G[S][i];
        if(dist[i]<INFINITY) count[i]=1;//是S的邻接点
        else count[i]=0;//不是
    }
    //初始化S点
    dist[S]=0;
    vis[S]=1;
    count[S]=1;
    while(1)
    {
        int v=-1;
        int min=INFINITY;
        for(int j=0;j<Graph->Nv;j++)//找v点
        {
            if(vis[j]==0&&dist[j]<min)
            {
                min=dist[j];
                v=j;
            }
        }
        if(v==-1) break;
        vis[v]=1;
        for(int w=0;w<Graph->Nv;w++)//访问v点的邻接点
        {
            //路径权值一致,说明w点原有的几条路径权值等同于经过v点到w点的几条路径权值
            if(dist[v]+Graph->G[v][w]==dist[w])
                count[w]+=count[v];//w点的路径要在原有基础上再加上v点的路径条数
            //没被访问过且路径权值比原来的小
            if((dist[v]+Graph->G[v][w]<dist[w])&&vis[w]==0)
            {
                dist[w]=dist[v]+Graph->G[v][w];
                count[w]=count[v];//有几条路径可以到达v点就有几条路径可以到达w点
            }
        }
    }
    for(int i=0;i<Graph->Nv;i++)//不能到达S点
    {
        if(dist[i]==INFINITY) dist[i]=-1;

    }
}
Bellman-Ford算法是一种用于求解带权重的有向中最短路径问题的动态规划算法。以下是使用C语言实现的一个简单版本: ```c #include <stdio.h> #include <limits.h> #define V 4 // 的顶点数 #define INF INT_MAX // 定义最大边权值 void bellman_ford(int graph[V][V], int src) { int dist[V]; // 初始化所有节点距离为无穷大,除了源点 for (int i = 0; i < V; i++) { dist[i] = INF; } dist[src] = 0; // 源点的距离设为0 // 进行V-1次迭代,因为最短路径最多V-1步 for (int i = 0; i < V - 1; i++) { for (int u = 0; u < V; u++) { // 对于每个节点u,检查是否有更短路径 for (int v = 0; v < V; v++) { if (graph[u][v] != INF && dist[u] + graph[u][v] < dist[v]) { dist[v] = dist[u] + graph[u][v]; } } } } // 再做一次检查,防止负权回路的存在 bool negative_cycle = false; for (int u = 0; u < V; u++) { if (graph[u][src] != INF && dist[u] + graph[u][src] < dist[src]) { negative_cycle = true; // 如果找到一条更短路径,则存在负权回路 break; } } if (!negative_cycle) printf("Shortest path from %d to all vertices are \n", src); else printf("Graph contains a negative weight cycle.\n"); // 打印结果 for (int i = 0; i < V; i++) printf("Distance from source to vertex %d is %d\n", i, dist[i]); } int main() { int graph[V][V] = {{0, -1, 4, INF}, {-1, 0, INF, 2}, {4, INF, 0, -3}, {INF, 2, -3, 0}}; bellman_ford(graph, 0); // 从顶点0开始寻找最短路径 return 0; } ``` 这里假设已经存储在一个二维数组`graph`中,`graph[u][v]`表示从顶点`u`到顶点`v`的边权。注意这个代码只能处理负权边不超过V-1倍的情况,若存在负权回路则会检测到。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值