#include <cstdio> #include <map> #include <vector> #include <deque> #include <algorithm> #include <cassert> #define DEBUG false struct task { int id; int start, end, duration; }; struct edge { int used; int end; edge(int end_) : used(false), end(end_) { }; }; struct vertex { bool visited; int capacity; std::vector<int> edges; }; int n, m; bool powieksz(std::vector<vertex> &graph, int &v_id, int from_id = -1) { vertex &v = graph[v_id]; if (v.visited) return false; v.visited = true; if (v.capacity > 0 && v_id >= n && from_id >= 0) { v.edges.push_back(from_id); v.capacity--; // printf("path: %d<-", v_id - n); return true; } //auto cmp_capacity = [graph](int a, int b) { return graph[a].capacity > graph[b].capacity; }; //std::sort(v.edges.begin(), v.edges.end(), cmp_capacity); for (auto eit = v.edges.begin(); eit != v.edges.end(); ++eit) { if (graph[*eit].capacity > 0 && powieksz(graph, *eit, v_id)) { v.edges.erase(eit); if (from_id > 0) v.edges.push_back(from_id); // printf("%d<- ", (v_id >= n ? v_id-n : v_id)); return true; } } for (auto eit = v.edges.begin(); eit != v.edges.end(); ++eit) { if (graph[*eit].capacity == 0 && powieksz(graph, *eit, v_id)) { v.edges.erase(eit); if (from_id > 0) v.edges.push_back(from_id); // printf("%d<- ", (v_id >= n ? v_id-n : v_id)); return true; } } return false; } int main() { scanf("%d %d", &n, &m); int end_of_time = 0; std::vector<task> tasks(n); tasks.reserve(n); for (int i = 0; i < n; ++i) { task t; t.id = i; scanf("%d %d %d", &t.start, &t.end, &t.duration); tasks.emplace_back(t); end_of_time = std::max(end_of_time, t.end); } std::vector<vertex> graph(n + end_of_time + 1); for (int i = n; i < n + end_of_time; ++i) graph[i].capacity = m; for (auto &&t : tasks) { graph[t.id].capacity = t.duration; for (int j = t.start; j < t.end; ++j) { graph[t.id].edges.emplace_back(n + j); } } int counter = 0; for (auto &&t : tasks) counter += t.duration; // printf("counter: %d\n", --counter); bool improved = true; bool all_scheduled = false; while (improved && !all_scheduled) { improved = false; all_scheduled = true; for (int i = 0; i < n; ++i) { auto &v = graph[i]; if (DEBUG) { printf("start v: %d (%d) e:", i, v.capacity); for (int e : v.edges) printf(" %d", e - n); printf("\n"); } for (auto &&v : graph) v.visited = false; while (v.capacity > 0) { if (powieksz(graph, i)) { improved = true; v.capacity--; // printf("\n"); --counter; } else break; } if (v.capacity > 0) all_scheduled = false; if (DEBUG) { printf("end v: %d (%d) e:", i, v.capacity); for (int e : v.edges) printf(" %d", e - n); printf("\n"); } // printf("counter: %d\n", --counter); } if (DEBUG) { printf("END all: %d improved: %d\n", all_scheduled, improved); printf("%100s\n", "---"); } } if (all_scheduled && improved) printf("TAK\n"); else printf("NIE\n"); return 0; }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 | #include <cstdio> #include <map> #include <vector> #include <deque> #include <algorithm> #include <cassert> #define DEBUG false struct task { int id; int start, end, duration; }; struct edge { int used; int end; edge(int end_) : used(false), end(end_) { }; }; struct vertex { bool visited; int capacity; std::vector<int> edges; }; int n, m; bool powieksz(std::vector<vertex> &graph, int &v_id, int from_id = -1) { vertex &v = graph[v_id]; if (v.visited) return false; v.visited = true; if (v.capacity > 0 && v_id >= n && from_id >= 0) { v.edges.push_back(from_id); v.capacity--; // printf("path: %d<-", v_id - n); return true; } //auto cmp_capacity = [graph](int a, int b) { return graph[a].capacity > graph[b].capacity; }; //std::sort(v.edges.begin(), v.edges.end(), cmp_capacity); for (auto eit = v.edges.begin(); eit != v.edges.end(); ++eit) { if (graph[*eit].capacity > 0 && powieksz(graph, *eit, v_id)) { v.edges.erase(eit); if (from_id > 0) v.edges.push_back(from_id); // printf("%d<- ", (v_id >= n ? v_id-n : v_id)); return true; } } for (auto eit = v.edges.begin(); eit != v.edges.end(); ++eit) { if (graph[*eit].capacity == 0 && powieksz(graph, *eit, v_id)) { v.edges.erase(eit); if (from_id > 0) v.edges.push_back(from_id); // printf("%d<- ", (v_id >= n ? v_id-n : v_id)); return true; } } return false; } int main() { scanf("%d %d", &n, &m); int end_of_time = 0; std::vector<task> tasks(n); tasks.reserve(n); for (int i = 0; i < n; ++i) { task t; t.id = i; scanf("%d %d %d", &t.start, &t.end, &t.duration); tasks.emplace_back(t); end_of_time = std::max(end_of_time, t.end); } std::vector<vertex> graph(n + end_of_time + 1); for (int i = n; i < n + end_of_time; ++i) graph[i].capacity = m; for (auto &&t : tasks) { graph[t.id].capacity = t.duration; for (int j = t.start; j < t.end; ++j) { graph[t.id].edges.emplace_back(n + j); } } int counter = 0; for (auto &&t : tasks) counter += t.duration; // printf("counter: %d\n", --counter); bool improved = true; bool all_scheduled = false; while (improved && !all_scheduled) { improved = false; all_scheduled = true; for (int i = 0; i < n; ++i) { auto &v = graph[i]; if (DEBUG) { printf("start v: %d (%d) e:", i, v.capacity); for (int e : v.edges) printf(" %d", e - n); printf("\n"); } for (auto &&v : graph) v.visited = false; while (v.capacity > 0) { if (powieksz(graph, i)) { improved = true; v.capacity--; // printf("\n"); --counter; } else break; } if (v.capacity > 0) all_scheduled = false; if (DEBUG) { printf("end v: %d (%d) e:", i, v.capacity); for (int e : v.edges) printf(" %d", e - n); printf("\n"); } // printf("counter: %d\n", --counter); } if (DEBUG) { printf("END all: %d improved: %d\n", all_scheduled, improved); printf("%100s\n", "---"); } } if (all_scheduled && improved) printf("TAK\n"); else printf("NIE\n"); return 0; } |