ZOJ 3362 Beer Problem
Beer Problem
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
#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的更多相关文章
- ZOJ 3362 Beer Problem(SPFA费用流应用)
Beer Problem Time Limit: 2 Seconds Memory Limit: 32768 KB Everyone knows that World Finals of A ...
- 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 ...
- ZOJ3362 Beer Problem(最小费用任意流)
题目大概说有n个城市,由m条无向边相连,每条边每天最多运送cap桶酒且其运送一桶的花费是cost.现在从1号城市开始出发运酒,供应到2到n号城市,这些城市的收购单价是price,问最大的盈利是多少. ...
- ZOJ 1455 Schedule Problem(差分约束系统)
// 题目描述:一个项目被分成几个部分,每部分必须在连续的天数完成.也就是说,如果某部分需要3天才能完成,则必须花费连续的3天来完成它.对项目的这些部分工作中,有4种类型的约束:FAS, FAF, S ...
- ZOJ 3777 B - Problem Arrangement 状压DP
LINK:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3777 题意:有N(\( N <= 12 \))道题,排顺序 ...
- zoj 3362(最大费用)
题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=3904 思路:费用流的题,增加一个超级源点和一个超级汇点,然后就是连边 ...
- zoj 3859 DoIt is Being Flooded (MFSet && Flood Fill)
ZOJ :: Problems :: Show Problem 这题开始的时候想不到怎么调整每个grid的实际淹没时间,于是只好找了下watashi的题解,发现这个操作还是挺简单的. ZOJ3354 ...
- poj 1436 && zoj 1391 Horizontally Visible Segments (Segment Tree)
ZOJ :: Problems :: Show Problem 1436 -- Horizontally Visible Segments 用线段树记录表面能被看见的线段的编号,然后覆盖的时候同时把能 ...
- poj 1689 && zoj 1422 3002 Rubbery (Geometry + BFS)
ZOJ :: Problems :: Show Problem 1689 -- 3002 Rubbery 这题是从校内oj的几何分类里面找到的. 题意不难,就是给出一个区域(L,W),这个区域里面有很 ...
随机推荐
- @Validated数据校验
//lastName必须是邮箱格式 @Email private String lastName;
- Android中关于内部存储的一些重要函数
一.简介 Android中,你也可以通过绝对路径以JAVA传统方式访问内部存储空间.但是以这种方式创建的文件是对私有,创建它的应用程序对该文件是可读可写,但是别的应用程序并不能直接访问它.不是所有的内 ...
- 历届试题 邮局(dfs+剪枝)
历届试题 邮局 时间限制:1.0s 内存限制:256.0MB 问题描述 C村住着n户村民,由于交通闭塞,C村的村民只能通过信件与外界交流.为了方便村民们发信,C村打算在C村建设k ...
- bzoj 4318 OSU! —— 期望DP
题目:https://www.lydsy.com/JudgeOnline/problem.php?id=4318 期望DP,因为平方的期望不等于期望的平方,所以用公式递推: 第一次推错了囧,还是看这位 ...
- springboot的登录拦截机制
转自:https://blog.csdn.net/qq_26555463/article/details/78296103 如果是一个后台的管理项目的,有些东西是不能直接就可以访问的,必须要登录才可以 ...
- linux下sh语法(转载)
介绍: 1 开头 程序必须以下面的行开始(必须方在文件的第一行): #!/bin/sh 符号#!用来告诉系统它后面的参数是用来执行该文件的程序. 在这个例子中我们使用/bin/sh来执行程序. 当编写 ...
- poj2975 Nim(经典博弈)
Nim Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 5866 Accepted: 2777 Description N ...
- Moon Http Server,强大如斯的全脚本web服务器
Moon Http Server(MHS) 是一个使用Pascal脚本的高性能Web服务器. 昨天晚上是第一次接触.花了30分钟入门,非常强大.是Delphi 者开发Web的福音. 引用一下作者的介绍 ...
- 实体 和 XML格式的转换
许多接口传输需要XML格式转换,那么如何实现?看下面帮助类→_→ /// <summary> /// XML处理帮助类,编码格式GBK!! /// </summary> pub ...
- http接口 两种调用http接口的方法
import org.apache.commons.httpclient.HttpClient; import org.apache.commons.httpclient.NameValuePair; ...