Chocolate

Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)

Total Submission(s): 577    Accepted Submission(s): 290

Problem Description
Lethe loves eating chocolates very much. In Lethe's birthday, her good friend echo brings N boxes to her, and makes the boxes on the circle. Furthermore, echo tells Lethe that there are many chocolates in the boxes, but the total
number of chocolates doesn't exceed N. Also, echo wants Lethe to displace the chocolates in such a way that in each box remains no more than one chocolate. In one move she can shift one chocolate from current box to its neighboring box. (Each box has two neighboring
boxes). Can you tell Lethe the minimum number of move to achieve this goal?
 
Input
There are multi-cases (The total number of cases won't exceed 20). First line is an integer N(1<=N<=500), the total number of boxes. Then N lines follow, each line includes a number, indicates the number of chocolates in the box.
 
Output
Output the minimum number of move.
 
Sample Input
10
1
3
3
0
0
2
0
0
0
0
 
Sample Output
9
 
Source
HDU 8th Programming Contest Site(2)



1:源点到每一个盒子建边,容量是每个盒子的巧克力数量,费用为零

2:汇点到每一个盒子建边,容量为一,因为最终每个盒子的巧克力数量不能超过1,费用用为零

3:每个盒子向邻近的盒子建边,容量为INF,因为没有规定上限,费用为1,因为每一步最多转移一个

#include<stdio.h>
#include<string.h>
#include<queue>
#include<stack>
#include<algorithm>
using namespace std;
#define MAXN 600
#define MAXM 10000
#define INF 0x3f3f3f3f
int head[MAXN],cue[MAXN],pre[MAXN];
int dis[MAXN],vis[MAXN];
int n,top;
struct node
{
int u,v,cap,flow,cost,next;
}edge[MAXM];
void init()
{
top=0;
memset(head,-1,sizeof(head));
}
void add(int u,int v,int w,int c)
{
node E1={u,v,w,0,c,head[u]};
edge[top]=E1;
head[u]=top++;
node E2={v,u,0,0,-c,head[v]};
edge[top]=E2;
head[v]=top++;
}
void getmap()
{
int a;
for(int i=1;i<=n;i++)
{
scanf("%d",&a);
add(0,i,a,0);
add(i,n+1,1,0);
if(i==1)
{
add(1,n,INF,1);
add(1,2,INF,1);
}
else if(i==n)
{
add(n,n-1,INF,1);
add(n,1,INF,1);
}
else
{
add(i,i-1,INF,1);
add(i,i+1,INF,1);
}
}
}
bool SPFA(int s,int t)
{
queue<int>q;
memset(dis,INF,sizeof(dis));
memset(vis,0,sizeof(vis));
memset(pre,-1,sizeof(pre));
dis[s]=0;
vis[s]=1;
q.push(s);
while(!q.empty())
{
int u=q.front();
q.pop();
vis[u]=0;
for(int i=head[u];i!=-1;i=edge[i].next)
{
node E=edge[i];
if(dis[E.v]>dis[E.u]+E.cost&&E.cap>E.flow)
{
dis[E.v]=dis[E.u]+E.cost;
pre[E.v]=i;
if(!vis[E.v])
{
vis[E.v]=1;
q.push(E.v);
}
}
}
}
return pre[t]!=-1;
}
void mcmf(int s,int t,int &cost)
{
cost=0;
while(SPFA(s,t))
{
int MIN=INF;
for(int i=pre[t];i!=-1;i=pre[edge[i^1].v])
{
node E=edge[i];
MIN=min(MIN,E.cap-E.flow);
}
for(int i=pre[t];i!=-1;i=pre[edge[i^1].v])
{
edge[i].flow+=MIN;
edge[i^1].flow-=MIN;
cost+=edge[i].cost*MIN;
}
}
}
int main()
{
while(scanf("%d",&n)!=EOF)
{
init();
getmap();
int cost;
mcmf(0,n+1,cost);
printf("%d\n",cost);
}
return 0;
}

 

hdoj--2282--Chocolate(最小费用)的更多相关文章

  1. hdoj 1533 Going Home 【最小费用最大流】【KM入门题】

    Going Home Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Tota ...

  2. hdoj 3488 Tour 【最小费用最大流】【KM算法】

    Tour Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 65535/65535 K (Java/Others) Total Submi ...

  3. HDU3376 最小费用最大流 模板2

    Matrix Again Time Limit: 5000/2000 MS (Java/Others)    Memory Limit: 102400/102400 K (Java/Others)To ...

  4. [板子]最小费用最大流(Dijkstra增广)

    最小费用最大流板子,没有压行.利用重标号让边权非负,用Dijkstra进行增广,在理论和实际上都比SPFA增广快得多.教程略去.转载请随意. #include <cstdio> #incl ...

  5. bzoj1927最小费用最大流

    其实本来打算做最小费用最大流的题目前先来点模板题的,,,结果看到这道题二话不说(之前打太多了)敲了一个dinic,快写完了发现不对 我当时就这表情→   =_=你TM逗我 刚要删突然感觉dinic的模 ...

  6. ACM/ICPC 之 卡卡的矩阵旅行-最小费用最大流(可做模板)(POJ3422)

    将每个点拆分成原点A与伪点B,A->B有两条单向路(邻接表实现时需要建立一条反向的空边,并保证环路费用和为0),一条残留容量为1,费用为本身的负值(便于计算最短路),另一条残留容量+∞,费用为0 ...

  7. HDU5900 QSC and Master(区间DP + 最小费用最大流)

    题目 Source http://acm.hdu.edu.cn/showproblem.php?pid=5900 Description Every school has some legends, ...

  8. Evacuation Plan-POJ2175最小费用消圈算法

    Time Limit: 1000MS Memory Limit: 65536K Special Judge Description The City has a number of municipal ...

  9. poj3422 Kaka's Matrix Travels(最小费用最大流问题)

    /* poj3422 Kaka's Matrix Travels 不知道 k次 dp做为什么不对??? 看了大牛的代码,才知道还可以这样做! 开始没有理解将a 和 a‘ 之间建立怎样的两条边,导致程序 ...

  10. P3381 【模板】最小费用最大流

    P3381 [模板]最小费用最大流 题目描述 如题,给出一个网络图,以及其源点和汇点,每条边已知其最大流量和单位流量费用,求出其网络最大流和在最大流情况下的最小费用. 输入输出格式 输入格式: 第一行 ...

随机推荐

  1. Apache Tez 0.7、0.83、 0.82 安装、调试笔记

    ———————————————————— 准备 Tez 编译环境 ———————————————————— 1 需要的支持 tez0.7 需要 Hadoop 2.60 以上 2 需要的 linux 相 ...

  2. [using_microsoft_infopath_2010]Chapter6 发布提交表单数据

    本章概要: 1.使用正确的方法发布表单 2.发布表单画库到SharePoint 3.在发布和保存表单之间做出选择 4.理解不同发布表单方式之间的区别

  3. POJ 1765 November Rain

    题目大意: 有一些屋顶,相当于一些线段(不想交). 问每一条线段能够接到多少水,相对较低的屋顶能够接到高屋顶留下的水(如题图所看到的).因为y1!=y2,所以保证屋顶是斜的. 解题思路: 扫描线,由于 ...

  4. LeetCode——Pascal&#39;s Triangle II

    Given an index k, return the kth row of the Pascal's triangle. For example, given k = 3, Return [1,3 ...

  5. linux数据库升级

    转自:老左博客:http://www.laozuo.org/6145.html 老左今天有在帮朋友的博客搬迁到另外一台VPS主机环境,其环境采用的是LLSMP架构的,原先的服务器采用的是LNMP网站环 ...

  6. Qt Quick Controls 与 Qt Quick Controls 2的区别(详细对照)

    Qt Quick Controls 原本是为支持桌面平台而开发的,后来又加入了移动平台和嵌入式平台的支持.它们应用非常广泛,因为它们提供了足够灵活的样式系统,以允许开发具有平台相关或者无关风格的应用程 ...

  7. 如何从 Datagrid 中获得单元格的内容与 使用值转换器进行绑定数据的转换IValueConverter

    一.如何从 Datagrid 中获得单元格的内容 DataGrid 属于一种 ItemsControl, 因此,它有 Items 属性并且用ItemContainer 封装它的 items. 但是,W ...

  8. mybatis的sql中使用$会出现sql注入示例

    mybatis的sql中使用$会出现sql注入示例: 模拟简单登录场景: 页面代码: function login(){ //sql注入 var user = { username : "' ...

  9. C#调用webservice(二)

    第二篇调用webservice,web服务是http://webservice.webxml.com.cn/webservices/DomesticAirline.asmx,航班查询服务 添加web服 ...

  10. Angular4集成ng2-file-upload

      在Github上找到了一个支持Angular4好用的文件上传组件ng2-file-upload,这里简单介绍一下这个库的集成使用方案.  本文基于该组件的1.2.1版. 1. 安装 安装非常简单, ...