6-17 Shortest Path [2](25 分)
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 分)的更多相关文章
- 1126 Eulerian Path (25 分)
1126 Eulerian Path (25 分) In graph theory, an Eulerian path is a path in a graph which visits every ...
- 【刷题-PAT】A1126 Eulerian Path (25 分)
1126 Eulerian Path (25 分) In graph theory, an Eulerian path is a path in a graph which visits every ...
- PAT A1126 Eulerian Path (25 分)——连通图,入度
In graph theory, an Eulerian path is a path in a graph which visits every edge exactly once. Similar ...
- 1003 Emergency (25 分)
1003 Emergency (25 分) As an emergency rescue team leader of a city, you are given a special map of y ...
- 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 ...
- 1003 Emergency (25分) 求最短路径的数量
1003 Emergency (25分) As an emergency rescue team leader of a city, you are given a special map of ...
- 1122 Hamiltonian Cycle (25 分)
1122 Hamiltonian Cycle (25 分) The "Hamilton cycle problem" is to find a simple cycle that ...
- 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 ...
- Shortest Path(hdu5636)
Shortest Path Accepts: 40 Submissions: 610 Time Limit: 4000/2000 MS (Java/Others) Memory Limit: ...
- PTA 旅游规划(25 分)
7-10 旅游规划(25 分) 有了一张自驾旅游路线图,你会知道城市间的高速公路长度.以及该公路要收取的过路费.现在需要你写一个程序,帮助前来咨询的游客找一条出发地和目的地之间的最短路径.如果有若干条 ...
随机推荐
- 3.5 Templates -- Binding Element Attributes(绑定元素属性)
一.概述 除了正常的文本,你可能还需要你的模板中包含的HTML元素的属性绑定到controller. 1. 例如,设想controller有一个属性包含一个图片的URL: <div id=&qu ...
- 自定义 Repository 方法
为某一个 Repository 上添加自定义方法 步骤: 定义一个接口: 声明要添加的, 并自实现的方法 提供该接口的实现类: 类名需在要声明的 Repository 后添加 Impl, 并实现方法 ...
- Scrapy:学习笔记(2)——Scrapy项目
Scrapy:学习笔记(2)——Scrapy项目 1.创建项目 创建一个Scrapy项目,并将其命名为“demo” scrapy startproject demo cd demo 稍等片刻后,Scr ...
- uva11324 有向图的强连通分量+记忆化dp
给一张有向图G, 求一个结点数最大的结点集,使得该结点集中任意两个结点u和v满足,要么u可以到达v, 要么v可以到达u(u和v相互可达也可以). 因为整张图可能存在环路,所以不好使用dp直接做,先采用 ...
- Java Exception 和Error有什么区别?
① Exception 和Error 都是继承了Throwable类,在Java中只有Throwable类型的实例才可以被抛出或者捕获,它是异常处理机制的基本类型. ② Exception和Error ...
- 微信小程序中公用内容
微信小程序中各个页面调用公用的js 在util.js文件中 // 跳转哪里 function go(where) { wx.reLaunch({ url: where, }) } // 将方法暴露出去 ...
- ENC28J60
8 KB发送接收数据包双端口 SRAM
- 扩容swap交换分区空间
安装linux系统时会指定Swap分区大小,一般是内存的两倍,但在有些场景下可能预先设置的Swap分区空间不足,这个时候需要增加其大小 官方建议在RAM是2到4.5G时,swap是RAM的2倍:如果R ...
- 【javascript】数据结构-集合
<!DOCTYPE html> <html> <head> <title>集合</title> <meta charset=" ...
- POJ 1047 Round and Round We Go
https://vjudge.net/problem/POJ-1047 题意: 给一个整数,它的长度为n,从1开始一直到n和该整数相乘,判断每次结果是否和原来的整数是循环的. 思路: 大整数的乘法. ...