zoj3231 Apple Transportation(最大流)
转载请注明出处: 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 u, v, c (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 x1, x2, ..., 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(最大流)的更多相关文章
- ZOJ3231 Apple Transportation(最小费用流)
题目给你一棵苹果树,然后每个结点上有一定的苹果树,你要将苹果运输达到某个状态,使得均方差最小. 将苹果x个从a->b的花费是x*w,w是边权. 当时比赛的时候想的就是,最后达到的状态一定是sum ...
- Apple公司Darwin流式服务器源代码分析
当前,伴随着Internet的飞速发展,计算机网络已经进入到每一个普通人的家庭.在这个过程中,一个值得我们关注的现象是:Internet中存储和传输内容的构成已经发生了本质的改变,从传统的基于文本或少 ...
- ZOJ 3231 Apple Transportation 树DP
一.前言 红书上面推荐的题目,在138页,提到了关键部分的题解,但是实际上他没提到的还有若干不太好实现的地方.尤其是在这道题是大家都拿网络流玩弄的大背景下,这个代码打不出来就相当的揪心了..最后在牛客 ...
- UVA1486 Transportation 费用流 拆边。
#include <iostream> #include <cstdio> #include <cmath> #include <queue> #inc ...
- iOS_直播类app_HTTP Live Streaming
http://www.2cto.com/kf/201606/513980.html https://developer.apple.com/library/ios/technotes/tn2224/_ ...
- HTML5视频Video 音频Audio
视频协议 视频格式 Flash HTML5 HTTP flv HTTP f4v HTTP mp4 HTTP m3u8 HTTP webm HTTP ogg RTMP flv RTMP f4v RTMP ...
- Darwin Streaming Server 简介
Darwin Streaming Server 概要 Darwin Streaming Server简称DSS.DSS是Apple公司提供的开源实时流媒体播放服务器程序.整个程序使用C++编写 ...
- 【转载】图论 500题——主要为hdu/poj/zoj
转自——http://blog.csdn.net/qwe20060514/article/details/8112550 =============================以下是最小生成树+并 ...
- M3U8文件
M3U本质上说不是音频文件,它是音频文件的列表文件,是纯文本文件.你下载下来打开它,播放软件并不是播放它,而是根据它的记录找到网络地址进行在线播放. M3U文件的大小很小,也就是因为它里面没有任何音频 ...
随机推荐
- jquery validate form 异步提交
jQuery取得select选中的值 jQuery("#select1 option:selected").text(); 相信很多人都用过jquery validate插件,非 ...
- 在Eclipse中运行hadoop程序
1.下载hadoop-eclipse-plugin-1.2.1.jar,并将之复制到eclipse/plugins下. 2.打开map-reduce视图 在eclipse中,打开window--> ...
- java生成UUID通用唯一识别码 (Universally Unique Identifier)
转自:http://blog.csdn.net/carefree31441/article/details/3998553 UUID含义是通用唯一识别码 (Universally Unique Ide ...
- Bootstrap学习笔记(未整理)
强调class 这些class通过颜色来表示强调.也可以应用于链接,当鼠标盘旋于链接上时,其颜色会变深,就像默认的链接样式. <p class="text-muted"> ...
- sql 字段先计算后再拿比对的字段进行比对 效率提升100倍
关于日期索引的使用,不要计算后再对比,否则使用不了索引例如:以下执行不了索引,耗时很大 dywl=# explain analyze SELECT car_bill.billno,car_bill.b ...
- Double Strings Solved Problem code: DOUBLE
# Fuking silly, OTZ.... import sys def main(): n = int(raw_input()) for num in sys.stdin: if int(num ...
- 深入理解 静态类和静态字段(C# 基础)
序言 以前,总是被提醒,在编程过程中尽量少用静态变量,数据丢失什么的,今天有空,禁不住对静态变量的强烈好奇,跟我一起了解下静态家族的内幕吧. 静态类 定义 静态类与非静态类的重要区别在于静态类不能实例 ...
- Shell脚本调试技术
http://www.ibm.com/developerworks/cn/linux/l-cn-shell-debug/ 一. 前言 shell编程在unix/linux世界中使用得非常广泛,熟练掌握 ...
- spring 4 泛型注入
最近对系统进行改造,发现在泛型实例初始化的时候,得不到想要的泛型.或者需要强制转换. spring 4 开始支持泛型对象初始化,初始化方法如下: 注:使用配置文件的方法暂时还没有发现,下面是使用jav ...
- 瑞柏匡丞:app商业价值如何体现
在互联网行业,想要实现商业价值,必须先实现用户价值.这个观点发源自PC统治互联网的时代,如今PC端的用户停留时间下降,用户行为趋于稳定保守,移动端则蒸蒸日上.而PC与移动端的区别之一是,PC端的用户流 ...