转载请注明出处: 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. Lua绑定C++类

    原文:http://blog.csdn.net/chenee543216/article/details/12074771 以下是代码: Animal.h文件 #pragma once #ifndef ...

  2. 搬寝室(HDU 1421 DP)

    搬寝室 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submiss ...

  3. juqery easy ui 实现二级菜单联动

    实现效果 代码: <asp:Content ID="Content1" ContentPlaceHolderID="HeadContent" runat= ...

  4. [TYVJ] P1001 第K极值

    第K极值   背景 Background 成成第一次模拟赛 第一道    描述 Description 给定一个长度为N(0<n<=10000)的序列,保证每一个序列中的数字a[i]是小于 ...

  5. LeetCode_N-Queens II

    Follow up for N-Queens problem. Now, instead outputting board configurations, return the total numbe ...

  6. PowerShell3.0中,所有的命令

    Get-Command * >> cmd.txt CommandType Name ModuleName ----------- ---- ---------- Alias % -> ...

  7. 10.1.5 Connection Character Sets and Collations

    10.1.5 Connection Character Sets and Collations Several character set and collation system variables ...

  8. Dynamics CRM 2013 初体验(2):UI

    Dynamics CRM 2013 系统的UI与2011相比改动是巨大的:传统的导航栏被去掉了,取代它的是win8风格的小磁铁:Ribbon风格的工具栏也被去掉啦,它的风格将回滚至4.0时代:新系统添 ...

  9. linux centos6.4 php连接sql server2008

    1.安装SQL Server驱动freetds yum search freetds yum install freetds php-mssql 或者下载编译安装   2.修改/etc/freetds ...

  10. C# WEB API ApiController 修改response header contentType

    var res = Request.CreateResponse(HttpStatusCode.OK, file); res.Content.Headers.ContentType = new Med ...