PTA ShortestPath[4]

纪念一条北方狼在杭州冻成了狗~
呜呜,居然还下冰雹。然而这并不能阻止我做ds作业的热情hhh
(ง •̀_•́)ง

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

typedef enum {false, true} bool;
#define INFINITY 1000000
#define MaxVertexNum 10  /* maximum number of vertices */
typedef int Vertex;      
typedef int WeightType;
typedef struct GNode *PtrToGNode;
struct GNode{
    int Nv;
    int Ne;
    WeightType G[MaxVertexNum][MaxVertexNum];
};
typedef PtrToGNode MGraph;

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

int main()
{
    int dist[MaxVertexNum], count[MaxVertexNum];
    Vertex S, V;
    int i,j;
    MGraph G1 = (MGraph*)malloc(sizeof(struct GNode));
    for(i=0;i<MaxVertexNum;i++)
        for(j=0;j<MaxVertexNum;j++)
            G1->G[i][j]=INFINITY;
    G1->G[3][0]=40;
    G1->G[3][1]=20;
    G1->G[3][2]=100;
    G1->G[3][7]=70;
    G1->G[0][4]=5;
    G1->G[0][7]=10;
    G1->G[4][7]=5;
    G1->G[6][2]=1; 
    G1->G[7][2]=50;
    G1->G[7][5]=3;
    G1->G[1][7]=30;   
    G1->Nv=8;
    G1->Ne=11;
    scanf("%d", &S);
    ShortestDist( G1, dist, count, S );
    for ( V=0; V<G1->Nv; V++ )
        printf("%d ", dist[V]);
    printf("\n");
    for ( V=0; V<G1->Nv; V++ )
        printf("%d ", count[V]);
    printf("\n");
    return 0;
}
void ShortestDist( MGraph Graph, int dist[], int path[], Vertex S )
{
    int i,j;
    int visit[MaxVertexNum];
    int PathLen[MaxVertexNum];
    for(i=0;i<MaxVertexNum;i++)
    {
        dist[i]=INFINITY;
        PathLen[i]=INFINITY;
        visit[i]=-1;
    }
    dist[S]=0;
    path[S]=-1;
    while(1)
    {
        int min=INFINITY;
        int v=-1;
        for(i=0;i<Graph->Nv;i++)
            if(visit[i]==-1)
                if(dist[i]<min)
                {  
                    min=dist[i];  
                    v=i;  
                }    
        if(v==-1)
            break;  
        visit[v]=1;
        for(j=0;j<Graph->Nv;j++)
        {
            if(Graph->G[v][j]!=INFINITY)
            {
                if(dist[j] > Graph->G[v][j] + dist[v])
                {
                    dist[j]=Graph->G[v][j]+dist[v];
                    PathLen[j]=PathLen[v]+1;
                    path[j]=v;
                }
                else if(dist[j]==Graph->G[v][j] + dist[v])
                    if(PathLen[j]>=PathLen[v]+1)
                    {
                        PathLen[j]=PathLen[v]+1;
                        path[j]=v;
                    } 
            }
        }
    }
    for(i=0;i<Graph->Nv;i++)
        if(dist[i]==INFINITY)
        {
            dist[i]=-1;
            path[i]=-1;
        }
}
### PTA平台第七章图的相关作业题目及解析 #### 题目1:无向图的邻接表表示法 编写一个程序,实现无向图的创建及其遍历操作。给定一组顶点和边的信息,构建该无向图并输出其邻接表。 ```cpp #include <iostream> #include <vector> using namespace std; class Graph { private: int V; // Number of vertices vector<int> *adj; public: Graph(int v); void addEdge(int u, int w); void printGraph(); }; Graph::Graph(int v) : V(v), adj(new vector<int>[V]) {} void Graph::addEdge(int u, int w) { adj[u].push_back(w); // Add w to u’s list. adj[w].push_back(u); // Since the graph is undirected } void Graph::printGraph() { for (int i = 0; i < V; ++i) { cout << "\n Adjacency list of vertex " << i << ": "; for (auto j : adj[i]) cout << "-> " << j; printf("\n"); } } ``` 此代码展示了如何通过邻接表来存储无向图,并打印出各个节点相连的关系[^1]。 #### 题目2:最短路径算法-Dijkstra算法的应用 对于加权连通图G=(V,E),求解从源点s出发到达其他各点v∈V\{s}之间的最短距离d(s,v)。假设所有权重均为正数。 ```cpp #define INF 99999 // Function that implements Dijkstra's single source shortest path algorithm for a graph represented using adjacency matrix representation void dijkstra(int graph[V][V], int src) { int dist[V]; // The output array. dist[i] will hold the shortest distance from src to i bool sptSet[V]; // sptSet[i] will be true if vertex i is included in shortest path tree or shortest distance from src to i is finalized // Initialize all distances as INFINITE and stpSet[] as false for (int i = 0; i < V; i++) dist[i] = INF, sptSet[i] = false; // Distance of source vertex from itself is always 0 dist[src] = 0; // Find shortest path for all vertices for (int count = 0; count < V - 1; count++) { // Pick the minimum distance vertex not yet processed int u = minDistance(dist, sptSet); // Mark the picked vertex as processed sptSet[u] = true; // Update dist value of adjacent vertices of the picked vertex for (int v = 0; v < V; v++) // Update dist[v] only if it is not in sptSet, // there is an edge from u to v, and total weight of path from src to v through u is smaller than current value of dist[v] if (!sptSet[v] && graph[u][v] && dist[u] != INT_MAX && dist[u]+graph[u][v] < dist[v]) dist[v] = dist[u] + graph[u][v]; } // Print the constructed distance array printSolution(dist, V); } ``` 上述实现了Dijkstra单源最短路径算法,适用于有权重且非负的简单网络结构中计算两点间最优路线问题[^2]。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值