POJ 2195 minimum charge flow

Source: Internet
Author: User

POJ 2195 minimum charge flow

Q: to give a messy square matrix, H Represents the home and m represents the people. Now everyone is going home and asking everyone the steps to go home and

Idea: I think of the cost flow very well. The cost is the minimum number of steps for people to go home, and the traffic is the number of people. If you connect to the edge, everyone will reach home with a capacity of 1, the cost is the edge of the number of steps. The super source point is connected to people. The capacity is 1, the cost is 0, and the home is connected to the super sink point. The same capacity is 1, and the fattening Hall is 0, the result is the minimum cost stream. PS: the entry question is quite simple .........

#include 
 
  #include 
  
   #include 
   
    #include 
    
     #include 
     
      #include 
      
       #includeusing namespace std;typedef long long ll;const int inf=0x3f3f3f3f;const int maxn=10010;typedef pair
       
         P;struct edge{ int to,cap,cost,rev; edge(); edge(int a,int b,int c,int d){to=a,cap=b,cost=c,rev=d;};};vector
        
         G[maxn];int h[maxn],dis[maxn],prevv[maxn],preve[maxn];void addedge(int st,int en,int cap,int cost){ G[st].push_back(edge(en,cap,cost,G[en].size())); G[en].push_back(edge(st,0,-cost,G[st].size()-1));}int min_cost_flow(int st,int en,int f){ int ans=0; memset(h,0,sizeof(h)); while(f>0){ priority_queue
         
          ,greater
          

>que; memset(dis,inf,sizeof(dis)); dis[st]=0;que.push(P(0,st)); while(!que.empty()){ P p=que.top();que.pop(); int v=p.second; if(dis[v] 0&&dis[e.to]>dis[v]+e.cost+h[v]-h[e.to]){ dis[e.to]=dis[v]+e.cost+h[v]-h[e.to]; prevv[e.to]=v; preve[e.to]=i; que.push(P(dis[e.to],e.to)); } } } if(dis[en]==inf) return -1; for(int 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.