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文件的大小很小,也就是因为它里面没有任何音频 ...
随机推荐
- String.equals()
名称 说明 String.Equals(Obejecct) 确定String实例是否指 ...
- hdu3714 三分
Error Curves Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Tota ...
- C/C++中的sizeof
代码: #include <iostream> #include <string> using namespace std; int main(){ char s1[]=&qu ...
- 重写String类,也有些区别,供参考
头文件如下: #pragma once #include <string> #include <string.h> #include <stdlib.h> #inc ...
- Visual C++基础知识(win32exe)
1.Visual C++简称VC或者VC++ 是一个集成开发环境(编辑器+调试器+编译器) gcc---Linux和Unix的C++编译器 Vc----Windows的C++编译器 2.MFC(Mic ...
- Aphache VFS
http://blog.csdn.net/hemingwang0902/article/details/4733911 http://jackyrong.iteye.com/blog/1330946 ...
- 怎样利用putty登陆SSH主机方法
PuTTY 是一套免费的SSH / Telnet 程序,是在Windows 32平台下的telnet.rlogin和ssh客户端,它是一个跨平台的远程登录工具 下载putty成功后,双击打开Putty ...
- 愉快的开始 - Windows程序设计(SDK)000
愉快的开始 让编程改变世界 Change the world by program 参考教材 购买链接:Windows程序设计(第5版)(珍藏版)(附CD-ROM光盘1张) 学习环境 视频演示:W ...
- Java学习笔记--JDBC数据库的使用
参考 hu_shengyang的专栏 : http://blog.csdn.net/hu_shengyang/article/details/6290029 一. JDBC API中提供的常用数据库 ...
- Codeforces 571B Minimization
http://codeforces.com/problemset/problem/571/B 给出一个序列,可以任意调整序列的顺序,使得给出的式子的值最小 思路:我们可以把序列分解,变成k条链,n%k ...