HDU 3947 River Problem
River Problem
This problem will be judged on HDU. Original ID: 3947
64-bit integer IO format: %I64d Java class name: Main
The structure of the river contains n nodes
and exactly n-1 edges between those nodes. It's just the same as all the
rivers in this world: The edges are all unidirectional to represent
water flows. There are source points, from which the water flows, and
there is exactly one sink node, at which all parts of the river meet
together and run into the sea. The water always flows from sources to
sink, so from any nodes we can find a directed path that leads to the
sink node. Note that the sink node is always labeled 1.
As you
can see, some parts of the river are polluted, and we set a weight Wi
for each edge to show how heavily polluted this edge is. We have m kinds
of chemicals to clean the river. The i-th chemical can decrease the
weight for all edges in the path from Ui to Vi by exactly 1. Moreover,
we can use this kind of chemical for Li times, the cost for each time is
Ci. Note that you can still use the chemical even if the weight of
edges are 0, but the weight of that edge will not decrease this time.
When the weight of all edges are 0, the river is cleaned, please help us to clean the river with the least cost.
Input
test cases. The following T blocks each represents a test case.
The
first line of each block contains a number n (2<=n<=150)
representing the number of nodes. The following n-1 lines each contains 3
numbers U, V, and W, means there is a directed edge from U to V, and
the pollution weight of this edge is W. (1<=U,V<=n,
0<=W<=20)
Then follows an number m (1<=m<=2000),
representing the number of chemical kinds. The following m lines each
contains 4 numbers Ui, Vi, Li and Ci (1<=Ui,Vi<=n,
1<=Li<=20, 1<=Ci<=1000), describing a kind of chemical, as
described above. It is guaranteed that from Ui we can always find a
directed path to Vi.
Output
number indicating the least cost you are required to calculate, if the
answer does not exist, output "-1" instead.
Sample Input
2
3
2 1 2
3 1 1
1
3 1 2 2
3
2 1 2
3 1 1
2
3 1 2 2
2 1 2 1
Sample Output
Case #1: -1
Case #2: 4
Source
#include <bits/stdc++.h>
using namespace std;
using PII = pair<int,int>;
const int INF = ~0u>>;
const int maxn = ;
struct arc {
int to,flow,cost,next;
arc(int x = ,int y = ,int z = ,int nxt = -) {
to = x;
flow = y;
cost = z;
next = nxt;
}
} e[];
int head[maxn],d[maxn],p[maxn],id[maxn],tot,S = ,T,flow;
bool in[maxn] = {};
vector<PII>g[maxn];
void add(int u,int v,int flow,int cost) {
e[tot] = arc(v,flow,cost,head[u]);
head[u] = tot++;
e[tot] = arc(u,,-cost,head[v]);
head[v] = tot++;
}
bool spfa() {
queue<int>q;
memset(d,0x3f,sizeof d);
memset(p,-,sizeof p);
d[S] = ;
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].flow && d[e[i].to] > d[u] + e[i].cost) {
d[e[i].to] = d[u] + e[i].cost;
p[e[i].to] = i;
if(!in[e[i].to]) {
in[e[i].to] = true;
q.push(e[i].to);
}
}
}
}
return p[T] > -;
}
PII solve() {
int flow = ,cost = ;
while(spfa()) {
int minF = INF;
for(int i = p[T]; ~i; i = p[e[i^].to])
minF = min(minF,e[i].flow);
for(int i = p[T]; ~i; i = p[e[i^].to]) {
e[i].flow -= minF;
e[i^].flow += minF;
}
cost += minF*d[T];
flow += minF;
}
return {flow,cost};
}
void dfs(int u,int psum) {
int sum = ;
for(int i = g[u].size()-; i >= ; --i) {
dfs(g[u][i].first,g[u][i].second);
sum += g[u][i].second;
add(id[u],id[g[u][i].first],INF,);
}
int tmp = psum - sum;
if(tmp > ) {
flow += tmp;
add(S,id[u],tmp,);
} else if(tmp < ) add(id[u],T,-tmp,);
}
int main() {
int kase,cs = ,n,m,u,v,w,L,C;
scanf("%d",&kase);
while(kase--) {
scanf("%d",&n);
for(int i = tot = flow = ; i <= n; ++i) g[i].clear();
for(int i = ; i < n; ++i) {
scanf("%d%d%d",&u,&v,&w);
g[v].push_back(PII(u,w));
id[u] = i;
}
id[] = n;
memset(head,-,sizeof head);
g[T = n + ].push_back(PII(,));
dfs(,);
scanf("%d",&m);
while(m--) {
scanf("%d%d%d%d",&u,&v,&L,&C);
add(id[u],id[v],L,C);
}
PII ret = solve();
printf("Case #%d: %d\n",cs++,ret.first == flow?ret.second:-);
}
return ;
}
HDU 3947 River Problem的更多相关文章
- River Problem HDU - 3947(公式建边)
River Problem Time Limit: 6000/2000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Tot ...
- HDU 3549 Flow Problem(最大流)
HDU 3549 Flow Problem(最大流) Time Limit: 5000/5000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/ ...
- hdu 5106 Bits Problem(数位dp)
题目链接:hdu 5106 Bits Problem 题目大意:给定n和r,要求算出[0,r)之间全部n-onebit数的和. 解题思路:数位dp,一个ct表示个数,dp表示和,然后就剩下普通的数位d ...
- HDU 3374 String Problem (KMP+最大最小表示)
HDU 3374 String Problem (KMP+最大最小表示) String Problem Time Limit: 2000/1000 MS (Java/Others) Memory ...
- hdu 5105 Math Problem(数学)
pid=5105" target="_blank" style="">题目链接:hdu 5105 Math Problem 题目大意:给定a.b ...
- Hdu 5445 Food Problem (2015长春网络赛 ACM/ICPC Asia Regional Changchun Online)
题目链接: Hdu 5445 Food Problem 题目描述: 有n种甜点,每种都有三个属性(能量,空间,数目),有m辆卡车,每种都有是三个属性(空间,花费,数目).问至少运输p能量的甜点,花费 ...
- 网络流 HDU 3549 Flow Problem
网络流 HDU 3549 Flow Problem 题目:pid=3549">http://acm.hdu.edu.cn/showproblem.php?pid=3549 用增广路算法 ...
- HDU 1022 Train Problem I
A - Train Problem I Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u ...
- HDU 3374 String Problem(KMP+最大/最小表示)
String Problem Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) T ...
随机推荐
- vue2.0:(三)、项目开始,首页入门(main.js,App.vue,importfrom)
接下来,就需要对main.js App.vue 等进行操作了. 但是这就出现了一个问题:什么是main.js,他主要干什么用的?App.vue又是干什么用的?main.js 里面的import fro ...
- CF1025B Weakened Common Divisor
思路: 首先选取任意一对数(a, b),分别将a,b进行因子分解得到两个因子集合然后取并集(无需计算所有可能的因子,只需得到不同的质因子即可),之后再暴力一一枚举该集合中的元素是否满足条件. 时间复杂 ...
- 浏览器详谈及其内部工作机制 —— web开发必读
浏览器介绍 如今,浏览器格局基本上是五分天下,分别是:IE.Firefox.Safari.Chrome.Opera,而浏览器引擎就更加集中了,主要是四大巨头:IE的浏览器排版引擎Trident,目前随 ...
- 用python格式化小说txt
下载了<无人生还>的txt版.传到手机,发现阅读器识别得不够好. 原文格式如下: 第一章 一 沃格雷夫法官先生新近离任退休,现在正在头等车厢的吸烟室里,倚角而坐,一 边喷着雪茄烟,一边兴致 ...
- MyBatis插入数据之后返回插入记录的id
MyBatis插入数据的时候,返回该记录的id<insert id="insert" keyProperty="id" useGeneratedKeys= ...
- UIButton Making the hit area larger
http://stackoverflow.com/questions/808503/uibutton-making-the-hit-area-larger-than-the-default-hit-a ...
- Ubuntu 14.04 配置confluence破解
1. 配置java环境,请参展我的另一篇博客 http://www.cnblogs.com/youran-he/p/8607155.html 2. 下载文件 https://pan.baidu.com ...
- (五)VMware Harbor 部署之SSL
转自:https://www.cnblogs.com/Rcsec/p/8479728.html 1 .签名证书与自签名证书 签名证书:由权威颁发机构颁发给服务器或者个人用于证明自己身份的东西. 自签名 ...
- apache shiro的工作流程分析
本文基于shiro的web环境,用宏观(也就是不精确)的角度去理解shiro的工作流程,先看shiro官方的一张图. 和应用程序直接交互的对象是Subject,securitymanager为Subj ...
- 浮动清楚浮动及position的用法
float 在 CSS 中,任何元素都可以浮动. 浮动元素会生成一个块级框,而不论它本身是何种元素. 关于浮动的两个特点: 浮动的框可以向左或向右移动,直到它的外边缘碰到包含框或另一个浮动框的边框为止 ...