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. ASP.NET-后台cookie与前台JQUERY解析cookie

    在controller中给cookie赋值 HttpCookie cookie =newHttpCookie("pageInfo"); cookie["page_inde ...

  2. Android自己定义百度地图缩放图标

    自己定义实现Android百度地图的缩放图标,须要自己定义一个缩放控件,实现效果例如以下: 这里的缩放效果,实现了点击button能够对地图的放大缩小,通过手势放大与缩小也控制缩放图标的可用状态.详细 ...

  3. 复习昨天的,继续过Hard题目

      # Title Editorial Acceptance Difficulty Frequency   . 65 Valid Number     12.6% Hard    . 126 Word ...

  4. POJ 1671

    其实求的是BELL数,即前N个第二类斯特林数的和. 一首诗有n行,每一行有一种韵律,问这首诗总共可能有多少种韵律排列.如4行,则所有的15种情况为:aaaa, aaab, aaba, aabb, aa ...

  5. flex 通过htmlservices链接moss的rest(rest 的get post方式)

    一:flex debug(调试)--trace() --moss导入 flex学习:1.flex出现不能使用trace调试语句的问题,控制台无信息输出.这个问题不须要改动安装文件的參量. 仅仅须要下载 ...

  6. Springmvc Get请求Tomcat、WebLogic中文乱码问题

    Springmvc Get请求Tomcat.WebLogic中文乱码问题 学习了:http://www.cnblogs.com/qingdaofu/p/5633225.html http://www. ...

  7. Qt 3D教程(三)实现对模型材质參数的控制

    Qt 3D教程(三)实现对模型材质參数的控制 蒋彩阳原创文章,首发地址:http://blog.csdn.net/gamesdev/article/details/47131841.欢迎同行前来探讨. ...

  8. iOS声明变量详解

    内容概述: 本文主要讲述了ios中多种声明变量方式的区别与联系,以及@interface声明的成员变量与@property属性的差异.最后介绍了推荐的声明方式. atany原创,转载请注明博主与博文链 ...

  9. [luogu P3813] [FJOI2017] 矩阵填数 解题报告 (容斥原理)

    题目链接: https://www.luogu.org/problemnew/show/P3813 题目: 给定一个 h*w的矩阵,矩阵的行编号从上到下依次为 1..h,列编号从左到右依次1..w. ...

  10. [jzoj 5926] [NOIP2018模拟10.25] naive 的图 解题报告(kruskal重构树+二维数点)

    题目链接: https://jzoj.net/senior/#main/show/5926 题目: 题解: 显然最小的最大路径在最小生成树上(最小生成树=最小瓶颈生成树) 于是我们建出kruskal重 ...