Poj3469 Dual Core CPU --- minimum cut

Source: Internet
Author: User

Poj3469 Dual Core CPU --- minimum cut

A cpu has two cores. To put n modules on one core, the cost of putting them on different cores is given.

In addition, the m pair module is provided. If it is not placed in the same module, additional costs will be incurred. Minimum cost.


For each module, you can select core 1, Core 2, and connected modules.

Based on this, the edge is built, the core 1 is the source point, the Core 2 is the sink point, and the two-way edge is built between the connected modules, and the edge weight is the cost. Find the minimum cut.


#include 
 
  #include 
  
   #include 
   
    #include 
    
     #include 
     
      #include #include 
      
       #include 
       
        #include 
        #define inf 0x3f3f3f3f#define eps 1e-6#define ll __int64const int maxn=20010;using namespace std;struct node{ int from,to,cap,flow;};struct dinic{ int n,m,s,t; vector
         
           e; vector
          
            g[maxn]; bool vis[maxn]; int d[maxn]; int cur[maxn]; void init(int n) { e.clear(); for(int i=0;i<=n+2;i++) g[i].clear(); } void addedge(int a,int b,int c,int d) { e.push_back((node){a,b,c,0}); e.push_back((node){b,a,d,0}); m=e.size(); g[a].push_back(m-2); g[b].push_back(m-1); } bool bfs() { memset(vis,0,sizeof vis); queue
           
             q; q.push(s); d[s]=0; vis[s]=1; while(!q.empty()) { int x=q.front();q.pop(); for(int i=0;i
            
             ee.flow) { vis[ee.to]=1; d[ee.to]=d[x]+1; q.push(ee.to); } } } return vis[t]; } int dfs(int x,int a) { if(x==t||a==0) return a; int flow=0,f; for(int& i=cur[x];i
             
              0) { ee.flow+=f; e[g[x][i]^1].flow-=f; flow+=f; a-=f; if(a==0) break; } } return flow; } int maxflow(int s,int t) { this->s=s; this->t=t; int flow=0; while(bfs()) { memset(cur,0,sizeof cur); flow+=dfs(s,inf); } return flow; }};dinic solve;int main(){ int i,a,b,cc,m,n,s,t; while(~scanf("%d%d",&n,&m)) { s=0,t=n+1; solve.init(n); for(i=1;i<=n;i++) { scanf("%d%d",&a,&b); solve.addedge(s,i,a,0); solve.addedge(i,t,b,0); } while(m--) { scanf("%d%d%d",&a,&b,&cc); solve.addedge(a,b,cc,cc); } printf("%d\n",solve.maxflow(s,t)); } return 0;}
             
            
           
          
         
       
      
     
    
   
  
 


Related Article

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.