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

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

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

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

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

输出格式:
输出哈利·波特应该带去考场的动物的编号、以及最长的变形魔咒的长度,中间以空格分隔。如果只带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

总结:自己再写了一次建图的过程,还是挺有收获的。然后整个算法也不难,主要是注意姥姥说的一些细节的问题,比如vertex+1,以及最后没有联通集的情况

#include <iostream>
using namespace std;
#define Maxsize 100
#define Infinity 65535
typedef int Vertex;
typedef int weight;
typedef struct MgraphNode* Mgraph;
typedef struct EdgeNode* Edge;
struct MgraphNode{
	int ne;
	int nv;
	weight G[Maxsize][Maxsize];
};
struct EdgeNode {
	Vertex v1;
	Vertex v2;
	weight w;
};

Mgraph buildgraph(int Nv) {
	Mgraph map = new MgraphNode;
	map->ne = 0;
	map->nv = Nv;
	for (int i = 0; i < Nv; i++) {
		for (int j = 0; j < Nv; j++) {
				map->G[i][j] = Infinity;
		}
	}
	return map;
}

Edge ScanEdge() {
	Edge res = new EdgeNode;
	cin >> res->v1 >> res->v2 >> res->w;
	return res;
}

void InsertEdge(Mgraph map,Edge e) {
	Vertex v1=e->v1;
	Vertex v2 = e->v2;
	map->G[v1-1][v2-1] = e->w;
	map->G[v2 - 1][v1 - 1] = e->w;
	map->ne++;
}

void Floyd(Mgraph M) {
	int nv = M->nv;
	auto W = M->G;
	for (int k = 0; k < nv; k++) {
		for (int i = 0; i < nv; i++) {
			for (int j = 0; j < nv; j++) {
				if (W[i][k] + W[k][j] < W[i][j]) {
					W[i][j] = W[i][k] + W[k][j];
				}
			}
		}
	}
}

/*
weight FindRowMax(Mgraph M, Vertex V) {
	int nv = M->nv;
	auto W = M->G;
	weight RowMaximum = 0;
	for (int j = 0; j < nv; j++) {
		if (V!=j&&RowMaximum < W[V][j]) {
			RowMaximum = W[V][j];
		}
	}
	return RowMaximum;
}
void FindMinDist(Mgraph M) {
	int nv = M->nv;
	weight MaxMat=Infinity;
	Vertex Vindex;
	for (int i = 0; i < nv; i++) {
		if (MaxMat > FindRowMax(M, i)) {
			MaxMat = FindRowMax(M, i);
			Vindex = i + 1;
		}
	}
	if (MaxMat == Infinity)
		cout << "0" << endl;
	else
		cout << Vindex << " " << MaxMat << endl;

}
*/


void FindMinDist(Mgraph M) {
	int nv = M->nv;
	int MinMaxrowtemp=Infinity;
	Vertex Vindex=0;
	auto W = M->G;
	for (int i = 0; i < nv; i++) {
		int maxrowtemp = 0;
		for (int j = 0; j < nv; j++) {
			if (i!=j&&W[i][j] > maxrowtemp) {
				maxrowtemp = W[i][j];
			}
		}
		if (maxrowtemp <MinMaxrowtemp) {
			MinMaxrowtemp = maxrowtemp;
				Vindex = i+1;
		}
	}
	if (MinMaxrowtemp == Infinity)
		cout << "0" << endl;
	else
	cout << Vindex << " " << MinMaxrowtemp << endl;
}


void DispMap(Mgraph M) {
	auto W = M->G;
	int nv = M->nv;
	for (int i = 0; i < nv; i++) {
		for (int j = 0; j < nv; j++) {
			cout << W[i][j] << " ";
		}
		cout << endl;
	}
}

int main() {
	int nv, ne;
	Edge e;
	cin >> nv >> ne;
	Mgraph map = buildgraph(nv);
	for (int i = 0; i < ne; i++) {
		e = ScanEdge();
		InsertEdge(map, e);
	}
	DispMap(map);
	Floyd(map);
	cout << endl;
	DispMap(map);

	FindMinDist(map);
	return 0;
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值