River Problem

Time Limit: 2000ms
Memory Limit: 65536KB

This problem will be judged on HDU. Original ID: 3947
64-bit integer IO format: %I64d      Java class name: Main

The River of Bitland is now heavily polluted. To solve this problem, the King of Bitland decides to use some kinds of chemicals to make the river clean again.

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

The first line of the input is an integer T representing the number of
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

First output "Case #k: ", where k is the case numbers, then follows a
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的更多相关文章

  1. River Problem HDU - 3947(公式建边)

    River Problem Time Limit: 6000/2000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Tot ...

  2. HDU 3549 Flow Problem(最大流)

    HDU 3549 Flow Problem(最大流) Time Limit: 5000/5000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/ ...

  3. hdu 5106 Bits Problem(数位dp)

    题目链接:hdu 5106 Bits Problem 题目大意:给定n和r,要求算出[0,r)之间全部n-onebit数的和. 解题思路:数位dp,一个ct表示个数,dp表示和,然后就剩下普通的数位d ...

  4. HDU 3374 String Problem (KMP+最大最小表示)

    HDU 3374 String Problem (KMP+最大最小表示) String Problem Time Limit: 2000/1000 MS (Java/Others)    Memory ...

  5. hdu 5105 Math Problem(数学)

    pid=5105" target="_blank" style="">题目链接:hdu 5105 Math Problem 题目大意:给定a.b ...

  6. Hdu 5445 Food Problem (2015长春网络赛 ACM/ICPC Asia Regional Changchun Online)

    题目链接: Hdu  5445 Food Problem 题目描述: 有n种甜点,每种都有三个属性(能量,空间,数目),有m辆卡车,每种都有是三个属性(空间,花费,数目).问至少运输p能量的甜点,花费 ...

  7. 网络流 HDU 3549 Flow Problem

    网络流 HDU 3549 Flow Problem 题目:pid=3549">http://acm.hdu.edu.cn/showproblem.php?pid=3549 用增广路算法 ...

  8. HDU 1022 Train Problem I

    A - Train Problem I Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u ...

  9. HDU 3374 String Problem(KMP+最大/最小表示)

    String Problem Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) T ...

随机推荐

  1. jQuery3.2.1 和2.0和 1区别

    1. 移除旧的IE工作区新的最终版最主要的目标是更加快速,更加时尚,因此,那些支持早于IE9版本的相关技术与工作区都被移除了.这意味着如果你想要或者需要支持IE6-8,你必须用回1.12版本,因为甚至 ...

  2. UVA 1412 Fund Management (预处理+状压dp)

    状压dp,每个状态可以表示为一个n元组,且上限为8,可以用一个九进制来表示状态.但是这样做用数组开不下,用map离散会T. 而实际上很多九进制数很多都是用不上的.因此类似uva 1601 Mornin ...

  3. 数组初始化 和 vector初始化

    ] = {}; 整个数组都初始化为0 vector<); 整个vector初始化为1 如果你定义的vector是这样定义的: vector<int> B; 去初始化,千万不要用: ; ...

  4. Jordan 标准型的实例

    将学习到什么 练习一下如何把一个矩阵化为 Jordan 标准型.   将矩阵化为 Jordan 标准型需要三步: 第一步 求出矩阵 \(A \in M_n\) 全部的特征值 \(\lambda_1,\ ...

  5. 把apk文件拖到re-sign.jar运行打开的界面找不到指定文件

    下载一个zipalign.exe放到tools目录下面就可以了 点击下载

  6. vue for循环中常见问题 之 求和(合计)

    例:求后台返回数据this.dataInfo 中某个字段(item.totalSum)的和,只需添加computed,然后模板中直接可以使用totalSumAll (不需要再data中声明) comp ...

  7. 使用jQuery 发送Ajax

    jQuery AJAX 方法 AJAX 是一种与服务器交换数据的技术,可以在不重新载入整个页面的情况下更新网页的一部分. 下面的表格列出了所有的 jQuery AJAX 方法: 构建页面:jqlogi ...

  8. 解读tensorflow之rnn【转】

    转自:https://blog.csdn.net/mydear_11000/article/details/52414342 from: http://lan2720.github.io/2016/0 ...

  9. HDU-2018-奶牛的故事

    这题找到递推式就好写了,递推式大致是: f=n (n<=4) f=f(n-1)+f(n-3) (n>4) 其实这题的题意,我觉得是有很大的问题的,它前后说的每年年初的意思都不一样,敬请参考 ...

  10. css3如何实现click后页面过渡滚动到顶部

    var getTop = document.getElementById("get-top"); var head = document.getElementById(" ...