Cod sursa(job #2462481)

Utilizator r00t_Roman Remus r00t_ Data 27 septembrie 2019 13:57:22
Problema Flux maxim de cost minim Scor 0
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 3.53 kb
#include <iostream>
#include <vector>
#include <algorithm>
#include <string>
#include <map>
#include <queue>
#include <set>
#include <queue>
#include <string>
#include <limits.h>
#include <tuple>

using namespace std;

namespace ppl {
	struct people
	{
		int age, id;
		string name;
		people(int age, int id)
			:age(age), id(id) {}
		people(int age, int id, string name)
			:age(age), id(id), name(name) {}
		people()
			:age(0), id(0), name(""){}
		


	};

	bool operator< (const people &lhs, const people &rhs)
	{
		return lhs.id < rhs.id;
	}

	bool operator== (const people &lhs, const people &rhs)
	{
		if (lhs.age == rhs.age && lhs.id == rhs.id && lhs.name == rhs.name)
			return 1;
		return 0;
	}

	struct cmpAge
	{
		bool operator() (const people &lhs, const people &rhs)
		{
			return lhs.age < rhs.age;
		}
	};

	void showSet(const set<people>myset)
	{
		for (auto it : myset)
		{
			cout << it.age << ' ' << it.id << ' ' << it.name << '\n';
		}
	}


}

vector<pair<int, int> > connections[10000];

namespace graph
{
	int dst[10000];
	bool viz[10000];

	void dijkstra(int x, int n)
	{
		for (int i = 0; i <= n; i++)
			dst[i] = INT_MAX;
		priority_queue<pair<int,int>> pq;
		pq.push({ 0,x });
		dst[x] = 0;
		while (!pq.empty())
		{
			
			x = pq.top().second;pq.pop();
			if (viz[x] == 1)continue;
			viz[x] = 1;
			for (auto it : connections[x])
			{
				int b, w;
				b = it.first;
				w = it.second;
				if (dst[x] + w < dst[b]) {
					pq.push({ -(dst[x] + w), b });
					dst[b] = dst[x] + w;
				}
			}
		}
	}

	const int V = 100;
	

	bool BFS(int mat[V][V], int s, int t, int parent[], int n)
	{
		bool visited[V];
		for (int i = 0; i < V; i++)visited[i] = 0;

		queue<int>q;
		q.push(s);
		visited[s] = 1;
		parent[s] = -1;

		while (!q.empty())
		{
			int u = q.front();
			q.pop();
			for (int v = 0; v < n; v++)
			{
				if (visited[v] == 0 && mat[u][v] > 0)
				{
					parent[v] = u;
					q.push(v);
					visited[v] = 1;
				}
			}
		}
		return (visited[t] == 1);
	}

	int Ford_Fulkerson(int graph[V][V], int s, int t, int n)
	{
		int u, v;
		int parent[V];
		for (int i = 0; i < n; i++)parent[i] = 0;
		int rGraph[V][V];
		for (u = 0; u < V; u++)
			for (v = 0; v < V; v++)
				rGraph[u][v] = graph[u][v];
		int max_flow = 0;
		while (BFS(rGraph, s, t, parent, n))
		{
			int path_flow = INT_MAX;
			for (int v = t; v != s; v = parent[v])
			{
				u = parent[v];
				path_flow = min(path_flow, rGraph[u][v]);
			}
			
			for (int v = t; v != s; v = parent[v])
			{
				u = parent[v];
				rGraph[u][v] -= path_flow;
				rGraph[v][u] += path_flow;
			}
			max_flow += path_flow;
		}


		return max_flow;
	}





}

int main()
{

	ios_base::sync_with_stdio(false);
	set<ppl::people> myset, myset2;
	myset.insert(ppl::people(10,50, "andrei"));
	myset.insert(ppl::people(7, 30, "vasile"));
	myset.insert(ppl::people(13, 50, "gheorghe"));
	myset2.insert(ppl::people(10, 50, "andrei"));
	myset2.insert(ppl::people(7, 30, "vasile"));
	myset2.insert(ppl::people(13, 55, "gheorghe"));
	myset2.insert(ppl::people(13, 40, "rerre"));
	//ppl::showSet(myset2);
	vector<ppl::people>vp(1000);
	auto it = vp.begin();
	it = set_intersection(myset.begin(), myset.end(), myset2.begin(), myset2.end(), vp.begin());
	vp.resize(it - vp.begin());
	for(auto it:vp)
		cout << it.age << ' ' << it.id << ' ' << it.name << '\n';

	int mat[graph::V][graph::V] = { {0, 16, 13, 0, 0, 0},
						{0, 0, 10, 12, 0, 0},
						{0, 4, 0, 0, 14, 0},
						{0, 0, 9, 0, 0, 20},
						{0, 0, 0, 7, 0, 4},
						{0, 0, 0, 0, 0, 0}
	};
	

	cout << "The maximum possible flow is " << graph::Ford_Fulkerson(mat, 0, 5, 6);


	
}