POJ 3680 Intervals (cost stream + discretization)

Source: Internet
Author: User

POJ 3680 Intervals (cost stream + discretization)

Address: POJ 3680

I can't really figure out this question. Creating images is still not open-minded and bold enough.

To solve this problem, we must first discretization the coordinates. You can use the left vertex to issue an edge to the right vertex. The capacity is 1 and the cost is a negative weight. Then, from left to right, the adjacent two points are connected sequentially. The weight is 0 and the capacity is k. That is to say, if this interval is selected, it will flow from the edge with a negative charge. Otherwise, it will flow from the edge with a zero charge. Then a super source is established to connect to the leftmost vertex. The weight is 0 and the capacity is k. This ensures that the number of overlaps is k, because the intervals passing through the augmented path must not overlap, and the traffic is only k, the problem is satisfied.

The Code is as follows:

#include 
 
  #include 
  
   #include 
   
    #include 
    
     #include 
     
      #include 
      
       #include 
       
        #include 
        
         #include 
         #include 
          
           #include using namespace std;const int INF=0x3f3f3f3f;int head[1000], source, sink, cnt, fei[10000], q[10000], flow, cost;int d[1000], vis[1000], cur[1000], f[1000];struct node{ int u, v, cap, cost, next;}edge[1000000];void add(int u, int v, int cap, int cost){ edge[cnt].v=v; edge[cnt].cap=cap; edge[cnt].cost=cost; edge[cnt].next=head[u]; head[u]=cnt++; edge[cnt].v=u; edge[cnt].cap=0; edge[cnt].cost=-cost; edge[cnt].next=head[v]; head[v]=cnt++;}int spfa(){ memset(d,INF,sizeof(d)); memset(vis,0,sizeof(vis)); queue
           
            q; q.push(source); d[source]=0; f[source]=INF; cur[source]=-1; while(!q.empty()) { int u=q.front(); q.pop(); vis[u]=0; for(int i=head[u];i!=-1;i=edge[i].next) { int v=edge[i].v; if(d[v]>d[u]+edge[i].cost&&edge[i].cap) { d[v]=d[u]+edge[i].cost; f[v]=min(f[u],edge[i].cap); cur[v]=i; if(!vis[v]) { vis[v]=1; q.push(v); } } } } if(d[sink]==INF) return 0; flow+=f[sink]; cost-=f[sink]*d[sink]; for(int i=cur[sink];i!=-1;i=cur[edge[i^1].v]) { edge[i].cap-=f[sink]; edge[i^1].cap+=f[sink]; } return 1;}void mcmf(){ flow=cost=0; while(spfa()) ; printf("%d\n",cost);}int get(int x, int high){ int low=1, mid; while(low<=high) { mid=low+high>>1; if(fei[mid]==x) return mid; else if(fei[mid]>x) high=mid-1; else low=mid+1; }}int main(){ int t, n, k, i, tot, a[300], b[300], max1, j, c[300]; scanf("%d",&t); while(t--) { scanf("%d%d",&n,&k); memset(head,-1,sizeof(head)); cnt=0; source=0; tot=1; max1=-1; for(i=1;i<=n;i++) { scanf("%d%d%d",&a[i],&b[i],&c[i]); q[2*i-1]=a[i]; q[2*i]=b[i]; } sort(q+1,q+2*n+1); fei[1]=q[1]; for(i=2;i<=2*n;i++) { if(q[i]!=q[i-1]) fei[++tot]=q[i]; } sink=tot+1; add(source,1,k,0); for(i=1;i<=n;i++) { int l=get(a[i],tot); int r=get(b[i],tot); add(l,r,1,-c[i]); //printf("%d %d\n",l,r); } for(i=2;i<=tot;i++) { add(i-1,i,k,0); } add(tot,sink,k,0); mcmf(); } return 0;}
           
          
        
       
      
     
    
   
  
 


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.