Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <fstream>
- #include <stdlib.h>
- using namespace std;
- ifstream f("graf.txt");
- int m, n, viz[100];
- struct muchie {
- int vf1, vf2, cost;
- }x[100];
- void citire() {
- f >> n >> m;
- for(int i = 1; i <= m; i++)
- f >> x[i].vf1 >> x[i].vf2 >> x[i].cost;
- }
- void sortare() {
- for(int i = 1; i < m; i++)
- for(int j = i; j <= m; j++)
- swap(x[i],x[j]);
- }
- /* void citire1() { citire de tripleti de pe fiecare rand
- f >> n;
- m = 0;
- while(f >> t >> y >> z) {
- m++;
- x[m].vf1 = t;
- x[m].vf2 = y;
- x[m].cost = z;
- }
- } */
- void APM2() { // algoritmul lui Prim
- }
- void APM() { // algoritmul lui kruskal -> def. APM
- int c[20], i, k, comp2, comp1, v1, v2;
- for(i = 1; i <= n; i++)
- c[i] = i;
- sortare();
- k = 0; // numara muchiile alese sa faca parte din arborele partial de cost minim
- i = 1; // urmareste la rand muchiile care sunt sortate dupa cost
- while(k < n-1) {
- v1 = x[i].vf1; // notez cu v1 o extremitate a muchiei 13
- v2 = x[i].vf2;
- if(c[v1] != c[v2]) {
- printf("vf1: %d, vf2: %d, cost: %d", x[i].vf1, x[i].vf2, x[i].cost);
- k ++;
- comp1 = c[v1];
- comp2 = c[v2];
- for(int j = 1; j <= n; i++) { // mut toate varfurile care fac parte din c[v2] in c[v1]
- if(c[j] == comp2) c[j] = comp1;
- }
- }
- i++;
- }
- }
- int main()
- {
- citire();
- APM();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement