转载请注明出处: http://www.cnblogs.com/fraud/          ——by fraud

Apple Transportation


Time Limit: 1 Second      Memory Limit: 32768 KB

There's a big apple tree in the forest. In the tree there are N nodes (numbered from 0 to N - 1), and the nodes are connected by branches. On each node of the tree, there is a squirrel. In the autumn, some apples will grow on the nodes. After all apples are ripe, each squirrel will collect all the apples of their own node and store them. For the demand to be harmonic, they decide to redistribute the apples to minimize the variance (please refer to the hint) of apples in all nodes. Obviously, an apple cannot be divided into several parts. To reach this goal, some transportation should be taken. The cost of transporting x apples from node u to node v is x * distance (node u, node v). Now given the current amount of apples of each node and the structure of the apple tree, you should help the squirrels to find the minimal cost to redistribute the apples.

Input

Input consists of multiple test cases (less than 80 cases)!

For each test case, the first line contains an integer N (1 <= N <= 100), which is the number of the nodes in the tree.

The following line contains N integers a0,a1,...,aN-1 (0 <= ai <= 10000), representing the amount of the i-th node's apples.

The following N - 1 lines each contain three integers uvc (0 <= u,v <= N - 1, 0 <= c <= 1000), which means node u and node v are connected by a branch, the length of the branch is c.

There is a blank line between consecutive cases.

Output

For each case output the minimal total transportation cost. The minimal cost is guaranteed to be less than 231.

Sample Input

3
1 2 3
0 1 1
0 2 1 3
1 3 3
0 1 3
0 2 4 2
1 2
0 1 1

Sample Output

1
3
0

Hint

The formula to calculate the variance  of x1x2, ..., xn:


Author: ZHOU, Yilun
Source: ZOJ Monthly, August 2009

 

上下界费用流的水题。。。懒的写题解了。。。sad

 //#####################
//Author:fraud
//Blog: http://www.cnblogs.com/fraud/
//#####################
#include <iostream>
#include <sstream>
#include <ios>
#include <iomanip>
#include <functional>
#include <algorithm>
#include <vector>
#include <string>
#include <list>
#include <queue>
#include <deque>
#include <stack>
#include <set>
#include <map>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <cstring>
#include <climits>
#include <cctype>
using namespace std;
#define XINF INT_MAX
#define INF 0x3FFFFFFF
#define MP(X,Y) make_pair(X,Y)
#define PB(X) push_back(X)
#define REP(X,N) for(int X=0;X<N;X++)
#define REP2(X,L,R) for(int X=L;X<=R;X++)
#define DEP(X,R,L) for(int X=R;X>=L;X--)
#define CLR(A,X) memset(A,X,sizeof(A))
#define IT iterator
typedef long long ll;
typedef pair<int,int> PII;
typedef vector<PII> VII;
typedef vector<int> VI;
struct edge
{
int to,cap,cost,rev;
edge(int _to,int _cap,int _cost,int _rev)
{
to=_to;cap=_cap;cost=_cost;rev=_rev;
}
};
int V;
const int MAX_V=;
vector<edge> G[MAX_V];
int dis[MAX_V];
int prevv[MAX_V],preve[MAX_V];//最短路中的前驱结点和对应的边
void add_edge(int from,int to,int cap,int cost)
{
G[from].push_back(edge(to,cap,cost,G[to].size()));
G[to].push_back(edge(from,,-cost,G[from].size()-));
}
int vis[MAX_V];
int min_cost_flow(int s,int t,int f)//如果不能在增广则返回-1
{
int res=;
while(f>)
{
fill(dis,dis+V,INF);
dis[s]=;
queue<int>q;
CLR(vis,);
q.push(s);
while(!q.empty())
{
int v=q.front();
q.pop();
vis[v]=;
for(int i=;i<G[v].size();i++)
{
edge &e=G[v][i];
if(e.cap>&&dis[e.to]>dis[v]+e.cost)
{
dis[e.to]=dis[v]+e.cost;
prevv[e.to]=v;
preve[e.to]=i;
if(!vis[e.to])
{
q.push(e.to);
vis[e.to]=;
}
}
}
}
/* bool update=1;
while(update)
{
update=false;
for(int v=0;v<V;v++)
{
if(dis[v]==INF) continue;
for(int i=0;i<G[v].size();i++)
{
edge &e=G[v][i];
if(e.cap>0&&dis[e.to]>dis[v]+e.cost)
{
dis[e.to]=dis[v]+e.cost;
prevv[e.to]=v;
preve[e.to]=i;
update=1;
}
}
}
}*/
if(dis[t]==INF)
{
return -;
}
int d=f;
for(int v=t;v!=s;v=prevv[v])
{
d=min(d,G[prevv[v]][preve[v]].cap);
}
f-=d;
res+=d*dis[t];
for(int v=t;v!=s;v=prevv[v])
{
edge &e=G[prevv[v]][preve[v]];
e.cap-=d;
G[v][e.rev].cap+=d;
}
//cout<<f<<endl;
// cout<<"ok"<<endl;
}
return res;
}
int a[MAX_V];
int main()
{
int n;
while(scanf("%d",&n)!=EOF){
int sum=;
for(int i=;i<=n;i++){
scanf("%d",a+i);
sum+=a[i];
}
CLR(prevv,-);
CLR(preve,-);
for(int i=;i<n+;i++)G[i].clear();
int s=,tt=n+,t=n+;
int temp=sum/n;
V=t+;
for(int i=;i<=n;i++){
add_edge(s,i,a[i],);
add_edge(i,tt,,);
add_edge(i,t,temp,);
}
add_edge(tt,t,sum-n*temp,);
int u,v,d; for(int i=;i<n-;i++){
scanf("%d%d%d",&u,&v,&d);
u++;
v++;
add_edge(u,v,INF,d);
add_edge(v,u,INF,d);
}
printf("%d\n",min_cost_flow(s,t,sum)); }
return ;
}

