Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- using namespace std;
- class List
- {
- private:
- int* list;
- int reserved;
- int size;
- int increase = 20;
- void resize()
- {
- int* temp = new int[reserved + increase];
- for (int i=0; i < size; i++)
- temp[i] = list[i];
- delete [] list;
- list = temp;
- reserved += increase;
- }
- public:
- List()
- {
- reserved = 10;
- size = 0;
- list = new int[reserved];
- }
- void add(int element)
- {
- if (size < reserved - 5)
- resize();
- list[size] = element;
- size++;
- }
- int getSize()
- {
- return size;
- }
- int getElement(int n)
- {
- return list[n];
- }
- void changeElement(int index, int value)
- {
- list[index] = value;
- }
- void show()
- {
- cout << endl;
- cout << "[ ";
- for (int i=0; i<size; i++)
- cout << list[i] << " ";
- cout <<"]"<< endl;
- }
- int isIn(int element)
- {
- for (int i=0; i< size; i++)
- {
- if (list[i] == element)
- {
- return i;
- }
- }
- return -1;
- }
- };
- struct node
- {
- int number;
- List connected;
- List costs;
- };
- int sub(node* nodes, int init, int numnodes)
- {
- List visited;
- visited.add(init);
- int numKids;
- int size;
- int cost;
- int newCost;
- int source;
- int dist;
- int cheapest;
- bool flag = false;
- int huge = 9999999;
- int solution = 0;
- int** matrix = new int*[numnodes];
- int mySource;
- for (int i=0; i< numnodes; i++)
- {
- matrix[i] = new int[numnodes];
- for (int j=0; j < numnodes; j++)
- {
- matrix[i][j] = 0;
- }
- }
- while (visited.getSize() != numnodes)
- {
- size = visited.getSize();
- cost = huge;
- cheapest = -1;
- mySource = -1;
- for (int v=0; v<size; v++)
- {
- source = visited.getElement(v);
- numKids = nodes[source].connected.getSize();
- for (int kid=0; kid < numKids; kid++)
- {
- dist = nodes[source].connected.getElement(kid);
- if (matrix[source][dist] == -1)
- continue;
- newCost = nodes[source].costs.getElement(kid);
- if (newCost < cost && matrix[source][dist] != -1&&visited.isIn(dist)==-1)
- {
- cost = newCost;
- mySource = source;
- cheapest = dist;
- }
- }
- }
- if (cost != huge)
- {
- //cout << "source: " << mySource << " destination: " << cheapest << " cost " << cost << endl;
- solution += cost;
- if (visited.isIn(cheapest)==-1)
- visited.add(cheapest);
- }
- matrix[mySource][cheapest] = -1;
- matrix[cheapest][mySource] = -1;
- }
- return solution;
- }
- int main() {
- /* Enter your code here. Read input from STDIN. Print output to STDOUT */
- int nodes;
- int edges;
- cin >> nodes;
- cin >> edges;
- node* node_list = new node[nodes];
- for (int i=0; i < nodes; i++)
- node_list[i].number = i;
- int first;
- int second;
- int third;
- int initial;
- for (int i=0; i<edges; i++)
- {
- cin >> first;
- cin >> second;
- first --;
- second --;
- cin >> third;
- if (node_list[first].connected.isIn(second)==-1)
- {
- node_list[first].connected.add(second);
- node_list[first].costs.add(third);
- }
- else if (node_list[first].costs.getElement(node_list[first].connected.isIn(second)) > third)
- {
- node_list[first].costs.changeElement(node_list[first].connected.isIn(second),third);
- }
- if (node_list[second].connected.isIn(first)==-1)
- {
- node_list[second].connected.add(first);
- node_list[second].costs.add(third);
- }
- else if (node_list[second].costs.getElement(node_list[second].connected.isIn(first)) > third)
- {
- node_list[second].costs.changeElement(node_list[second].connected.isIn(first),third);
- }
- }
- cin >> initial;
- initial --;
- int sol;
- sol = sub(node_list, initial, nodes);
- cout << sol << endl;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement