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. 2017 Benelux Algorithm Programming Contest (BAPC 17) Solution

    A - Amsterdam Distance 题意:极坐标系,给出两个点,求最短距离 思路:只有两种方式,取min  第一种,先走到0点,再走到终点 第二种,走到同一半径,再走过去 #include ...

  2. 爬取乌云上所有人民币和乌云符号的漏洞(python脚本)

    import httplib from HTMLParser import HTMLParser import urlparse import urllib from bs4 import Beaut ...

  3. SQL: 为列取有意义的名称

    1.用法 2.在where字句中使用别名要注意,(别名是select之后才会生效)

  4. 大数据领域两大最主流集群管理工具Ambari和Cloudera Manger

    不多说,直接上干货! 目前啊,都知道,大数据集群管理方式分为手工方式(Apache hadoop)和工具方式(Ambari + hdp 和Cloudera Manger + CDH).  手工部署呢, ...

  5. zabbix-2.4.7环境部署与初始化安装

    一.zabbix简介: zabbix的特点: - 安装与配置简单,学习成本低 - 支持多语言(包括中文) - 免费开源 - 自动发现服务器与网络设备 - 分布式监视以及WEB集中管理功能 - 可以无a ...

  6. POJ 2337 Catenyms

    http://poj.org/problem?id=2337 题意: 判断给出的单词能否首尾相连,输出字典序最小的欧拉路径. 思路: 因为要按字典序大小输出路径,所以先将字符串排序,这样加边的时候就会 ...

  7. ERROR: cannot launch node of type [robot_pose_publisher/robot_pose_publisher]: robot_pose_publisher

    sudo apt-get install ros-indigo-robot-pose-publisher

  8. callee 与 caller

    arguments.callee 在函数内部指向函数本身 1.函数调用 function sum (num){ if(num <= 1){ return 1; }else{ return num ...

  9. SSM(Spring+SpringMVC+Mybatis)+Mysql 框架整合搭建流程以及其间注意事项

    复习SSM框架,太久没用自己手动撘一个,发现自己还是有很多地方忘记了和没注意的事项... 首先,直接给出总流程: 零.引jar包 1.引包(或者写maven.pom) 一.数据库部分 设计数据库各表结 ...

  10. BeginInit与EndInit的实践总结

    在项目中,遇到这种情况,总结随便如下: 初始化时:添加操作,BeginInit{flag=true}  警情是一条条加入的,全部都加入后,图表再一次性生成   EndInit{flag=false} ...