设为首页 加入收藏

TOP

poj3469 Dual Core CPU --- 最小割
2015-07-20 18:01:41 来源: 作者: 【 】 浏览:2
Tags:poj3469 Dual Core CPU --- 最小

一个CPU有两个核,要把n个模块放在其中一个核上,给出放在不同核上的花费。

另给出m对模块,若不放在同一模块则会产生额外花费。求最小花费。


对于每一个模块可以选择核1,核2,和相连的模块。

据此建边,核1为源点,核2为汇点,相连的模块之间建双向边,边权均为花费。求最小割即可。


#include 
  
   
#include 
   
     #include 
    
      #include 
     
       #include 
      
        #include 
       
         #include 
        
          #include 
         
           #include 
           #define inf 0x3f3f3f3f #define eps 1e-6 #define ll __int64 const 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; } 
               
              
             
            
           
         
        
       
      
     
    
   
  


】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇Apache反向代理结合Tomcat集群来.. 下一篇HDU 1455――Sticks(神棍)

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容: