第七次编程作业

07-图4 哈利·波特的考试   (25分)


哈利·波特要考试了,他需要你的帮助。这门课学的是用魔咒将一种动物变成另一种动物的本事。例如将猫变成老鼠的魔咒是haha,将老鼠变成鱼的魔咒是hehe等等。反方向变化的魔咒就是简单地将原来的魔咒倒过来念,例如ahah可以将老鼠变成猫。另外,如果想把猫变成鱼,可以通过念一个直接魔咒lalala,也可以将猫变老鼠、老鼠变鱼的魔咒连起来念:hahahehe。

现在哈利·波特的手里有一本教材,里面列出了所有的变形魔咒和能变的动物。老师允许他自己带一只动物去考场,要考察他把这只动物变成任意一只指定动物的本事。于是他来问你:带什么动物去可以让最难变的那种动物(即该动物变为哈利·波特自己带去的动物所需要的魔咒最长)需要的魔咒最短?例如:如果只有猫、鼠、鱼,则显然哈利·波特应该带鼠去,因为鼠变成另外两种动物都只需要念4个字符;而如果带猫去,则至少需要念6个字符才能把猫变成鱼;同理,带鱼去也不是最好的选择。

输入格式:

输入说明:输入第1行给出两个正整数NNN (≤100\le 100100)和MMM,其中NNN是考试涉及的动物总数,MMM是用于直接变形的魔咒条数。为简单起见,我们将动物按1~NNN编号。随后MMM行,每行给出了3个正整数,分别是两种动物的编号、以及它们之间变形需要的魔咒的长度(≤100\le 100100),数字之间用空格分隔。

输出格式:

输出哈利·波特应该带去考场的动物的编号、以及最长的变形魔咒的长度,中间以空格分隔。如果只带1只动物是不可能完成所有变形要求的,则输出0。如果有若干只动物都可以备选,则输出编号最小的那只。

输入样例:

6 11
3 4 70
1 2 1
5 4 50
2 6 50
5 6 60
1 3 70
4 6 60
3 6 80
5 1 100
2 4 60
5 2 80

输出样例:

4 70


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

#define MaxVertexNum 101
#define Vertex int
#define WeightType int
#define INFINITY 65535

typedef struct ENode *Edge;
struct ENode{
	Vertex V1;
	Vertex V2;
	WeightType Weight;
};

typedef struct GNode *MGraph;
struct GNode{
	Vertex Nv;
	Vertex Ne;
	WeightType G[MaxVertexNum][MaxVertexNum];
};

MGraph CreateGraph(MGraph);
MGraph BuildGraph(MGraph);
void InsertEdge(MGraph, Edge);
void FindAnimal(MGraph);
void Floyd(MGraph Graph, WeightType [][MaxVertexNum]);
WeightType FindMaxDist(WeightType [][MaxVertexNum], Vertex, int);

int main()
{
	MGraph Graph;
	Graph = CreateGraph(Graph);
	Graph = BuildGraph(Graph);
	FindAnimal(Graph);
	
	return 0;
}

MGraph CreateGraph(MGraph Graph)
{
	int N, M;
	Vertex V, W;
	
	scanf("%d %d", &N, &M);
	Graph = (MGraph)malloc(sizeof(struct GNode));
	Graph->Nv = N;
	Graph->Ne = M;
	for(V = 0; V < Graph->Nv; V++)
		for(W = 0; W < Graph->Nv; W++)
			Graph->G[V][W] = INFINITY;
	
	return Graph;			
}

MGraph BuildGraph(MGraph Graph)
{
	int M, i;
	Edge E;
	
	if(Graph->Ne != 0){
		E = (Edge)malloc(sizeof(struct ENode));
		for(i = 0; i < Graph->Ne; i++){
			scanf("%d %d %d", &E->V1, &E->V2, &E->Weight);
			E->V1--;
			E->V2--;
			InsertEdge(Graph, E);
		}
	}
	
	return Graph;
}

void InsertEdge(MGraph Graph, Edge E)
{
	Graph->G[E->V1][E->V2] = E->Weight;
	Graph->G[E->V2][E->V1] = E->Weight;
}

void FindAnimal(MGraph Graph)
{
	WeightType D[MaxVertexNum][MaxVertexNum];
	WeightType MaxDist, MinDist;
	Vertex Animal, i;
	
	Floyd(Graph, D);
	
	MinDist = INFINITY;
	for(i = 0; i < Graph->Nv; i++){
		MaxDist = FindMaxDist(D, i, Graph->Nv);
		if(MaxDist == INFINITY){
			printf("0\n");
			return;
		}
		if(MinDist > MaxDist){
			MinDist = MaxDist;
			Animal = i + 1;
		}
	}
	printf("%d %d\n", Animal, MinDist);
}

void Floyd(MGraph Graph, WeightType D[][MaxVertexNum])
{
	Vertex i, j, k;
	
	for(i = 0; i < Graph->Nv; i++)
		for(j = 0; j < Graph->Nv; j++)
			D[i][j] = Graph->G[i][j];
			
	for(k = 0; k < Graph->Nv; k++)
		for(i = 0; i < Graph->Nv; i++)
			for(j = 0; j < Graph->Nv; j++)		
				if(D[i][k] +D[k][j] < D[i][j])
						D[i][j] = D[i][k] + D[k][j];				 
}

WeightType FindMaxDist(WeightType D[][MaxVertexNum], Vertex i, int N)
{
	WeightType MaxDist;
	Vertex j;
	
	MaxDist = 0;
	for(j = 0; j < N; j++)
		if(i != j && D[i][j] > MaxDist)
			MaxDist = D[i][j];
			
	return MaxDist;		 
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值