bool hasAug(){ queue<int> q; vi vis(V+1,0); P.assign(V+1,NULL); q.push(S); vis[S] = 1; while(!q.empty()){ int u = q.front();q.pop(); if(u == T){ break; } for (int i = 0; i < G[u].size(); ++i) { Edge * e = G[u][i]; int v = e->other(u); if(!vis[v] && e->resTo(v)>0){ q.push(v); vis[v] = 1; P[v] = e; } } } return P[T] != NULL; }
bool augmentingPath(){ queue<int> q; parent.assign(MAX_V, NULL); visited.assign(MAX_V, 0); q.push(SRC); visited[SRC] = 1; while(!q.empty()){ int u = q.front(); q.pop(); if(u == SINK) break; for (int i = 0; i < adj[u].size(); ++i) { Edge* e = adj[u][i]; int v = e->other(u); if(!visited[v] && e->residualTo(v) > 0){ visited[v] = 1; parent[v] = e; q.push(v); } } } return visited[SINK]; }
bool hasAugmentingPath(){ queue<int> q; vi vist(t+1, 0); vist[s] = 1; q.push(s); p.assign(t+1, NULL); // printf("s' = %d\n", s); while(!q.empty()){ int u = q.front(); q.pop(); // printf("u = /%d\n", u); if(u == t)break; for (int i = 0; i < G[u].size(); ++i) { Edge *e = G[u][i]; int v = e->other(u); if(!vist[v] && e->retTo(v) > 0){ q.push(v); vist[v] = 1; p[v] = e; } } } return p[t] != NULL; }
int main(){ while (cin >> n){ if (n == 0) break; cin >> m; V = 50001 + n + 2; s = 0; t = 50001 + n; mf = total = 0; G.assign(V,edgeList()); for (int i = 0; i < n; i++){ cin >> c >> a >> b; for (int j = a; j < b; j++){ addEdge(j, 50001 + i, 1); addEdge(s, j, m); } addEdge(50001 + i, t, c); } while (1) { vi dist(V, INF); dist[s] = 0; queue<int> q; q.push(s); P.assign(V, NULL); while (!q.empty()){ int u = q.front(); q.pop(); cout << "u: " << u <<endl; if (u == t) break; for (int j = 0; j < G[u].size(); j++){ Edge * e = G[u][j]; int v = e->other(u); if (e->cap > 0 && dist[v] == INF){ dist[v] = dist[u] + 1, q.push(v), P[v] = e; } } } int min_res = INF; for(int v = t; v!= s;v = P[v]->other(v)){ min_res = min(min_res, P[v]->cap); } cout << "f: " << min_res << endl; // // Add flow to the residual graph for(int v = t; v!= s;v = P[v]->other(v)){ G[P[v]->other(v)][v]->cap -= min_res; } if (min_res == 0) break; mf += min_res; } cout << mf << endl; } return 0; }
/** * Returns true if there is an augmenting path * stores augmenting path in P */ bool bellmanFord(){ vector<double> dist(N,INF); P.assign(N, NULL); dist[S] = 0; for (int i = 0; i < N-1; ++i) { for (int u = 0; u < N; ++u) { for (int k = 0; k < G[u].size(); ++k) { Edge * e = G[u][k]; int v = e->other(u); // Relax edge if(e->resTo(v) > 0 && dist[v] > dist[u] + e->getCost(v)){ dist[v] = dist[u] + e->getCost(v); // Parent of v is in edge 'e' P[v] = e; } } } } return P[T] != NULL; }