Poj 3680 maximum fee stream of Intervals

Source: Internet
Author: User

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
      
       

 

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.