代码君

zoj3231 Apple Transportation(最大流)的更多相关文章

  1. ZOJ3231 Apple Transportation(最小费用流)

    题目给你一棵苹果树,然后每个结点上有一定的苹果树,你要将苹果运输达到某个状态,使得均方差最小. 将苹果x个从a->b的花费是x*w,w是边权. 当时比赛的时候想的就是,最后达到的状态一定是sum ...

  2. Apple公司Darwin流式服务器源代码分析

    当前,伴随着Internet的飞速发展,计算机网络已经进入到每一个普通人的家庭.在这个过程中,一个值得我们关注的现象是:Internet中存储和传输内容的构成已经发生了本质的改变,从传统的基于文本或少 ...

  3. ZOJ 3231 Apple Transportation 树DP

    一.前言 红书上面推荐的题目,在138页,提到了关键部分的题解,但是实际上他没提到的还有若干不太好实现的地方.尤其是在这道题是大家都拿网络流玩弄的大背景下,这个代码打不出来就相当的揪心了..最后在牛客 ...

  4. UVA1486 Transportation 费用流 拆边。

    #include <iostream> #include <cstdio> #include <cmath> #include <queue> #inc ...

  5. iOS_直播类app_HTTP Live Streaming

    http://www.2cto.com/kf/201606/513980.html https://developer.apple.com/library/ios/technotes/tn2224/_ ...

  6. HTML5视频Video 音频Audio

    视频协议 视频格式 Flash HTML5 HTTP flv HTTP f4v HTTP mp4 HTTP m3u8 HTTP webm HTTP ogg RTMP flv RTMP f4v RTMP ...

  7. Darwin Streaming Server 简介

    Darwin Streaming Server     概要 Darwin Streaming Server简称DSS.DSS是Apple公司提供的开源实时流媒体播放服务器程序.整个程序使用C++编写 ...

  8. 【转载】图论 500题——主要为hdu/poj/zoj

    转自——http://blog.csdn.net/qwe20060514/article/details/8112550 =============================以下是最小生成树+并 ...

  9. M3U8文件

    M3U本质上说不是音频文件,它是音频文件的列表文件,是纯文本文件.你下载下来打开它,播放软件并不是播放它,而是根据它的记录找到网络地址进行在线播放. M3U文件的大小很小,也就是因为它里面没有任何音频 ...

随机推荐

  1. 优化PHP代码的40条建议(转载)

    [size=5][color=Red](译文)优化PHP代码的40条建议[/color][/size] 40 Tips for optimizing your php Code 原文地址:http:/ ...

  2. Service Lane

    Link https://www.hackerrank.com/challenges/service-lane def main(): n, t = map(int, raw_input().spli ...

  3. Asp.Net MVC3.0 Partial RenderPartial Action RenderAction 区别和用法

    本人写的博文不多,专业知识不强,以下纯属于个人笔记.如有不对,还请各路大拿,拍砖指导,谢谢! 区别: 1.Partial 与 RenderPartial 两个方法性质基本一样,只是把一个静态用户控件给 ...

  4. javascript中遍历EL表达式List集合中的值

    http://www.cnblogs.com/limeiky/p/6002900.html

  5. 蓝牙芯片NRF51822入门学习1:时间管理

    前言 之前辞职找工作的时候发现,很多公司希望招聘蓝牙技术方面的人才,所以干脆丢开LWIP静下心来学习蓝牙技术.原本以为一两星期能基本学会的,谁知道所选的蓝牙芯片nrf51822是个坑货,坑了我一个月. ...

  6. linux下能ping ip不能ping域名详解

    今天在开发的同事来说,内网不能通过域名访问自己的服务器!然后做了下面的测试发现这样的问题: [root@itmop ~]# ping www.downcc.com ping: unknown host ...

  7. 如何判断是REQUEST请求是来自移动终端还是来自PC端

    public bool IsMoblie()        {            string agent = (Request.UserAgent + "").ToLower ...

  8. Openstack no valid hot

    错误: 创建实例 "ce" 失败: 请稍后再试 [错误: No valid host was found. ].

  9. Android学习笔记__1__Android体系架构

    Android 体系结构图 Android作为一个移动设备的平台,其软件层次结构包括了一个操作系统(OS),中间件(MiddleWare)和应用程序(Application).根据Android的软件 ...

  10. linux下的守护进程及会话、进程组

    守护进程.会话.进程组网上有许多不错的资料.我也是网上搜罗了一堆,加上自己的理解.不敢说原创,只是写在这怕自己忘记罢了.才疏学浅,难免有错误,欢迎大家指正.下面这篇写很不错,大家可以去看看:http: ...