Poj 3680 maximum fee stream of Intervals
Question:
To give n an open range (ai, bi) and the corresponding weight wi, We need to select some ranges. We need to ensure that either of them cannot be overwritten by more than k ranges, and the goal is to maximize the total weight.
Analysis:
Convert to the maximum charge flow, and change the template of the minimum charge flow.
Code:
//poj 3680//sep9#include
#include
#include
#include using namespace std;const int maxN=2048;const int maxM=20024;const int inf=2000000000;struct Edge{int v,f,w,nxt;}e[4*maxM+10];int g[maxN+10];int nume,src,sink;queue
Q;bool inq[maxN+10];int dist[maxN+10];int prev[maxN+10],pree[maxN+10];int a[maxN],b[maxN],w[maxN];void addedge(int u,int v,int c,int w){e[++nume].v=v;e[nume].f=c;e[nume].w=w;e[nume].nxt=g[u];g[u]=nume;e[++nume].v=u;e[nume].f=0;e[nume].w=-w;e[nume].nxt=g[v];g[v]=nume;}bool find_path(){while(!Q.empty()) Q.pop();memset(dist,-1,sizeof(dist));memset(inq,false,sizeof(inq));Q.push(src);inq[src]=true;dist[src]=0;while(!Q.empty()){int u=Q.front();Q.pop();inq[u]=false;for(int i=g[u];i;i=e[i].nxt){if(e[i].f>0&&dist[u]+e[i].w>dist[e[i].v]){dist[e[i].v]=dist[u]+e[i].w;prev[e[i].v]=u;pree[e[i].v]=i;if(!inq[e[i].v]){Q.push(e[i].v);inq[e[i].v]=true;}}}}return dist[sink]==-1?false:true;}int max_cost_flow(int f){int res=0;while(f>0){if(find_path()==false)return -1;int d=f;for(int v=sink;v!=src;v=prev[v])d=min(d,e[pree[v]].f);f-=d;res+=d*dist[sink];for(int v=sink;v!=src;v=prev[v]){e[pree[v]].f-=d;e[pree[v]^1].f+=d;}}return res;}void init(){memset(g,0,sizeof(g));nume=1;}int main(){int cases;scanf("%d",&cases);while(cases--){init();int i,n,m,k;scanf("%d%d",&n,&k);vector
x;for(i=0;i