Beer Problem

Time Limit: 2000ms
Memory Limit: 32768KB

This problem will be judged on ZJU. Original ID: 3362
64-bit integer IO format: %lld      Java class name: Main

 

Everyone knows that World Finals of ACM ICPC 2004 were held in Prague. Besides its greatest architecture and culture, Prague is world famous for its beer. Though drinking too much is probably not good for contestants, many teams took advantage of tasting greatest beer for really low prices.

A new beer producing company Drink Anywhere is planning to distribute its product in several of the n European cities. The brewery is located near Prague, that we would certainly call city number 1. For delivering beer to other cities, the company is planning to use logistics company Drive Anywhere that provides m routes for carrying goods. Each route is described by the cities it connects (products can be transported in either direction), its capacity --- the number of barrels of beer that can be transported along this route each day, and the cost of transporting one barrel of beer along it. To deliver beer to some city it may be necessary (or advantageous) to use several routes consequently, and maybe even deliver beer using several different paths.

Each city is in turn characterized by the price that local pubs and restaurants are ready to pay for one barrel of beer. You may assume that demand for beer is essentially unlimited in each city, since this is the product that will always find its consumer.

Drink Anywhere is not planning to distribute its beer in Prague for a while, because of the high competition there, so it is just planning to provide beer to other cities for now. Help it to find out, what is the maximal income per day it can get.

Input

The first line of the input file contains n and m --- the number of cities in Europe we consider and the number of delivery routes respectively (2 ≤ n ≤ 100), 1 ≤ m ≤ 2000). The next line contains n - 1 integer numbers --- prices of a barrel of beer in European cities 2, 3 ..., n respectively (prices are positive integers and do not exceded 1000).

The following m lines contain four integer numbers each and describe delivery routes. Each route is specified by the numbers of cities it connects, its capacity, and the price of transporting one barrel of beer along it (the capacity and the price are positive integers, they do not exceed 1000).

There are multiple cases. Process to the end of file.

Output

Output the maximal income the company can get each day.

Sample Input

4 4
80 50 130
1 2 80 50
2 4 40 90
3 1 40 60
3 4 30 50

Sample Output

3000

Hint

The company should deliver 80 barrels of beer to the second city (using the first route it costs 50 per barrel to deliver beer, income is 30 per barrel, 2400 total), and 30 barrels to the fourth city (the best path uses routes 3 and 4, it costs 110 to deliver a barrel, income is 20 per barrel, 600 total). It is not profitable to deliver beer to the third city, so the company should not do it.

 

Source

Author

Andrew Stankevich
 
解题:一切皆网络流!学了一句很叼的口号。。。。。。是不是很叼?
 
啤酒,运输到各地,要赚钱,那么肯定是每单元的运费少于每单元的啤酒售价。至于什么生成费之类的,暂不考虑。如何建边?直接把各个城市的售价取负数,然后源点1号城市到其他城市的费用取整数。一旦要亏,必然出现从源点到汇点的距离,也就是费用>=0.如果这样,不必继续下去了,没赚。卖一瓶啤酒赚的钱还不够车费,搞毛线啊
 
 #include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <climits>
#include <vector>
#include <queue>
#include <cstdlib>
#include <string>
#include <set>
#include <stack>
#define LL long long
#define pii pair<int,int>
#define INF 0x3f3f3f3f
using namespace std;
const int maxn = ;
struct arc{
int v,w,f,next;
arc(int x = ,int y = ,int z = ,int nxt = ){
v = x;
w = y;
f = z;
next = nxt;
}
};
arc e[];
int head[maxn],p[maxn],d[maxn];
bool in[maxn];
int n,m,S,T,tot;
queue<int>q;
void add(int u,int v,int w,int f){
e[tot] = arc(v,w,f,head[u]);
head[u] = tot++;
e[tot] = arc(u,-w,,head[v]);
head[v] = tot++;
}
bool spfa(){
for(int i = ; i <= T; i++){
d[i] = INF;
in[i] = false;
p[i] = -;
}
while(!q.empty()) q.pop();
d[S] = ;
in[S] = true;
q.push(S);
while(!q.empty()){
int u = q.front();
q.pop();
in[u] = false;
for(int i = head[u]; ~i; i = e[i].next){
if(e[i].f > && d[e[i].v] > d[u] + e[i].w){
d[e[i].v] = d[u] + e[i].w;
p[e[i].v] = i;
if(!in[e[i].v]){
in[e[i].v] = true;
q.push(e[i].v);
}
}
}
}
return d[T] < ;
}
int solve(){
int tmp = ,minV;
while(spfa()){
minV = INF;
for(int i = p[T]; ~i; i = p[e[i^].v])
minV = min(minV,e[i].f);
for(int i = p[T]; ~i; i = p[e[i^].v]){
e[i].f -= minV;
e[i^].f += minV;
}
tmp += minV*d[T];
}
return tmp;
}
int main() {
int tmp,u,v,f;
while(~scanf("%d %d",&n,&m)){
S = ;
T = n+;
memset(head,-,sizeof(head));
tot = ;
for(int i = ; i <= n; i++){
scanf("%d",&tmp);
add(i,T,-tmp,INF);
}
for(int i = ; i < m; i++){
scanf("%d %d %d %d",&u,&v,&f,&tmp);
add(u,v,tmp,f);
add(v,u,tmp,f);
}
printf("%d\n",-solve());
}
return ;
}

ZOJ 3362 Beer Problem的更多相关文章

  1. ZOJ 3362 Beer Problem(SPFA费用流应用)

    Beer Problem Time Limit: 2 Seconds      Memory Limit: 32768 KB Everyone knows that World Finals of A ...

  2. zoj 3621 Factorial Problem in Base K 数论 s!后的0个数

    Factorial Problem in Base K Time Limit: 20 Sec  Memory Limit: 256 MB 题目连接 http://acm.zju.edu.cn/onli ...

  3. ZOJ3362 Beer Problem(最小费用任意流)

    题目大概说有n个城市,由m条无向边相连,每条边每天最多运送cap桶酒且其运送一桶的花费是cost.现在从1号城市开始出发运酒,供应到2到n号城市,这些城市的收购单价是price,问最大的盈利是多少. ...

  4. ZOJ 1455 Schedule Problem(差分约束系统)

    // 题目描述:一个项目被分成几个部分,每部分必须在连续的天数完成.也就是说,如果某部分需要3天才能完成,则必须花费连续的3天来完成它.对项目的这些部分工作中,有4种类型的约束:FAS, FAF, S ...

  5. ZOJ 3777 B - Problem Arrangement 状压DP

    LINK:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3777 题意:有N(\( N <= 12 \))道题,排顺序 ...

  6. zoj 3362(最大费用)

    题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=3904 思路:费用流的题,增加一个超级源点和一个超级汇点,然后就是连边 ...

  7. zoj 3859 DoIt is Being Flooded (MFSet && Flood Fill)

    ZOJ :: Problems :: Show Problem 这题开始的时候想不到怎么调整每个grid的实际淹没时间,于是只好找了下watashi的题解,发现这个操作还是挺简单的. ZOJ3354 ...

  8. poj 1436 && zoj 1391 Horizontally Visible Segments (Segment Tree)

    ZOJ :: Problems :: Show Problem 1436 -- Horizontally Visible Segments 用线段树记录表面能被看见的线段的编号,然后覆盖的时候同时把能 ...

  9. poj 1689 && zoj 1422 3002 Rubbery (Geometry + BFS)

    ZOJ :: Problems :: Show Problem 1689 -- 3002 Rubbery 这题是从校内oj的几何分类里面找到的. 题意不难,就是给出一个区域(L,W),这个区域里面有很 ...

随机推荐

  1. Codeforces Round #277 (Div. 2)A. Calculating Function 水

    A. Calculating Function   For a positive integer n let's define a function f: f(n) =  - 1 + 2 - 3 +  ...

  2. SqlServer还原步骤

    SqlServer还原步骤 2009-09-05 10:32:12|  分类: 数据库|字号 订阅     1 . 删除原有数据库 新建数据库  hywlxt 2. 在master 中新建存储过程 k ...

  3. PCB 周期日历

    在PCB行业一直以来没找到适合我们这行业的日历,主要存在2个差异导致. 1.周期差异:由于PCB 周期计算的复杂性,市面上无法找到符合PCB行业计算周期方式 (另一遍博文中有写周期计算逻辑) http ...

  4. Palindrome(dp)

    http://poj.org/problem?id=1159 题意:给定一个字符,问最少插入多少字符,使该字符串变成回文字符串. 思路:设原字符串序列为X,其逆字符串为Y,则最少插入的字符数=leng ...

  5. selenium3 + python - xpath定位

    什么是xpath呢? 官方介绍:XPath即为XML路径语言,它是一种用来确定XML1(标准通用标记语言3的子集)文档中某部分位置的语言.反正小编看这个介绍是云里雾里的,通俗一点讲就是通过元素的路径来 ...

  6. 模拟Queue(wait/notify)

    BlockingQueue:顾名思义,首先它是一个队列,并且支持阻塞的机制,阻塞的放入和得到数据.我们要实现LinkedBlockingQueue下面的两个方法put和take. put(anObje ...

  7. Ubuntu下搭建repo服务器(一): 配置gitosis

    1. 说明 服务器端IP: 192.168.1.126,下文简称:A端: 客户端IP: 192.168.130.19,下文简称:B端: Android工程代号:17435. 2. 安装必要软件(A端) ...

  8. 仿QQ空间长图效果简易版--母亲节感恩

    手机网站 母亲节最火的两件事 1.NBA 杜兰特在获MVP催泪致辞献给母亲:她才是真的MVP. 2.QQ空间长图 ------------------------------------------- ...

  9. jQuery中容易让人困惑的东西

    前言:jqueryt很灵活,太灵活了,可以说是他一个优点,也是他一个缺点,达到一种效果,十个人也许会用十种不同的方法来实现这个过程,结果一样,过程不一样,这到底是好,还是坏呢. 一,什么是jquery ...

  10. CXF-JAX-WS开发(一)入门案例

    一.Web Service 1.定义 W3C定义,Web服务(Web service)应当是一个软件系统,用以支持网络间不同机器的互动操作. 2.作用 多系统间数据通信 二.CXF是什么? CXF是目 ...