POJ 3670 Intervals (billing flow)

Source: Internet
Author: User
POJ 3680 Intervals

Question link

Question: given some intervals, each interval has a weight value, which is required to be overwritten. Each vertex can cover up to k times and ask how many weights can be obtained at most.

Train of Thought: Typical range k coverage problem, range edge capacity 1, cost-w, then other points adjacent to the two sides of the edge, capacity k, cost 0, just run the charge flow

Code:

# Include <cstdio> # include <cstring> # include <vector> # include <queue> # include <algorithm> using namespace std; const int MAXNODE = 100005; const int MAXEDGE = 2005; typedef int Type; const Type INF = 0x3f3f3f; struct Edge {int u, v; Type cap, flow, cost; Edge () {} Edge (int u, int v, Type cap, Type flow, Type cost) {this-> u = u; this-> v = v; this-> cap = cap; this-> flow = flow; this-> cost = cost ;}}; struct MCFC { Int n, m, s, t; Edge edges [MAXEDGE]; int first [MAXNODE]; int next [MAXEDGE]; int inq [MAXNODE]; Type d [MAXNODE]; int p [MAXNODE]; Type a [MAXNODE]; void init (int n) {this-> n = n; memset (first,-1, sizeof (first )); m = 0;} void add_Edge (int u, int v, Type cap, Type cost) {edges [m] = Edge (u, v, cap, 0, cost ); next [m] = first [u]; first [u] = m ++; edges [m] = Edge (v, u, 0, 0,-cost ); next [m] = first [v]; first [v] = m ++;} bool Bellmanford (int s, int t, Type & flow, Type & cost) {for (int I = 0; I <n; I ++) d [I] = INF; memset (inq, false, sizeof (inq); d [s] = 0; inq [s] = true; p [s] = s; a [s] = INF; queue <int> Q; Q. push (s); while (! Q. empty () {int u = Q. front (); Q. pop (); inq [u] = false; for (int I = first [u]; I! =-1; I = next [I]) {Edge & e = edges [I]; if (e. cap> e. flow & d [e. v]> d [u] + e. cost) {d [e. v] = d [u] + e. cost; p [e. v] = I; a [e. v] = min (a [u], e. cap-e. flow); if (! Inq [e. v]) {Q. push (e. v); inq [e. v] = true ;}}}if (d [t] = INF) return false; flow + = a [t]; cost + = d [t] * a [t]; int u = t; while (u! = S) {edges [p [u]. flow + = a [t]; edges [p [u] ^ 1]. flow-= a [t]; u = edges [p [u]. u;} return true;} Type Mincost (int s, int t) {Type flow = 0, cost = 0; while (bellmanford (s, t, flow, cost )); return cost ;}} gao; const int N = 405; int t, n, k, save [N], sn; int main () {scanf ("% d ", & t); while (t --) {scanf ("% d", & n, & k); int l, r, w; sn = 0; gao. init (100005); while (n --) {scanf ("% d", & l, & r, & w ); save [sn ++] = l; save [sn ++] = r; gao. add_Edge (l, r, 1,-w);} save [sn ++] = 0; save [sn ++] = 100001; sort (save, save + sn ); for (int I = 1; I <sn; I ++) gao. add_Edge (save [I-1], save [I], k, 0); printf ("% d \ n",-gao. mincost (0, 100001);} return 0 ;}


POJ 3670 Intervals (billing flow)

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.