Banker algorithm-Safe sequence full list implementation

Source: Internet
Author: User

#include <stdio.h>
#include <string.h>
#include <iostream>
#include <vector>
#include <algorithm>
#include <cmath>
#include <string>
#include <queue>
#include <climits>
#include <map>
#include <stack>
#include <list>
#include <set>
#include <ctime>
#include <cstdlib>
#include <sstream>
#define FILE_IN freopen ("Input.txt", "R", stdin)
#define MAX 100005
#define INF 0x3f3f3f3f
#define HASH 100019
#define MAX_C 100
#define MAP_IT (type1,type2) map<type1,type2>::iterator
using namespace Std;
#define LL Long Long
#define FF (X,y,i) for (int i=x;i<y;i++)
struct Process {
int num;
vector<int>max;
vector<int>occupied;
vector<int>need;
};
vector<int>ava;
vector<process>processlist;
BOOL Test (VECTOR&LT;INT&GT;PMU) {
Vector<process>temppro = processlist;
Vector<int>tempava = Ava;
for (int i = 0; i < pmu.size (); i++) {
int currentpro = Pmu[i];
for (int i = 0; i < temppro[currentpro].need.size (); i++) {
if (Temppro[currentpro].need[i] > Tempava[i])
return 0;
}
for (int i = 0; i < temppro[currentpro].occupied.size (); i++) {
Tempava[i] + = Temppro[currentpro].occupied[i];
}
}
return true;
}
int main () {
int numprocess, numres;
cout << "Please input number of processes" << Endl;
Cin >> Numprocess;
Processlist.resize (numprocess);
cout << "Please input the number of resource types" << Endl;
Cin >> Numres;
for (int i = 0; i < numprocess; i++)
{
Processlist[i].max.resize (Numres);
Processlist[i].occupied.resize (Numres);
Processlist[i].need.resize (Numres);
}
Ava.resize (Numres);
cout << kind of resource, please input it's number of remaining instances "<< Endl;
for (int i = 0; i < numres; i++)
CIN >> Ava[i];
cout << "For each process, input the occuied instance it owns by the order" << Endl;
for (int i = 0; i < numprocess; i++) {
cout << "Process" << I << ":" << Endl;
for (int j = 0; J < Numres; J + +) {
CIN >> Processlist[i].occupied[j];

}
}
cout << "For each process, input the Maxium instance it needs" << Endl;
for (int i = 0; i < numprocess; i++) {
cout << "Process" << I << ":" << Endl;
for (int j = 0; J < Numres; J + +) {
CIN >> Processlist[i].max[j];
PROCESSLIST[I].NEED[J] = processlist[i].max[j]-processlist[i].occupied[j];
}
}
vector<int>possiblepermu;
for (int i = 0; i < processlist.size (); i++) Possiblepermu.push_back (i);
while (Next_permutation (Possiblepermu.begin (), Possiblepermu.end ())) {
if (test (POSSIBLEPERMU)) {
for (int i = 0; i < possiblepermu.size (); i++)
cout << Possiblepermu[i] << "";
cout << Endl;
}

}


}

Banker algorithm-Safe sequence full list implementation

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.