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 ...
随机推荐
- Django的ORM基础增删改查
查询 all() 返回模型类对应表格中所有数据,返回查询集 get() 返回表格中满足条件的一条且只能有一条数据 如果查到多条数据,则抛异常:MultipleObjectsReturned 查询不到数 ...
- 前端之CSS常见兼容性问题
1.双倍浮动BUG: 描述:块状元素设置了float属性后,又设置了横向的margin值,在IE6下显示的margin值要比设置的值大: 解决方案:给float的元素添加 display:inline ...
- VUE学习,vue运行环境搭建遇见的小问题
1.使用vscode来编辑项目,那么首先给它搭一个vue运行的环境,打开集成终端,使用npm install live-server -g安装live-server. problem1:cmd终端分行 ...
- activity间的传参
Intent有两个作用:激活组件和附带数据 激活另一个activity的方法显示意图: 1. Intent intent = new Intent(); intent.setClass ...
- css命名规范—CSS样式命名整理
CSS样式命名整理 页面结构 容器: container/wrap整体宽度:wrapper页头:header内容:content页面主体:main页尾:footer导航:nav侧栏:sidebar栏目 ...
- (转)VIM 一键自动添加文件头注释
通过修改VIM的配置文件.vimrc可以让Vim(gvim)支持自动添加作者信息,具体代码如下: "进行版权声明的设置 "添加或更新头 map <F4> :call T ...
- CF 1119F Niyaz and Small Degrees
打VP的时候由于CXR和XRY切题太快了导致我只能去写后面的题了 然而VP的时候大概还有一小时时想出了\(O(n^2\log n)\)的暴力,然后过了二十分钟才想到删点的优化 结果细节很多当然是写不出 ...
- 什么是Java内存模型中的happens-before
Java内存模型JMM Java内存模型(即Java Memory Model , 简称JMM),本身是一种抽象的概念,并不真实存在,它描述的是一组规则或规范,通过这组规范定义了程序个各个变量(包括实 ...
- POI 读取 Excel 文件
import java.io.File; import java.io.FileOutputStream; import java.io.InputStream; import java.io.Out ...
- JAVA遍历map元素
第一种: Map map = new HashMap(); Iterator iter = map.entrySet().iterator(); while (iter.hasNext()) { Ma ...