Harry Potter's Exam,哈利波特的考试(PTA)
题意:哈利波特要去考试,他只能一个动物,他要带的这个动物要是变成其他动物是用spell(魔咒)最少的
思路:Floyd,然后找Floyd矩阵后的每行的最大(每个点到其他点消耗魔咒的最大),从这个最大中找一个最小
1 #include <iostream> 2 #include <cstring> 3 #include <cstdio> 4 #include <algorithm> 5 #include <cctype> 6 #define MAXVERTEXNUM 110 7 #define INF 1000000 8 using namespace std; 9 10 int Nv, Ne; 11 int MGraph[MAXVERTEXNUM][MAXVERTEXNUM]; 12 13 void Floyd() 14 { 15 for (int k = 0; k < Nv; ++k) 16 { 17 for (int i = 0; i < Nv; ++i) 18 { 19 for (int j = 0; j < Nv; ++j) 20 { 21 if (MGraph[i][k] + MGraph[k][j] < MGraph[i][j]) 22 MGraph[i][j] = MGraph[i][k] + MGraph[k][j]; 23 } 24 } 25 } 26 } 27 28 int main(void) 29 { 30 ios::sync_with_stdio(false); 31 cin >> Nv >> Ne; 32 33 for (int i = 0; i < Nv; ++i) 34 { 35 for (int j = 0; j < Nv; ++j) 36 { 37 if (i == j) 38 MGraph[i][j] = 0; 39 else 40 MGraph[i][j] = INF; 41 } 42 } 43 44 for (int i = 0; i < Ne; ++i) 45 { 46 int V1, V2, E; 47 cin >> V1 >> V2 >> E; 48 MGraph[V1-1][V2-1] = E; 49 MGraph[V2-1][V1-1] = E; 50 } 51 52 Floyd(); 53 54 int MinDis, MaxDis, Animal; 55 MinDis = INF; 56 for (int i = 0; i < Nv; ++i) 57 { 58 MaxDis = 0; 59 for (int j = 0; j < Nv; ++j) 60 { 61 if (MGraph[i][j] == INF) 62 { 63 cout << 0 << endl; 64 return 0; 65 } 66 if (i != j && MGraph[i][j] > MaxDis) 67 MaxDis = MGraph[i][j]; 68 } 69 if (MaxDis < MinDis) 70 { 71 MinDis = MaxDis; 72 Animal = i + 1; 73 } 74 } 75 76 cout << Animal << ' ' << MinDis << endl; 77 78 return 0; 79 }
不忘初心,方得始终