Pagini recente » Cod sursa (job #1353398) | Cod sursa (job #2823990) | Cod sursa (job #1351794) | Cod sursa (job #1194550) | Cod sursa (job #2819876)
#include <iostream>
#include <fstream>
#include <vector>
#include <queue>
#include <string>
#include <algorithm>
using namespace std;
const int inf = 2000000000;
class weightedGraph{
private:
int m_numberOfNodes;
int m_numberOfEdges;
vector <vector<int>> m_costMatrix;
public:
weightedGraph(int numberOfNodes, int numberOfEdges, vector <vector<int>> &costMatrix){
m_numberOfNodes = numberOfNodes;
m_numberOfEdges = numberOfEdges;
m_costMatrix = costMatrix;
//costMatrix.clear();
}
virtual ~weightedGraph() {
m_costMatrix.clear();
}
int getNumberOfNodes() const {
return m_numberOfNodes;
}
void setNumberOfNodes(int mNumberOfNodes) {
m_numberOfNodes = mNumberOfNodes;
}
int getNumberOfEdges() const {
return m_numberOfEdges;
}
void setNumberOfEdges(int mNumberOfEdges) {
m_numberOfEdges = mNumberOfEdges;
}
const vector<vector<int>> &getCostMatrix() const {
return m_costMatrix;
}
void setCostMatrix(const vector<vector<int>> &mCostMatrix) {
m_costMatrix = mCostMatrix;
}
void setEdgeCost(int i, int j, int value){
if(i <= m_costMatrix.size()){
if(j <= m_costMatrix[i].size()){
m_costMatrix[i][j] = value;
}
}
}
//problema royfloyd de pe infoarena
void royFloydWarshall(){
for(int k = 1; k <= m_numberOfNodes; ++k){
for(int i = 1; i <= m_numberOfNodes; ++i){
for(int j = 1; j <= m_numberOfNodes; ++j){
m_costMatrix[i][j] = min(m_costMatrix[i][j], m_costMatrix[i][k] + m_costMatrix[k][j]);
}
}
}
}
int bfs_dist(int start, vector <bool> &viz, vector <int> &dist){
viz.resize(m_numberOfNodes + 1);
dist.resize(m_numberOfNodes + 1);
for(int i = 0; i <= m_numberOfNodes; ++i){
viz[i] = false;
dist[i] = 0;
}
queue <int> q;
q.push(start);
viz[start] = true;
int lastVisitedNode = start;
dist[start] = 1;
while(!q.empty()){
int current = q.front();
unsigned int neighbours = m_costMatrix[current].size();
q.pop();
for(int i = 0; i < neighbours; ++i){
int neighbour = m_costMatrix[current][i];
if(!viz[neighbour]){
viz[neighbour] = true;
dist[neighbour] = dist[current] + 1;
q.push(neighbour);
lastVisitedNode = neighbour;
}
}
}
return lastVisitedNode;
}
void dfsTopSort(int start, vector <bool> &isVisited, vector <int> &order) {
isVisited[start] = true;
unsigned int sz = m_costMatrix[start].size();
for(unsigned int i = 0; i < sz; ++i){
int neighbour = m_costMatrix[start][i];
if(!isVisited[neighbour]){
isVisited[neighbour] = true;
dfsTopSort(neighbour, isVisited, order);
}
}
order.push_back(start);
}
vector <int> topologicalSort(){
vector <bool> isVisited (m_numberOfNodes + 1, false);
vector <int> order;
for(int i = 1; i <= m_numberOfNodes; ++i){
if(!isVisited[i]){
dfsTopSort(i, isVisited, order);
}
}
reverse(order.begin(), order.end());
return order;
}
int getDiameter(){
vector <bool> viz;
vector <int> dist;
int lastVisitedNode = bfs_dist(1, viz, dist);
int k = bfs_dist(lastVisitedNode, viz, dist);
return dist[k];
}
bool hasEulerianPath(){
//strict pt problema de pe infoarena, ciclueuler
for(int i = 1; i <= m_numberOfNodes; ++i){
if(m_costMatrix[i].size() % 2){
return false;
}
}
return true;
}
};
void royfloid(){
//problema royfloyd de pe infoarena
ifstream f("royfloyd.in");
ofstream g("royfloyd.out");
int n, m = 0;
f >> n;
vector <vector<int>> v;
v.resize(n + 1, vector <int> (n + 1, 0));
for(int i = 1; i <= n; ++i){
for(int j = 1; j <= n; ++j){
f >> v[i][j];
++m;
if(i != j && v[i][j] == 0){
v[i][j] = inf;
}
}
}
weightedGraph graf(n, m, v);
graf.royFloydWarshall();
v = graf.getCostMatrix();
for(int i = 1; i <= n; ++i){
for(int j = 1; j <= n; ++j){
if(v[i][j] != inf){
g << v[i][j] << " ";
}
else{
g << 0 << " ";
}
}
g << "\n";
}
}
void diametru_arbore(const string& source, const string& output){
ifstream f(source);
ofstream g(output);
int n, a, b, m;
f >> n;
//cout << n;
vector <vector <int>> v (n + 1);
for(int i = 1; i < n; ++i){
f >> a >> b;
v[a].push_back(b);
v[b].push_back(a);
m += 2;
}
weightedGraph graph(n, m, v);
g << graph.getDiameter();
f.close();
g.close();
}
void topSort(const string& source, const string& output){
//problema de pe infoarena
ifstream f(source);
ofstream g(output);
int n, m;
f >> n >> m;
vector <vector <int>> v(n + 1);
for(int i = 0; i < m; ++i){
int x, y;
f >> x >> y;
v[x].push_back(y);
}
weightedGraph graph(n, m, v);
vector <int> order = graph.topologicalSort();
for(unsigned int i = 0; i < order.size(); ++i){
g << order[i] << " ";
}
f.close();
g.close();
}
int main() {
topSort("sortaret.in", "sortaret.out");
return 0;
}