Advertisement
Guest User

Untitled

a guest
May 21st, 2018
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 15.25 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <queue>
  4. #include <fstream>
  5. using namespace std;
  6.  
  7. const int inf = 1e9;
  8.  
  9. vector<int> r;
  10. vector<int> d;
  11. vector<int> p;
  12. vector<int> w;
  13. int n;
  14. int t;
  15.  
  16. int penalty(int time, int j)
  17. {
  18. if (time>d[j])
  19. return w[j];
  20. return 0;
  21. }
  22.  
  23. void gen(int nn)
  24. {
  25. int mod = 10;
  26. int wmod = 10;
  27. n = nn;
  28. t = rand()%mod+1;
  29. r.resize(n);
  30. d.resize(n);
  31. p.resize(n);
  32. w.resize(n);
  33. for (int i = 0; i<n; i++)
  34. {
  35. d[i] = p[i] = w[i] = r[i] = 0;
  36. if (i)
  37. r[i] = r[i-1];
  38. r[i] = r[i] + rand()%mod;
  39.  
  40. d[i] = r[i] + rand()%mod+1;
  41. while (i && d[i]<d[i-1])
  42. d[i] = r[i] + rand()%mod+1;
  43.  
  44. p[i] = rand()%(d[i]-r[i])+1;
  45. w[i] = rand()%wmod;
  46. }
  47. }
  48. void read()
  49. {
  50. cin >> n >> t;
  51. r.resize(n);
  52. for (int i = 0; i<n; i++)
  53. cin >> r[i];
  54. d.resize(n);
  55. for (int i = 0; i<n; i++)
  56. cin >> d[i];
  57. p.resize(n);
  58. for (int i = 0; i<n; i++)
  59. cin >> p[i];
  60. w.resize(n);
  61. for (int i = 0; i<n; i++)
  62. cin >> w[i];
  63.  
  64. }
  65. bool readfromfile()
  66. {
  67. cout << "Введите путь к файлу: ";
  68. string s;
  69. cin >> s;
  70. ifstream fin(s);
  71. if (!(fin >> n >> t))
  72. return false;
  73. r.resize(n);
  74. for (int i = 0; i<n; i++)
  75. if (!(fin >> r[i]))
  76. return false;
  77. d.resize(n);
  78. for (int i = 0; i<n; i++)
  79. if (!(fin >> d[i]))
  80. return false;
  81. p.resize(n);
  82. for (int i = 0; i<n; i++)
  83. if (!(fin >> p[i]))
  84. return false;
  85. w.resize(n);
  86. for (int i = 0; i<n; i++)
  87. if (!(fin >> w[i]))
  88. return false;
  89. return true;
  90. }
  91.  
  92. int calcT(vector<int> &v, int t, int without)
  93. {
  94. int curtime = t;
  95. for (int i = 0; i<v.size(); i++)
  96. {
  97. if (v[i] == without)
  98. continue;
  99.  
  100. curtime = max(curtime,r[v[i]]);
  101. curtime+= p[v[i]];
  102.  
  103. }
  104. return curtime;
  105. }
  106. pair<vector<int>,vector<int>> algo()
  107. {
  108. int curtime = t;
  109. vector<int> late,notlate;
  110. queue<int> todo;
  111. for (int i = 0; i<n; i++)
  112. todo.push(i);
  113. while (!todo.empty())
  114. {
  115.  
  116. int i = todo.front();
  117. curtime = max(curtime,r[i]);
  118. if (curtime+p[i]<=d[i]) // если не запаздывает добавляем в множество незапаздывающих
  119. {
  120. notlate.push_back(i);
  121. curtime+=p[i];
  122. todo.pop();
  123. }
  124. else
  125. {
  126. int m = i;
  127. int mintime = INT_MAX;
  128. notlate.push_back(i);
  129. for (int j = 0; j<notlate.size(); j++)
  130. {
  131. int calct = calcT(notlate,t,notlate[j]);
  132. if (calct<mintime)
  133. {
  134. m = j;
  135. mintime = calct;
  136. }
  137. }
  138. notlate.pop_back();
  139. if (notlate[m] == i)
  140. {
  141. late.push_back(i);
  142. todo.pop();
  143. }
  144. else
  145. {
  146. late.push_back(notlate[m]);
  147. notlate.erase(notlate.begin()+m);
  148. curtime = calcT(notlate, t, inf);
  149. }
  150. }
  151. }
  152. return {notlate,late};
  153. }
  154. int calcF(vector<int> &late)
  155. {
  156. int ans = 0;
  157. for (auto it:late)
  158. ans+=w[it];
  159. return ans;
  160. }
  161. void update(pair<vector<int>,vector<int>> &best, vector<int>&late, vector<int> &notlate)
  162. {
  163. if (best.first.size()==0 && best.second.size()==0)
  164. {
  165. best = {notlate,late};
  166. return;
  167. }
  168. if (calcF(best.second)>calcF(late))
  169. {
  170. best = {notlate,late};
  171. }
  172. }
  173.  
  174. pair<vector<int>,vector<int>> bestSol()
  175. {
  176. vector<int> perm;
  177. for (int i = 0; i<n; i++)
  178. {
  179. perm.push_back(i);
  180. }
  181.  
  182. pair<vector<int>,vector<int>> best;// первое значение незапаздывающие второе запаздывающие
  183. do
  184. {
  185. int curtime = t;
  186. vector<int> late,notlate;
  187. for (auto it:perm)
  188. {
  189. int i = it;
  190. curtime = max(curtime,r[i]);
  191. if (curtime+p[i]<=d[i])
  192. {
  193. notlate.push_back(i);
  194. curtime+=p[i];
  195. }
  196. else
  197. {
  198. late.push_back(i);
  199. }
  200. }
  201. update(best,late,notlate);
  202. }
  203. while (next_permutation(perm.begin(),perm.end()));
  204. return best;
  205. }
  206.  
  207. pair<vector<int>,vector<int>> algo1()// не учитываем веса, если не нашлось требования удовлетворяющее двум условиям
  208. {
  209. int curtime = t; // tk
  210. vector<int> late,notlate;// П1, П2
  211. queue<int> todo;// Nk
  212. for (int i = 0; i<n; i++)
  213. todo.push(i);
  214. while (!todo.empty())
  215. {
  216.  
  217. int i = todo.front();
  218. curtime = max(curtime,r[i]);
  219. if (curtime+p[i]<=d[i]) // если не запаздывает добавляем в множество незапаздывающих
  220. {
  221. notlate.push_back(i);
  222. curtime+=p[i];
  223. todo.pop();// удаление из очереди
  224. }
  225. else
  226. {
  227. int mintimeind = 0;
  228. int minwind = 0;
  229. notlate.push_back(i); // добавить конец
  230. vector<pair<int,int>> forcalc(notlate.size());
  231.  
  232. for (int j = 0; j<notlate.size(); j++)
  233. {
  234. forcalc[j] = {calcT(notlate,t,notlate[j]),w[notlate[j]]};
  235. if (forcalc[mintimeind].first>forcalc[j].first)
  236. mintimeind = j;
  237. if (forcalc[minwind].second>forcalc[j].second)
  238. minwind = j;
  239. }
  240.  
  241. int m = -1;
  242.  
  243. for(int j = 0; j<notlate.size(); j++)
  244. if (forcalc[j].first == forcalc[mintimeind].first && forcalc[j].second == forcalc[minwind].second)
  245. m = j;
  246.  
  247. if (m==-1)// тут мы не смогли найти требование которое удовлетворяет двум условиям, берем то у которого минимальное время и среди всех таких мин вес
  248. {
  249. m = mintimeind;
  250. for (int j = 0; j<notlate.size(); j++)
  251. if (forcalc[j].first == forcalc[m].first && forcalc[j].second<forcalc[m].second)
  252. m = j;
  253. }
  254.  
  255. if (notlate[m] == i)
  256. {
  257. notlate.pop_back();// удалить с конца
  258.  
  259. late.push_back(i);
  260. todo.pop();
  261. }
  262. else
  263. {
  264. notlate.pop_back();// удалить с конца
  265.  
  266. late.push_back(notlate[m]);
  267. notlate.erase(notlate.begin()+m);
  268. curtime = calcT(notlate, t, inf);
  269. }
  270. }
  271. }
  272. return {notlate,late};
  273. }
  274.  
  275.  
  276. pair<vector<int>,vector<int>> algo2()// учитываем веса, если не нашлось требования удовлетворяющее двум условиям
  277. {
  278. int curtime = t;
  279. vector<int> late,notlate;
  280. queue<int> todo;
  281. for (int i = 0; i<n; i++)
  282. todo.push(i);
  283. while (!todo.empty())
  284. {
  285.  
  286. int i = todo.front();
  287. curtime = max(curtime,r[i]);
  288. if (curtime+p[i]<=d[i]) // если не запаздывает добавляем в множество незапаздывающих
  289. {
  290. notlate.push_back(i);
  291. curtime+=p[i];
  292. todo.pop();
  293. }
  294. else
  295. {
  296. int mintimeind = 0;
  297. int minwind = 0;
  298. notlate.push_back(i);
  299. vector<pair<int,int>> forcalc(notlate.size());
  300.  
  301. for (int j = 0; j<notlate.size(); j++)
  302. {
  303. forcalc[j] = {calcT(notlate,t,notlate[j]),w[notlate[j]]};
  304. if (forcalc[mintimeind].first>forcalc[j].first)
  305. mintimeind = j;
  306. if (forcalc[minwind].second>forcalc[j].second)
  307. minwind = j;
  308. }
  309.  
  310. int m = -1;
  311.  
  312. for(int j = 0; j<notlate.size(); j++)
  313. if (forcalc[j].first == forcalc[mintimeind].first && forcalc[j].second == forcalc[minwind].second)
  314. m = j;
  315. if (m==-1)// тут мы не смогли найти требование которое удовлетворяет двум условиям, берем то у которого мин вес и среди всех таких мин время
  316. {
  317. m = minwind;
  318. for (int j = 0; j<notlate.size(); j++)
  319. if (forcalc[j].second == forcalc[m].second && forcalc[j].first<forcalc[m].first)
  320. m = j;
  321. }
  322.  
  323. if (notlate[m] == i)
  324. {
  325. notlate.pop_back();
  326.  
  327. late.push_back(i);
  328. todo.pop();
  329. }
  330. else
  331. {
  332. notlate.pop_back();
  333.  
  334. late.push_back(notlate[m]);
  335. notlate.erase(notlate.begin()+m);
  336. curtime = calcT(notlate, t, inf);
  337. }
  338. }
  339. }
  340. return {notlate,late};
  341. }
  342.  
  343. bool issled = false;
  344. int bettersolution[2];
  345. void solve(int disp)
  346. {
  347. // debug();
  348. auto ans = algo();
  349. if (disp)
  350. {
  351. cout << "Результат работы алгоритма из лекции\n";
  352. cout << "Незапаздывающие: ";
  353. for (auto it: ans.first)
  354. cout << it+1 << " ";
  355. cout << endl << "Запаздывающие: ";
  356. for (auto it:ans.second)
  357. cout << it+1 << " ";
  358.  
  359. cout << "\nСумма весов запаздывающих: " << calcF(ans.second) << endl << endl;
  360. }
  361.  
  362. auto ans1 = algo1();
  363. if (disp)
  364. {
  365. cout << "Результат работы алгоритма с учетом первого условия\n";
  366. cout << "Незапаздывающие: ";
  367. for (auto it: ans1.first)
  368. cout << it+1 << " ";
  369. cout << endl << "Запаздывающие: ";
  370. for (auto it:ans1.second)
  371. cout << it+1 << " ";
  372.  
  373. cout << "\nСумма весов запаздывающих: " << calcF(ans1.second) << endl << endl;
  374. }
  375. auto ans3 = algo2();
  376. if (disp)
  377. {
  378. cout << "Результат работы алгоритма с учетом второго условия\n";
  379. cout << "Незапаздывающие: ";
  380. for (auto it: ans3.first)
  381. cout << it+1 << " ";
  382. cout << endl << "Запаздывающие: ";
  383. for (auto it:ans3.second)
  384. cout << it+1 << " ";
  385.  
  386. cout << "\nСумма весов запаздывающих: " << calcF(ans3.second) << endl << endl;
  387.  
  388. }
  389. if (n<10)
  390. {
  391. auto ans2 = bestSol();
  392. if (disp)
  393. {
  394. cout << "Результат работы точного алгоритма\n";
  395. cout << "Незапаздывающие: ";
  396. for (auto it: ans2.first)
  397. cout << it+1 << " ";
  398. cout << endl << "Запаздывающие: ";
  399. for (auto it:ans2.second)
  400. cout << it+1 << " ";
  401.  
  402. cout << "\nСумма весов запаздывающих: " << calcF(ans2.second) << endl;
  403. }
  404. }
  405. if (issled)
  406. {
  407. int second = calcF(ans3.second);
  408. int first = calcF(ans1.second);
  409. int best = min(first,second);
  410. if (first == best)
  411. {
  412. bettersolution[0]++;
  413. if (disp)
  414. {
  415. cout << "Алгоритм с учетом первого условия хорошо отработал для данной задачи\n";
  416. }
  417. }
  418. if (second == best)
  419. {
  420. bettersolution[1]++;
  421. if (disp)
  422. {
  423. cout << "Алгоритм с учетом второго условия хорошо отработал для данной задачи\n";
  424. }
  425. }
  426. }
  427.  
  428. }
  429.  
  430. void display()
  431. {
  432. cout << "N = " << n << " t = "<< t << endl;
  433. cout << "r = ";
  434. for (int i = 0; i<n; i++)
  435. cout << r[i] << " ";
  436. cout << endl;
  437. cout << "d = ";
  438. for (int i = 0; i<n; i++)
  439. cout << d[i] << " ";
  440. cout << endl;
  441. cout << "p = ";
  442.  
  443. for (int i = 0; i<n; i++)
  444. cout << p[i] << " ";
  445. cout << endl;
  446. cout << "w = ";
  447. for (int i = 0; i<n; i++)
  448. cout << w[i] << " ";
  449. cout << endl;
  450. }
  451. int get_command()
  452. {
  453. int x = -1;
  454. while (x<0 || x>3)
  455. {
  456. cout << "Введите 1 для проведения иследование\n 2 для ввода данных с файла\n 3 для ввода данных вручную\n 0 для выхода\n";
  457. cin >> x;
  458. }
  459. return x;
  460. }
  461. void make_isledovanie()
  462. {
  463. issled = true;
  464. cout << "Введите количество тестов: ";
  465. int tests;
  466. cin >> tests;
  467.  
  468. int disp = 2;
  469. while (disp!=0 && disp!=1)
  470. {
  471. cout << "Для просмотра решения каждого примера нажмите 1, иначе 0: ";
  472. cin >> disp;
  473. }
  474.  
  475. int nn = 25;// размерность количества требования при генерации (N) ЭТО НАДО МЕНЯТЬ ДЛЯ ИССЛЕДОВАНИЯ
  476. bettersolution[0] = bettersolution[1] = 0;
  477.  
  478.  
  479. for (int i = 0; i<tests; i++)
  480. {
  481. // if(i%5==0)
  482. // nn+=5;
  483. gen(nn);
  484. if (disp)
  485. display();
  486. solve(disp);
  487. }
  488. cout << "Алгоритм с учетом первого условия для размерности " << nn << " хорошо отработал для "<< bettersolution[0]*100.0/tests<<"% задач\n";
  489. cout << "Алгоритм с учетом второго условия для размерности " << nn << " хорошо отработал для "<< bettersolution[1]*100.0/tests<<"% задач\n";
  490. cout << endl;
  491.  
  492. issled = false;
  493. }
  494.  
  495. int main()
  496. {
  497. srand(time(0));
  498. while (true) {
  499. int x = get_command();
  500. if (x==0)
  501. break;
  502. if (x==1)
  503. {
  504. make_isledovanie();
  505. }
  506. else if (x == 2)
  507. {
  508. bool readed = readfromfile();
  509. if (!readed)
  510. {
  511. cout << "Неправильный путь или неверные входные данные в файле. Повторите еще раз";
  512. continue;
  513. }
  514. display();
  515. solve(1);
  516. }
  517. else
  518. {
  519. read();
  520. solve(1);
  521. }
  522. }
  523.  
  524. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement