zoukankan      html  css  js  c++  java
  • luogu3980 [NOI2008]志愿者招募

    神题,还不太清楚

    #include <iostream>
    #include <cstring>
    #include <cstdio>
    #include <queue>
    using namespace std;
    int n, m, hea[1005], cnt, uu, vv, ww, minCost, pre[1005], dis[1005], ss, tt;
    const int oo=0x3f3f3f3f;
    bool vis[1005];
    queue<int> d;
    struct Edge{
    	int too, nxt, val, cst;
    }edge[23005];
    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));
    	d.push(ss);
    	vis[ss] = true;
    	dis[ss] = 0;
    	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 mcmf(){
    	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>>m;
    	ss = 0; tt = n + 1;
    	for(int i=1; i<=n; i++){
    		scanf("%d", &uu);
    		addEdge(i, i+1, oo-uu, 0);
    	}
    	addEdge(ss, 1, oo, 0);
    	for(int i=1; i<=m; i++){
    		scanf("%d %d %d", &uu, &vv, &ww);
    		addEdge(uu, vv+1, oo, ww);
    	}
    	mcmf();
    	cout<<minCost<<endl;
    	return 0;
    }
    
  • 相关阅读:
    Linux如何查找大文件或目录总结
    Linux下动态调整LVM文件系统大小
    ios学习路线图
    js模块,类,继承,命名空间,私有属性等相关概念梳理
    <代码大全2>记录
    2017读书计划
    Spring声明式事务
    Spring-Aop
    Spring静态工厂和扫描器
    Spring-IOC
  • 原文地址:https://www.cnblogs.com/poorpool/p/8301640.html
Copyright © 2011-2022 走看看