Write a program to find the weighted shortest distances from any vertex to a given source vertex in a digraph. It is guaranteed that all the weights are positive.

Format of functions:

void ShortestDist( MGraph Graph, int dist[], 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.

Sample program of judge:

#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[], Vertex S ); int main()
{
int dist[MaxVertexNum];
Vertex S, V;
MGraph G = ReadG(); scanf("%d", &S);
ShortestDist( G, dist, S ); for ( V=0; V<G->Nv; V++ )
printf("%d ", dist[V]); return 0;
} /* Your function will be put here */

Sample Input (for the graph shown in the figure):

7 9
0 1 1
0 5 1
0 6 1
5 3 1
2 1 2
2 6 3
6 4 4
4 5 5
6 5 12
2

Sample Output:

-1 2 0 13 7 12 3
不能抵达的为INFINITY,用过dijkstra算法,最后记得把INFINITY变成-1,dist[S]变成0
代码:
void ShortestDist( MGraph Graph, int dist[], Vertex S )
{
for(int i = ;i < Graph -> Nv;i ++)
dist[i] = Graph -> G[S][i];
int vis[MaxVertexNum] = {};
vis[S] = ;
for(int i = ;i < Graph -> Nv;i ++)
{
int min = INFINITY;
int t = INFINITY;
for(int j = ;j < Graph -> Nv;j ++)
if(!vis[j]&&dist[j] < min)min = dist[j],t = j;
if(min == INFINITY)continue;
vis[t] = ;
for(int j = ;j < Graph -> Nv;j ++)
{
if(!vis[j])
{
if(dist[j] > Graph -> G[t][j] + min)dist[j] = Graph -> G[t][j] + min;
}
}
}
for(int i = ;i < Graph -> Nv;i ++)
if(i == S)dist[i] = ;
else if(dist[i] == INFINITY)dist [i] = -;
}

6-17 Shortest Path [2](25 分)的更多相关文章

  1. 1126 Eulerian Path (25 分)

    1126 Eulerian Path (25 分) In graph theory, an Eulerian path is a path in a graph which visits every ...

  2. 【刷题-PAT】A1126 Eulerian Path (25 分)

    1126 Eulerian Path (25 分) In graph theory, an Eulerian path is a path in a graph which visits every ...

  3. PAT A1126 Eulerian Path (25 分)——连通图,入度

    In graph theory, an Eulerian path is a path in a graph which visits every edge exactly once. Similar ...

  4. 1003 Emergency (25 分)

    1003 Emergency (25 分) As an emergency rescue team leader of a city, you are given a special map of y ...

  5. HDU 4725 The Shortest Path in Nya Graph (最短路 )

    This is a very easy problem, your task is just calculate el camino mas corto en un grafico, and just ...

  6. 1003 Emergency (25分) 求最短路径的数量

    1003 Emergency (25分)   As an emergency rescue team leader of a city, you are given a special map of ...

  7. 1122 Hamiltonian Cycle (25 分)

    1122 Hamiltonian Cycle (25 分) The "Hamilton cycle problem" is to find a simple cycle that ...

  8. HDU - 4725 The Shortest Path in Nya Graph 【拆点 + dijkstra】

    This is a very easy problem, your task is just calculate el camino mas corto en un grafico, and just ...

  9. Shortest Path(hdu5636)

    Shortest Path  Accepts: 40  Submissions: 610  Time Limit: 4000/2000 MS (Java/Others)  Memory Limit: ...

  10. PTA 旅游规划(25 分)

    7-10 旅游规划(25 分) 有了一张自驾旅游路线图,你会知道城市间的高速公路长度.以及该公路要收取的过路费.现在需要你写一个程序,帮助前来咨询的游客找一条出发地和目的地之间的最短路径.如果有若干条 ...

随机推荐

  1. VC中加载LIB库文件的三种方法

    VC中加载LIB库文件的三种方法 在VC中加载LIB文件的三种方法如下: 方法1:LIB文件直接加入到工程文件列表中   在VC中打开File View一页,选中工程名,单击鼠标右键,然后选中&quo ...

  2. cocos-lua基础学习(八)Layer类学习笔记

    创建 local layer = cc.Layer:create() local layer1 = cc.LayerColor:create(cc.c4b(192, 0, 0, 255), s.wid ...

  3. 2.JVM运行机制 -- JVM序列

    上一次写了1.初步认识JVM -- JVM序列,今天接着记录写JVM的运行机制. 一.JVM启动流程 Java平台包括JVM以及Java语言,其中JVM也是运行在操作系统中的一个应用程序进程,那么也应 ...

  4. 牛客国庆集训派对Day1 Solution

    A    Tobaku Mokushiroku Kaiji 水. #include <bits/stdc++.h> using namespace std; ], b[]; void Ru ...

  5. hdu1199 线段树

    这题说的是给了 n 个操作. 每个操作会把 [a,b] 之间的球 涂为黑色或者 白色, 然后最后问 最长的连续的白色的 球有多少个,初始的时候全是黑的. 我们将所有的点离散化, 记得离散 a-1, b ...

  6. Java backup

    待补充 ........ 0:常用头文件(待补充) import java.util.Arrays; import java.util.HashSet; import java.util.TreeSe ...

  7. HDU4112

    对于n*m*k的方块,用手掰成1**1的那么搜需要的步骤是固定的,为n*m*k-,如果用刀切,因为可以把多块叠在一起切,所以对于长度为n的,将他切成0,所需要的步骤数位k 对于n*m*k的方块,用手掰 ...

  8. Vue学习笔记之计算属性和侦听器

    0x00 计算属性 模板内的表达式非常便利,但是设计它们的初衷是用于简单运算的.在模板中放入太多的逻辑会让模板过重且难以维护.例如: <div> {{ message.split(''). ...

  9. Sublime Text 3 配置Python3.x

    Sublime Text 3 配置Python3.x 一.Package Control 安装: 1,通过快捷键 ctrl+` 或者 View > Show Console 打开控制台,然后粘贴 ...

  10. linux及安全第五周总结——20135227黄晓妍

    (注意:本文总结备份中有较多我手写笔记的图片,其中重要的部分打出来了.本文对分析system_call对应的汇编代码的工作过程,系统调用处理过程”的理解,以及流程图都写在实验部分.) 实验部分 使用g ...