Untitled

mail@pastecode.io avatar
unknown
plain_text
2 years ago
1.5 kB
2
Indexable
Never
#include<bits/stdc++.h>
 
using namespace std;


int main()
{
	int n = 5, m =3;

	int alloc[n][m] = {{0,1,0},{2,0,0},{3,0,2},{2,1,1},{0,0,2}};
	int maxNeed[n][m] = {{7,5,3},{3,2,2},{9,0,2},{2,2,2},{4,3,3}};

	int avail[m] = {3,3,2};

	int Rneed[n][m];
	int chk[n];

	for( int i = 0 ; i < n ;i++ )
	{
		chk[i] = 0;
	}

	cout<<"Need"<<endl;

	for( int i = 0 ; i < n ; i++ )
	{
		for( int j = 0 ; j < m ; j++ )
		{
			Rneed[i][j] = maxNeed[i][j] - alloc[i][j];
			cout<<Rneed[i][j]<<" ";
		}
		cout<<endl;
	}

	int ans[n];
	int ai = 0;


	cout<<"available"<<endl;

	for( int k = 0 ; k < n ; k++ )
	{
		for( int i = 0; i < n ; i++ )
		{
			int flag = 0;
			if( chk[i] == 0 )
			{
				for( int j = 0 ;j < m ; j++ )
				{
					if( Rneed[i][j] > avail[j] )
					{
						flag = 1;
						break;
					}
				}

				if( flag == 0 )
				{
					ans[ai] = i;
					ai++;
					chk[i] = 1;

					for( int j = 0 ; j < m ; j++ )
					{
						avail[j] += alloc[i][j];
						cout<<avail[j]<<" ";
					}
					cout<<endl;
				}
			}
		}
	}

	int safe = 1;
	for( int i = 0 ; i < n ; i++ )
	{
		if( chk[i] == 0 )
		{
			cout<<"The system is not safe"<<endl;
			safe = 0;
			break;
		}
	}

	if( safe == 1 )
	{
		for( int i = 0 ; i < n ; i++ )
		{
			if( i != n-1 )
			{
				cout<<"P"<<ans[i]+1<<"->";
			}
			else
			{
				cout<<"P"<<ans[i]+1<<endl;
			}
		}

	}




    return 0;
}