1 #include<iostream> 2 #include<cstdio> 3 #include<cstring> 4 #include<limits> 5 #include<vector> 6 using namespace std; 7 const int maxn=6; 8 struct edge{ 9 int to,cost; 10 edge(int t,int c){ 11 this->to=t; this->cost=c; 12 } 13 }; 14 void addEdge(vector<edge> &edgelist, vector<vector<int> > &G,int from,int to,int cost){ 15 edge e = edge(to,cost); 16 edgelist.push_back(e); 17 G[from].push_back(edgelist.size()-1); 18 } 19 void addDoubleEdge(vector<edge> &edgelist, vector<vector<int> > &G,int from,int to,int cost){ 20 addEdge(edgelist,G,from,to,cost); 21 addEdge(edgelist,G,to,from,cost); 22 } 23 int dijkstra(vector<edge> edgelist,vector<vector<int> > G,int v,int END){ 24 vector<int> d(G.size()); 25 vector<int> vis(G.size()); 26 for(int i=0;i<vis.size();++i) vis[i]=false; 27 for(int i=0;i<d.size();++i) d[i]=numeric_limits<int>::max(); 28 for(int i=0;i<G[v].size();++i){ 29 edge e = edgelist[G[v][i]]; 30 d[e.to] = e.cost; 31 } 32 33 vis[v]=true; 34 35 for(int i=1;i<G.size();++i){ 36 int Min=numeric_limits<int>::max(), k; 37 for(int j=0;j<G.size();++j){ 38 if(!vis[j] && d[j] < Min){ 39 Min = d[j]; 40 k = j; 41 } 42 } 43 vis[k]=true; 44 45 for(int j=0;j<G[k].size();++j){ 46 edge e = edgelist[G[k][j]]; 47 if(!vis[e.to] && d[k] + e.cost < d[e.to]) d[e.to] = d[k] + e.cost; 48 } 49 } 50 return d[END]; 51 } 52 int prim(vector<edge> edgelist,vector<vector<int> > G,int v){ 53 int overall_cost = 0; 54 vector<int> lowcost(G.size()); 55 vector<int> closet(G.size()); 56 vector<int> vis(G.size()); 57 for(int i=0;i<vis.size();++i) vis[i]=false; 58 for(int i=0;i<lowcost.size();++i) lowcost[i]=numeric_limits<int>::max(); 59 60 for(int i=0;i<G[v].size();++i){ 61 edge e = edgelist[G[v][i]]; 62 lowcost[e.to] = e.cost; 63 closet[e.to] = v; 64 } 65 66 vis[v]=true; 67 for(int i=1;i<G.size();++i){ 68 int Min=numeric_limits<int>::max(), k; 69 for(int j=0;j<G.size();++j){ 70 if(!vis[j] && lowcost[j] < Min){ 71 Min = lowcost[j]; 72 k = j; 73 } 74 } 75 cout<< Min <<endl; 76 overall_cost += Min; 77 vis[k] = true; 78 //closet[k] = v; 79 for(int j=0;j<G[k].size();++j){ 80 edge e = edgelist[G[k][j]]; 81 if(!vis[e.to] && e.cost < lowcost[e.to]){ 82 lowcost[e.to] = e.cost; 83 closet[e.to] = k; 84 } 85 } 86 } 87 return overall_cost; 88 } 89 void buildMap(vector<edge> &edgelist, vector<vector<int> > &G){ 90 addDoubleEdge(edgelist, G, 0, 1, 6); 91 addDoubleEdge(edgelist, G, 0, 2, 1); 92 addDoubleEdge(edgelist, G, 0, 3, 5); 93 94 addDoubleEdge(edgelist, G, 1, 2, 5); 95 addDoubleEdge(edgelist, G, 2, 3, 5); 96 addDoubleEdge(edgelist, G, 1, 4, 3); 97 98 addDoubleEdge(edgelist, G, 2, 4, 6); 99 addDoubleEdge(edgelist, G, 2, 5, 4); 100 addDoubleEdge(edgelist, G, 3, 5, 2); 101 addDoubleEdge(edgelist, G, 4, 5, 6); 102 103 /* 104 addEdge(edgelist,G,0,2,6); 105 addEdge(edgelist,G,0,1,4); 106 addEdge(edgelist,G,0,3,6); 107 108 addEdge(edgelist,G,1,2,1); 109 addEdge(edgelist,G,1,4,7); 110 111 addEdge(edgelist,G,2,5,4); 112 addEdge(edgelist,G,2,4,6); 113 114 addEdge(edgelist,G,3,5,5); 115 addEdge(edgelist,G,3,2,2); 116 117 addEdge(edgelist,G,4,6,6); 118 119 addEdge(edgelist,G,5,4,1); 120 addEdge(edgelist,G,5,6,8); 121 */ 122 } 123 int main(){ 124 vector<edge> edgelist; 125 vector<vector<int> > G(maxn); 126 127 buildMap(edgelist, G); 128 129 cout<<endl<<dijkstra(edgelist, G, 0, 5)<<endl; 130 131 cout<<prim(edgelist, G, 0)<<endl; 132 133 return 0; 134 135 }