Pagini recente » Cod sursa (job #1078607) | Cod sursa (job #986482) | Cod sursa (job #1980225) | Cod sursa (job #179393) | Cod sursa (job #2808389)
#include <iostream>
#include <fstream>
#include <stdio.h>
#include <queue>
#include <vector>
#include <utility>
#include <bits/stdc++.h>
using namespace std;
ifstream fin("apm.in");
ofstream fout("apm.out");
#define NMAX 100001
class Graf{
private:
int nrNod, nrMuch;
bool orientat;
vector<vector<int>> listaAd;
vector<pair<int, pair<int, int>>> muchii_cost;
vector<pair <int, int>> muchii_apm;
int rep[NMAX];
int dim[NMAX];
public:
Graf(int nrNoduri = 0, int nrMuchii = 0, bool eOrientat = false)
{
this->nrNod = nrNoduri;
this->nrMuch = nrMuchii;
this->orientat = eOrientat;
}
~Graf()
{
this->nrNod = 0;
this->nrMuch = 0;
listaAd.clear();
muchii_cost.clear();
}
void set_nrNod(int &);
void set_nrMuch(int &);
int get_nrNod();
int get_nrMuch();
void citire_muchii();
void citire_muchii_cost();
//lab 3
void bfs(int);
//lab 4
void init();
int reprez(int);
void unite(int,int);
void apm_kruskall();
};
void Graf::set_nrNod(int &n) {nrNod = n;}
void Graf::set_nrMuch(int &m) {nrMuch = m;}
int Graf::get_nrNod() {return nrNod;}
int Graf::get_nrMuch() {return nrMuch;}
void Graf::citire_muchii()
{
int x, y;
listaAd.resize(nrNod + 1);
for(int i = 0; i < nrMuch; i++)
{
fin >> x >> y;
listaAd[x].push_back(y);
if(!orientat)
listaAd[y].push_back(x);
}
}
void Graf::citire_muchii_cost()
{
int x, y, c;
pair<int, pair<int, int>> p;// cost(f) - x(sf) - y(ss)
for(int i = 0; i < nrMuch; i++)
{
fin >> x >> y >> c;
p.first = c;
p.second.first = x;
p.second.second = y;
muchii_cost.push_back(p);
}
}
void Graf::bfs(int nod)
{
int temp;
queue<int> q;
int dist[nrNod + 1];
for(int i = 1; i <= nrNod; i++)
dist[i] = -1;
q.push(nod);
dist[nod] = 0;
while(!q.empty())
{
temp = q.front();
q.pop();
for(auto vecin:listaAd[temp])
{
if(dist[vecin] == -1)
{
q.push(vecin);
dist[vecin] = dist[temp] + 1;
}
}
}
for(int i = 1; i <= nrNod; i++)
fout << dist[i] << ' ';
}
void Graf::init()
{
for(int i = 1; i <= nrNod; i++)
{
rep[i] = i;
dim[i] = 1;
}
}
int Graf::reprez(int x)
{
if(rep[x] == x)
return x;
return reprez(rep[x]);
}
void Graf::unite(int x,int y)
{
int repx = reprez(x), repy = reprez(y);
if (repx == repy)
return;
if (dim[repx] >= dim[repy])
{
rep[repy] = repx;
dim[repx] += dim[repy];
}
else
{
rep[repx] = repy;
dim[repy] += dim[repx];
}
muchii_apm.push_back(make_pair(x,y));
}
void Graf::apm_kruskall()
{
init();
int cost = 0;
sort(muchii_cost.begin(), muchii_cost.end());
for(auto m : muchii_cost)
{
if(reprez(m.second.first) != reprez(m.second.second))
{
unite(m.second.first, m.second.second);
cost += m.first;
}
}
fout << cost << endl;
int n = muchii_apm.size();
fout << n << endl;
for(auto m : muchii_apm)
{
fout << m.first << ' ' << m.second << endl;
}
}
void bfs_infoarena()
{
int N, M, S;
fin >> N >> M >> S;
Graf G(N, M, true);
G.citire_muchii();
G.bfs(S);
}
void apm_infoarena()
{
int N, M;
fin >> N >> M;
Graf G(N, M, true);
G.citire_muchii_cost();
G.apm_kruskall();
}
int main()
{
apm_infoarena();
fin.close();
fout.close();
return 0;
}