转载请注明出处: 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. 我的django之旅(三)数据库和模型

    我的django之旅(三)模型和数据库 标签(空格分隔):模型 数据库 ORM 1.django ORM django内置了一套完整的解决方案,其中就包括他自己的ORM.可惜没有使用SQLAlchem ...

  2. jquery国内cdn

    推荐几个国内的jquery CDN服务地址吧: 新浪CDN,感觉很快,用的人很多,推荐使用! http://lib.sinaapp.com/js/jquery/1.9.1/jquery-1.9.1.m ...

  3. jdk,j2ee,j2se,j2me的概念区别

    jdk,j2ee,j2se,j2me的概念区别1.JDK是Java development toolkit,相当于是Java的库函数,是编译,运行java程序的工具包.J2EE是Java 2 ente ...

  4. js兼容性 - 动态删除script标签后 ,定义的函数是否执行

    hello.js function hello(){ alert('hello'); } hello.html <!DOCTYPE html> <html lang="en ...

  5. 基于cygwin构建u-boot(三)make错误忽视

    接上文,修改gcc 的-std标准后,.depend文件处理仍然出现了错误: 五.错误:make中命令报错(sed找不到需要的文件) 错误告警如下: make -C examples/api all ...

  6. [FML]学习笔记二 PAC Learning Model

    对于一个concept class C,如果存在一个算法A和一个多项式poly(.,.,.,.),有对于任意的ε>0.δ>0以及X的任意分布D和任何target concept C,当sa ...

  7. keil c51中C程序的启动过程

    汇编是从org 0000h开始启动,那么keil c51是如何启动main()函数的?keil c51有一个启动程序startup.a51,它总是和c程序一起编译和链接.下面看看它和main()函数是 ...

  8. Golden Pyramid

    Golden Pyramid Our Robo-Trio need to train for future journeys and treasure hunts. Stephan has built ...

  9. Live555 分析(二):服务端

    live555支持单播和组播,我们先分析单播的流媒体服务端,后面分析组播的流媒体服务端. 一.单播的流媒体服务端: // Create the RTSP server: RTSPServer* rts ...

  10. JAVA获得系统配置文件的System Properties

    来个java获得系统配置文件的 public class SystemProperties { public static void main(String[] args) { Properties ...