6-1 Shortest Path [1]

Write a program to find the unweighted shortest distances from any vertex to a given source vertex in a digraph.

Format of functions:

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

where LGraph is defined as the following:

typedef struct AdjVNode *PtrToAdjVNode; 
struct AdjVNode{
    Vertex AdjV;
    PtrToAdjVNode Next;
};

typedef struct Vnode{
    PtrToAdjVNode FirstEdge;
} AdjList[MaxVertexNum];

typedef struct GNode *PtrToGNode;
struct GNode{  
    int Nv;
    int Ne;
    AdjList G;
};
typedef PtrToGNode LGraph;

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 MaxVertexNum 10  /* maximum number of vertices */
typedef int Vertex;      /* vertices are numbered from 0 to MaxVertexNum-1 */

typedef struct AdjVNode *PtrToAdjVNode; 
struct AdjVNode{
    Vertex AdjV;
    PtrToAdjVNode Next;
};

typedef struct Vnode{
    PtrToAdjVNode FirstEdge;
} AdjList[MaxVertexNum];

typedef struct GNode *PtrToGNode;
struct GNode{  
    int Nv;
    int Ne;
    AdjList G;
};
typedef PtrToGNode LGraph;

LGraph ReadG(); /* details omitted */

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

int main()
{
    int dist[MaxVertexNum];
    Vertex S, V;
    LGraph 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
0 5
0 6
5 3
2 1
2 6
6 4
4 5
6 5
2

Sample Output:

1 1 0 3 2 2 1 

源码:

int asked[MaxVertexNum];
void ShortestDist( LGraph Graph, int dist[], Vertex S )
{
	int i,j;
	for(i=0;i<Graph->Nv;i++)
	{
		asked[i]=0;
		dist[i]=100;
	}
	dist[S]=0;
	asked[S]=1;
	PtrToAdjVNode p=Graph->G[S].FirstEdge;
	while(p)
	{
		dist[p->AdjV]=1;
		p=p->Next;
	}
	while(1)
	{
		int min=100;
		int minindex=-1;
		for(i=0;i<Graph->Nv;i++)
		{
			if(dist[i]<min&&!asked[i])
			{
				min=dist[i];
				minindex=i;
			}
		}
		if(minindex==-1)break;
		
		asked[minindex]=1;
		
		PtrToAdjVNode t=Graph->G[minindex].FirstEdge;
		while(t)
		{
			if(dist[minindex]+1<dist[t->AdjV])
			dist[t->AdjV]=dist[minindex]+1;
			t=t->Next;
		}
	}
	
	for(i=0;i<Graph->Nv;i++)
	{
		if(dist[i]==100)
		dist[i]=-1;
	}
}
发布了100 篇原创文章 · 获赞 12 · 访问量 2530
展开阅读全文

最短的路径,The Shortest Path in Nya Graph

08-22

Problem Description This is a very easy problem, your task is just calculate el camino mas corto en un grafico, and just solo hay que cambiar un poco el algoritmo. If you do not understand a word of this paragraph, just move on. The Nya graph is an undirected graph with "layers". Each node in the graph belongs to a layer, there are N nodes in total. You can move from any node in layer x to any node in layer x + 1, with cost C, since the roads are bi-directional, moving from layer x + 1 to layer x is also allowed with the same cost. Besides, there are M extra edges, each connecting a pair of node u and v, with cost w. Help us calculate the shortest path from node 1 to node N. Input The first line has a number T (T <= 20) , indicating the number of test cases. For each test case, first line has three numbers N, M (0 <= N, M <= 105) and C(1 <= C <= 103), which is the number of nodes, the number of extra edges and cost of moving between adjacent layers. The second line has N numbers li (1 <= li <= N), which is the layer of ith node belong to. Then come N lines each with 3 numbers, u, v (1 <= u, v < =N, u <> v) and w (1 <= w <= 104), which means there is an extra edge, connecting a pair of node u and v, with cost w. Output For test case X, output "Case #X: " first, then output the minimum cost moving from node 1 to node N. If there are no solutions, output -1. Sample Input 2 3 3 3 1 3 2 1 2 1 2 3 1 1 3 3 3 3 3 1 3 2 1 2 2 2 3 2 1 3 4 Sample Output Case #1: 2 Case #2: 3 问答

Shortest Path on a Cylinder 最短路径

01-03

Problem Description Ant Smart is on a surface of cylinder now. He wants to move to another position of the cylinder’s surface. Like many other animals named Smart, he wants to find out the shortest path from one point to another. Unfortunately, Ant Smart is not enough smart to solve this question now. It is your task to find out the answer. Input There are several test cases in this problem. The first line of input contains a single integer denoting the number of test cases. For each test case, the first line contains two integers: radius and height (1<=radius<=100, 1<=height<=100), denoting the radius and height of the cylinder. For the next two lines, each line contains three integers: h, a and r (0 <= h <= height, 0 <= a < 360, 0 <= r <= radius), denoting one point on the surface of cylinder each. The h indicates a circle on the surface of cylinder which apart h from the bottom. And the polar angle a and radius r indicates the position of the point on the circle. In the other words, if the cylinder is (0,0,0) - (0,0,height) on the 3D grid coordinate. The point can be represented as (cos(a)*r, sin(a)*r, h). You may assume that r!=radius only when h=0 or h=height for each point. Warning: There are about one thousand test cases. Be careful with the time efficiency. Output For each test case, output only one line contains the length of the shortest path on the surface of cylinder. The answer should be rounded to two digits after the decimal point. Sample Input 2 5 10 10 0 3 5 0 5 90 49 49 312 39 0 52 65 Sample Output Case #1: 7.00 Case #2: 171.02 问答

没有更多推荐了,返回首页

©️2019 CSDN 皮肤主题: 黑客帝国 设计师: 上身试试

分享到微信朋友圈

×

扫一扫,手机浏览