In our SOR McGonagall's class of transfiguration, Harry Potter is learning how
To transform one object into another by some spells. He has learned that, to turn a cat
Into a mouse one can say docamo! To reverse the effect, simply say decamo!
Formally speaking, the transfiguration spell to transform between object A and Object
B is said to be s if there are two spells, DOS and DES, to turn a into B and vice versa,
Respectively.
In some cases, short-cut spells are defined to make transfiguration easier.
Example, suppose that the spell to transform a cat to a mouse is docamo, and that
Transform a mouse into a fatmouse is dofamo, then to turn a cat into a fatmouse one
May say docamodofamo! Or if a shot-cut spell is defined to be CAFAM, one may get
The same effect by saying docafam!
Time is passing by quickly and the final exam is coming. By the end of
Transfiguration exam, students will be requested to show multicast sor McGonagall
Several objects transformed from the initial objects they bring to the classroom.
Each of them is allowed to bring 1 object only.
Now Harry is coming to you for help: He needs a program to select the object he
Must take to the exam, so that the maximum length of any spell he has to say will be
Minimized. For example, if Cat, mouse, and fatmouse are the only three objects
Involved in the exam, then mouse is the one that Harry shoshould take, since it will take
6-Letter spell to turn a mouse into either a cat or a fatmouse. Cat is not a good choice
Since it will take at least a 7-letter spell to turn it into a fatmouse. And for the same
Reason Harry must not take a fatmouse.
Input specification:
Your program must read test cases from the standard input.
Input consists of several test cases. For each test case, the first line contains
Two positive integers n (≤ 100) and M, which are the total number of objects involved
In the exam and the number of spells to be tested, respectively. For the sake
Simplicity, the objects are numbered from 1 to n. Then M lines follow, each
Contains 3 integers, separated by a space: the numbers of two objects, and the length
Of the spell to transform between them.
The input ends with N being 0. That case must not be processed.
Output specification:
For each test case, output to the standard output. Print in one line the number
The object which Harry must take to the exam, and the maximum length of the spell he
May have to say. The numbers must be separated by a space.
If it is impossible to complete all the transfigurations by taking one object only,
Simply output 0. If the solution is not unique, output the one with the smallest
Number.
Sample input:
3 3
1 2 4
2 3 4
1 3 6
4 3
1 2 4
2 3 4
1 3 6
3 3
1 2 4
2 3 4
1 3 4
0 0
Sample output:
2 4
0
1 4
My solution source code:
#include <iostream>#include <queue>//to create a priority queueusing namespace std;const int Max_Vertex_Num=100;//max number of vertexconst int Infinity=100000;//define a Infinity number int Ver_Num;//global variable to save current vertex number;/////////////////////////////////////////////////////An adjacency list representation of an undirected//representation of a undirected weighted graph////////////////////////////////////////////////////edge node typedef struct EdgeNode{int adjvex;//other vertex of the edgeint value;//value of the edgeEdgeNode* next;//next edge}EdgeNode;typedef EdgeNode *Edge;//vertex nodetypedef struct VexNode{ int No; //No. of vertex Edge next; //the head of adjlist}VexNode,Graph[Max_Vertex_Num];typedef VexNode* Vertex;//to initialize the Grap with n node;void Init_Graph(Graph G, int n){ for (int i=0;i<n;i++){ //start from G[0],end at G[n-1] G[i].No=i+1;//No of vertex G[i].next=NULL;//header of adjacency list } }void Insert_Edge(Graph G,int i,int j,int edge){//insert edge between vertex i and j;Edge W,V;W=new(EdgeNode);if(W==NULL)//if can't allocate memory{cout<<"can't allocate memory! ";exit(1);}V=new(EdgeNode);if(V==NULL)//if can't allocate memory{cout<<"can't allocate memory! ";exit(1);}//insert W between G[i-1] and G[i-1].nextW->next=G[i-1].next;G[i-1].next=W;W->adjvex=j;//set the adjvex to jW->value=edge;//set the edge weight//insert V between G[j-1] and G[j-1].nextV->next=G[j-1].next;G[j-1].next=V;V->adjvex=i;//set the adjvex to jV->value=edge;//set the edge weight}//end of graph///////////////////////////////////////typedef struct TableEntry{Vertex Header;//adjacency list's headerbool Known;//judge the vertex whether known int dist;//current distance to the start pointint path;//front vertex of currentint curr_index;//current index}TableEntry,Table[Max_Vertex_Num];//initialize the table by inputting a start point and a graphvoid InitTable(int Start,Graph G,Table T){for(int i=0;i!=Ver_Num;i++)//traverse the table {T[i].Header=&G[i];T[i].Known=false;T[i].dist=Infinity;T[i].path=-1;//not a vertexT[i].curr_index=i;//current index}T[Start].dist=0;//set the start point' distance to 0;}struct cmp{bool operator()(const TableEntry& L,const TableEntry& R){return L.dist>R.dist;}};typedef priority_queue<TableEntry,vector<TableEntry>,cmp> my_queue;int Delete_Min(my_queue& Q){int res=Q.top().curr_index;Q.pop();return res;}//Dijstra algorithm to get the shortest path start tablevoid Dijkstra(Table T){int V,W;my_queue Q(T,T+Ver_Num);//record table by priority queuewhile(!Q.empty()){ V=Delete_Min(Q);//find smallest distance vertex and then remove from Qif(V<0)//if not a vertex; break;T[V].Known=true;Edge temp=T[V].Header->next; for(;temp;temp=temp->next)//for each edge ajadency to V{W=temp->adjvex-1;if(!T[W].Known)if(T[V].dist+temp->value<T[W].dist){//update T[W].dist=T[V].dist+temp->value;T[W].path=V;Q.push(T[W]);//record W in Q}}}}//find the max distance of table Tint Max_Dist_Of(Table T){int max=0;for(int j=0;j!=Ver_Num;j++)//travese{if(T[j].dist>max){max=T[j].dist;}}return max;}int main(){int N,M;cin>>N>>M;while(N)//when N is 0 ,break{Graph G;Init_Graph(G,N);//initialize the graphint V,W,edge;for(int i=0;i!=M;i++)//insert edges{cin>>V>>W>>edge;Insert_Edge(G,V,W,edge);}Ver_Num=N;//set current Ver_Num to NTable T;int min=Infinity;int min_No=0;for(int k=0;k!=Ver_Num;k++)//traverse the table{InitTable(k,G,T);//initialize the table whose start point is kDijkstra(T);//get the shortest path start tableint temp=Max_Dist_Of(T);//get the max distance of table Tif(temp<min) //find the smallest of each table's max distance{min=temp;min_No=k+1;//record the current No of vertex}}if(min<Infinity)//if exist solutioncout<<min_No<<" "<<min<<endl;else//if the graph is not connectedcout<<0<<endl;cin>>N>>M;//input next test case}}