zoukankan      html  css  js  c++  java
  • luogu4016 负载平衡问题

    网络流不用动脑子的好爽啊

    #include <iostream>
    #include <cstring>
    #include <cstdio>
    #include <queue>
    using namespace std;
    int n, uu, hea[105], cnt, ss, tt, sum, minCost, pre[105], dis[105];
    const int oo=0x3f3f3f3f;
    bool vis[105];
    queue<int> d;
    struct Edge{
    	int too, nxt, val, cst;
    }edge[805];
    void add_edge(int fro, int too, int val, int cst){
    	edge[cnt].nxt = hea[fro];
    	edge[cnt].too = too;
    	edge[cnt].val = val;
    	edge[cnt].cst = cst;
    	hea[fro] = cnt++;
    }
    void addEdge(int fro, int too, int val, int cst){
    	add_edge(fro, too, val, cst);
    	add_edge(too, fro, 0, -cst);
    }
    bool spfa(){
    	memset(dis, 0x3f, sizeof(dis));
    	memset(pre, -1, sizeof(pre));
    	dis[ss] = 0;
    	d.push(ss);
    	vis[ss] = true;
    	while(!d.empty()){
    		int x=d.front();
    		d.pop();
    		vis[x] = false;
    		for(int i=hea[x]; i!=-1; i=edge[i].nxt){
    			int t=edge[i].too;
    			if(dis[t]>dis[x]+edge[i].cst && edge[i].val>0){
    				dis[t] = dis[x] + edge[i].cst;
    				pre[t] = i;
    				if(!vis[t]){
    					vis[t] = true;
    					d.push(t);
    				}
    			}
    		}
    	}
    	return dis[tt]!=oo;
    }
    void f(){
    	while(spfa()){
    		int tmp=oo;
    		for(int i=pre[tt]; i!=-1; i=pre[edge[i^1].too])
    			tmp = min(tmp, edge[i].val);
    		for(int i=pre[tt]; i!=-1; i=pre[edge[i^1].too]){
    			edge[i].val -= tmp;
    			edge[i^1].val += tmp;
    			minCost += tmp * edge[i].cst;
    		}
    	}
    }
    int main(){
    	memset(hea, -1, sizeof(hea));
    	cin>>n;
    	ss = 0; tt = n + 1;
    	for(int i=1; i<=n; i++){
    		scanf("%d", &uu);
    		addEdge(ss, i, uu, 0);
    		sum += uu;
    	}
    	sum /= n;
    	for(int i=1; i<=n; i++){
    		addEdge(i, tt, sum, 0);
    		if(i>1)	addEdge(i, i-1, oo, 1);
    		if(i<n)	addEdge(i, i+1, oo, 1);
    	}
    	addEdge(1, n, oo, 1);
    	addEdge(n, 1, oo, 1);
    	f();
    	cout<<minCost<<endl;
    	return 0;
    }
    
  • 相关阅读:
    『ORACLE』 DBLINK(11g)
    『ORACLE』 对永久表空间进行DDL操作(11g)
    『ORACLE』 对永久表空间进行DML操作(11g)
    『ORACLE』 数据库suspend模式(11g)
    『ORACLE』 数据库quiesce模式(11g)
    『ORACLE』 数据库restricted模式(11g)
    [转]为什么不去读顶级会议上的论文
    learn from 德国老师
    IDA 调试 Android
    L#中 int.TryParse 有问题
  • 原文地址:https://www.cnblogs.com/poorpool/p/8111252.html
Copyright © 2011-2022 走看看