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. dialog样式的activity设置activity的title为隐藏属性

    View view= this.findViewById(android.R.id.title);view.setVisibility(View.GONE);

  2. Android镜像文件ramdisk.img,system.img,userdata.img介绍

    Android 源码编译后,在out目录下生成的三个镜像文件:ramdisk.img,system.img,userdata.img以及它们对应的目录树root,system,data. ramdis ...

  3. vijos 1190 繁忙的都市

    描述 城市C是一个非常繁忙的大都市,城市中的道路十分的拥挤,于是市长决定对其中的道路进行改造.城市C的道路是这样分布的:城市中有n个交叉路口,有些交叉路口之间有道路相连,两个交叉路口之间最多有一条道路 ...

  4. js 下载文件/导出

    const url = '/sasd/fsd/xxxx/exportMailData2Excel'this.downloadFile(url, 'blob', this.isSearch) // 调用 ...

  5. WPF使用附加属性绑定,解决data grid列绑定不上的问题

    背景 需要对datagrid的列header添加自定义属性,然后绑定,并根据不同的列header绑定不同的值,传统的加扩展类太麻烦,而附加属性的特点更适用于这种场景. 1.xaml 代码 <Da ...

  6. windows搭建gcc开发环境(msys2) objdump

    前言 可能你并不太了解msys2,但是作为一个程序员,你一定知道mingw,而msys2就集成了mingw,同时msys2还有一些其他的特性,例如包管理器等. msys2可以在windows下搭建一个 ...

  7. k8s framework

    reference 1. k8s master framework master master 是k8s cluster运行着daemon服务:kube-apiserver, kube-schedul ...

  8. C++的反射

    写得挺不错,支持转帖下 C++语言本身是不支持反射的,但实际应用中总是会有将对象序列化的需求,总不可能C++不支持,我们就不用C++了,既然发明C++的大师们没有考虑这个,那我们只有自己动手了,毛主席 ...

  9. javase(2)_递归&迭代

    一.递归  程序调用自身的编程技巧称为递归( recursion).递归做为一种算法在程序设计语言中广泛应用. 一个过程或函数在其定义或说明中有直接或间接调用自身的一种方法,它通常把一个大型复杂的问题 ...

  10. 20171201Jsp Jstl详细配置

    Jsp Jstl详细配置 1. 下载包 http://archive.apache.org/dist/jakarta/taglibs/standard/binaries/jakarta-taglibs ...