SPOJ BOXES
给出n个循环位置,每个位置有一定数量的盒子,每次操作可以使一个盒子转移到相邻位置,问最少需要转移多少次使得所有位置上的盒子的数量不会超过1个。
简单题。对于每个位置,加边(s,i,a[i],0),(i,t,1,0)。对于相邻的位置加边(i,i+1,inf,1),(i,i-1,inf,1) 。
显然最后我们需要求的就是最小费用了。
召唤代码君:
#include <iostream>
#include <cstring>
#include <cstdio>
#define maxn 1010
#define maxm 55555
using namespace std; const int inf=maxn;
int to[maxm],cap[maxm],cost[maxm],next[maxm],first[maxn],edge;
int d[maxn],num[maxn],from[maxn],tag[maxn],TAG=;
int Q[maxm],bot,top;
int n,m,s,t,ans,T,a[maxn]; void _init()
{
edge=-,s=,t=n+,ans=;
for (int i=s; i<=t; i++) first[i]=-;
} void addedge(int U,int V,int W,int C)
{
edge++;
to[edge]=V,cap[edge]=W,cost[edge]=C,next[edge]=first[U],first[U]=edge;
edge++;
to[edge]=U,cap[edge]=,cost[edge]=-C,next[edge]=first[V],first[V]=edge;
} bool bfs()
{
Q[bot=top=]=s,d[s]=,num[s]=inf,from[s]=-,tag[s]=++TAG;
while (bot<=top){
int cur=Q[bot++];
for (int i=first[cur]; i!=-; i=next[i])
if (cap[i]> && (tag[to[i]]!=TAG || d[cur]+cost[i]<d[to[i]])){
d[to[i]]=d[cur]+cost[i];
tag[to[i]]=TAG,
num[to[i]]=min(num[cur],cap[i]);
Q[++top]=to[i];
from[to[i]]=i;
}
}
if (tag[t]!=TAG || num[t]<=) return false;
ans+=num[t]*d[t];
for (int i=t; from[i]!=-; i=to[from[i]^])
cap[from[i]]-=num[t],cap[from[i]^]+=num[t];
return true;
} int main()
{
scanf("%d",&T);
while (T--)
{
scanf("%d",&n);
_init();
for (int i=; i<=n; i++) {
scanf("%d",&a[i]);
addedge(s,i,a[i],);
addedge(i,t,,);
if (i<n) addedge(i,i+,inf,);
if (i>) addedge(i,i-,inf,);
}
addedge(,n,inf,);
addedge(n,,inf,);
while (bfs()) ;
printf("%d\n",ans);
}
return ;
}
SPOJ BOXES的更多相关文章
- spoj 371 Boxes
N个盒子围成一圈,第i个盒子初始时有Ai个小球,每次可以把一个小球从一个盒子移到相邻的两个盒子之一里.问最少移动多少次使得每个盒子中小球的个数不超过1. ΣAi<=N.1<=N<=1 ...
- spoj 371 Boxes【最小费用最大流】
对于ai==0连接(i,t,1,0),对于ai>1(s,i,ai-1,0),然后对以相邻的两个点(i,j)连接(i,j,inf,1),注意这里是一个环的形式,所以1和n+1相连 #include ...
- BZOJ 2588: Spoj 10628. Count on a tree [树上主席树]
2588: Spoj 10628. Count on a tree Time Limit: 12 Sec Memory Limit: 128 MBSubmit: 5217 Solved: 1233 ...
- SPOJ DQUERY D-query(主席树)
题目 Source http://www.spoj.com/problems/DQUERY/en/ Description Given a sequence of n numbers a1, a2, ...
- SPOJ GSS3 Can you answer these queries III[线段树]
SPOJ - GSS3 Can you answer these queries III Description You are given a sequence A of N (N <= 50 ...
- Fedora 24 Gnome Boxes 无法ping通网络
安装Fedora 24在试用虚拟机时发现无法ping通外网. 我傻傻地以为是软件问题. 问题描述: 尝试ping程序来测试网络连通性: (我之前也是ping百度,后来在为了少打字百度了一些比较短的域名 ...
- 【填坑向】spoj COT/bzoj2588 Count on a tree
这题是学主席树的时候就想写的,,, 但是当时没写(懒) 现在来填坑 = =日常调半天lca(考虑以后背板) 主席树还是蛮好写的,但是代码出现重复,不太好,导致调试的时候心里没底(虽然事实证明主席树部分 ...
- SPOJ bsubstr
题目大意:给你一个长度为n的字符串,求出所有不同长度的字符串出现的最大次数. n<=250000 如:abaaa 输出: 4 2 1 1 1 spoj上的时限卡的太严,必须使用O(N)的算法那才 ...
- 【SPOJ 7258】Lexicographical Substring Search
http://www.spoj.com/problems/SUBLEX/ 好难啊. 建出后缀自动机,然后在后缀自动机的每个状态上记录通过这个状态能走到的不同子串的数量.该状态能走到的所有状态的f值的和 ...
随机推荐
- Unity3D使用Assetbundle打包加载(Prefab、场景)
之前有一篇文章中我们相惜讨论了Assetbundle的原理,如果对原理还不太了解的朋友可以看这一篇文章:Unity游戏开发使用Assetbundle加载场景的原理 本篇文章我们将说说assetbund ...
- linux 安装软件程序
1.用aptitude管理软件包 查看已安装的/未安装的等软件包 无法通过aptitude看到一个细节是所有跟某个特定软件包关联的所有文件的列表.利用dpkg命令能看到这个列表. dpkg -L pa ...
- silverlight简单数据绑定3
3种数据绑定模式 OneTime(一次绑定) OneWay(单项绑定) TwoWay(双向绑定) OneTime:仅在数据绑定创建时使用数据源更新目标. 列子: 第一步,创建数据源对象让Person ...
- Linux zip/unzip命令
From: http://www.ixdba.net/a/os/linux/2010/0725/359.html From: http://www.cnblogs.com/chinareny2k/ar ...
- hive 全局排序
不分发数据,使用单个reducer ; select * from dw.dw_app where dt>='2016-09-01' and dt <='2016-09-18' order ...
- problem
有两个数组a,b,大小都为n,数组元素的值任意,无序: 要求:通过交换a,b中的元素,使数组a元素的和与数组b元素的和之间的差最小
- dedecms代码研究六
今天讲的是dedecms最关键的东西,模板分析啦.也就是dedetag.class.php 里面的ParseTemplet方法 模板解析方法 先看看一个dedecms标签,大家心里有个数: {dede ...
- dispay属性的block,inline,inline-block
转自下面的几位大神: http://www.cnblogs.com/KeithWang/p/3139517.html 总体概念 block和inline这两个概念是简略的说法,完整确切的说应该是 bl ...
- xpath表达式,提取标签下的全部内容(将其他标签过滤)
例如要提取span下的内容 //div[@class="content"]/span 正确的其中一种写法如下data = response.xpath('//div[@class= ...
- iptables转发
需求 将流入服务器的公网IP的80端口的流量全部转发到另一个公网IP(1.2.3.4)的80端口上. 操作 iptables -P FORWARD ACCEPT iptables -t nat -A ...