River Problem

Time Limit: 6000/2000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 721    Accepted Submission(s): 282

Problem Description
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
 
Author
Thost & Kennethsnow
 

Noi2008 志愿者招募 一样 就是相邻的节点  不是连续的天数了 而是建立了一个图

用dfs走一遍  建图就好了

公式不用推  看懂 那个题想一下就好了

#include <iostream>
#include <cstdio>
#include <sstream>
#include <cstring>
#include <map>
#include <cctype>
#include <set>
#include <vector>
#include <stack>
#include <queue>
#include <algorithm>
#include <cmath>
#include <bitset>
#define rap(i, a, n) for(int i=a; i<=n; i++)
#define rep(i, a, n) for(int i=a; i<n; i++)
#define lap(i, a, n) for(int i=n; i>=a; i--)
#define lep(i, a, n) for(int i=n; i>a; i--)
#define rd(a) scanf("%d", &a)
#define rlld(a) scanf("%lld", &a)
#define rc(a) scanf("%c", &a)
#define rs(a) scanf("%s", a)
#define rb(a) scanf("%lf", &a)
#define rf(a) scanf("%f", &a)
#define pd(a) printf("%d\n", a)
#define plld(a) printf("%lld\n", a)
#define pc(a) printf("%c\n", a)
#define ps(a) printf("%s\n", a)
#define MOD 2018
#define LL long long
#define ULL unsigned long long
#define Pair pair<int, int>
#define mem(a, b) memset(a, b, sizeof(a))
#define _ ios_base::sync_with_stdio(0),cin.tie(0)
//freopen("1.txt", "r", stdin);
using namespace std;
const int maxn = 1e5 + , INF = 0x7fffffff, LL_INF = 0x7fffffffffffffff;
int n, m, s, t;
int head[maxn], d[maxn], vis[maxn], nex[maxn], f[maxn], p[maxn], cnt, head1[maxn], nex1[maxn];
int xu[maxn], flow, value, ans; struct edge
{
int u, v, c;
}Edge[maxn << ]; void addedge(int u, int v, int c)
{
Edge[ans].u = u;
Edge[ans].v = v;
Edge[ans].c = c;
nex1[ans] = head1[u];
head1[u] = ans++;
}; struct node
{
int u, v, w, c;
}Node[maxn << ]; void add_(int u, int v, int w, int c)
{
Node[cnt].u = u;
Node[cnt].v = v;
Node[cnt].w = w;
Node[cnt].c = c;
nex[cnt] = head[u];
head[u] = cnt++;
} void add(int u, int v, int w, int c)
{
add_(u, v, w, c);
add_(v, u, -w, );
} int spfa()
{
for(int i = ; i < maxn; i ++) d[i] = INF;
deque<int> Q;
mem(vis, );
mem(p, -);
Q.push_front(s);
d[s] = ;
p[s] = , f[s] = INF;
while(!Q.empty())
{
int u = Q.front(); Q.pop_front();
vis[u] = ;
for(int i = head[u];i != -; i = nex[i])
{
int v = Node[i].v;
if(Node[i].c)
{
if(d[v] > d[u] + Node[i].w)
{
d[v] = d[u] + Node[i].w;
p[v] = i;
f[v] = min(f[u], Node[i].c);
if(!vis[v])
{
// cout << v << endl;
if(Q.empty()) Q.push_front(v);
else
{
if(d[v] < d[Q.front()]) Q.push_front(v);
else Q.push_back(v);
}
vis[v] = ;
}
}
}
}
}
if(p[t] == -) return ;
flow += f[t], value += f[t] * d[t];
// cout << value << endl;
for(int i = t; i != s; i = Node[p[i]].u)
{
Node[p[i]].c -= f[t];
Node[p[i] ^ ].c += f[t];
}
return ;
} void max_flow()
{
flow = value = ;
while(spfa());
}
int sum_flow; void init()
{
mem(head, -);
mem(head1, -);
Edge[].c = ;
cnt = sum_flow = ;
ans = ;
} void dfs(int u, int pre_sum)
{
int sum = ;
for(int i = head1[u]; i != -; i = nex1[i])
{
int v = Edge[i].v;
add(u, v, , INF);
dfs(v, Edge[i].c);
sum += Edge[i].c; //要减去当前子节点的所有父节点的公式
}
int tmp = pre_sum - sum;
if(tmp > ) add(s, u, , tmp), sum_flow += tmp;
else add(u, t, , -tmp); } int id[maxn]; int main()
{
int T, kase = ;
int u, v, w, c;
rd(T);
while(T--)
{
init();
rd(n);
s = , t = n + ;
rap(i, , n - )
{
rd(u), rd(v), rd(w);
addedge(v, u, w); //反向建图 想一下是下一个公式减去上一个公式 即子结点减去父结点
}
addedge(t, , );
rd(m);
rap(i, , m)
{
rd(u), rd(v), rd(c), rd(w);
add(u, v, w, c);
}
dfs(, );
max_flow();
printf("Case #%d: ", ++kase);
if(sum_flow == flow)
cout << value << endl;
else
cout << - << endl; } return ;
}

River Problem

Time Limit: 6000/2000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 721    Accepted Submission(s): 282

Problem Description
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
 
Author
Thost & Kennethsnow
 

River Problem HDU - 3947(公式建边)的更多相关文章

  1. HDU 3947 River Problem

    River Problem Time Limit: 2000ms Memory Limit: 65536KB This problem will be judged on HDU. Original ...

  2. Flow Problem HDU - 3549

    Flow Problem HDU - 3549 Network flow is a well-known difficult problem for ACMers. Given a graph, yo ...

  3. D - Ugly Problem HDU - 5920

    D - Ugly Problem HDU - 5920 Everyone hates ugly problems. You are given a positive integer. You must ...

  4. Prime Ring Problem HDU - 1016 (dfs)

    Prime Ring Problem HDU - 1016 A ring is compose of n circles as shown in diagram. Put natural number ...

  5. 志愿者招募 HYSBZ - 1061(公式建图费用流)

    转自神犇:https://www.cnblogs.com/jianglangcaijin/p/3799759.html 题意:申奥成功后,布布经过不懈努力,终于 成为奥组委下属公司人力资源部门的主管. ...

  6. HDU 3947 Assign the task

    http://acm.hdu.edu.cn/showproblem.php?pid=3974 Problem Description There is a company that has N emp ...

  7. (线段树 区间查询)The Water Problem -- hdu -- 5443 (2015 ACM/ICPC Asia Regional Changchun Online)

    链接: http://acm.hdu.edu.cn/showproblem.php?pid=5443 The Water Problem Time Limit: 1500/1000 MS (Java/ ...

  8. 差分约束系统+(矩阵)思维(H - THE MATRIX PROBLEM HDU - 3666 )

    题目链接:https://cn.vjudge.net/contest/276233#problem/H 题目大意:对于给定的矩阵  每一行除以ai  每一列除以bi 之后 数组的所有元素都还在那个L- ...

  9. HDU 4522 (恶心建图)

    湫湫系列故事——过年回家 Time Limit: 500/200 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Others)Total ...

随机推荐

  1. 基于 Token 的身份验证:JSON Web Token(附:Node.js 项目)

    最近了解下基于 Token 的身份验证,跟大伙分享下.很多大型网站也都在用,比如 Facebook,Twitter,Google+,Github 等等,比起传统的身份验证方法,Token 扩展性更强, ...

  2. 二十一、当锚点遇到fixed(margin和padding)

    当锚点点击跳转的时候,如果上方有fixed,锚点跳转会默认跳转到top为0的地方,有一部分就被遮挡了 解决方法:(像素值随便给的) 给锚点跳转到的具体内容加padding-top:-50px:marg ...

  3. C#对摄像头的操作示例,采用Aforge库

    操作摄像头有三个办法:VFW.DirectShow.花钱买第三方控件 VFW技术比较古老,无法解决驱动不完善造成的某些问题 DirectShow技术相对完善一些,但这是C++才能实现的技术.如果用.N ...

  4. 学习mongoDB的一些感受(转自:http://blog.csdn.net/liusong0605/article/details/11581019)

    曾经使用过mongoDB来保存文件,最一开始,只是想总结一下在开发中如何实现文件与mongoDB之间的交互.在此之前,并没有系统的了解过mongoDB,虽然知道我们用它来存储文件这些非结构化数据,但是 ...

  5. 个人项目 Individual Project

    通讯录中的联系人包含以下信息项:姓名.手机.办公电话.家庭电话.电子邮箱.所在省市.工作单位.家庭住址,群组分类(亲属.同事.同学.朋友.其他). 系统的主要功能包括: 1. 输入联系人的信息,要求: ...

  6. 安装使用swoole

    swoole首页:https://www.swoole.com/ 方法1:使用pecl安装 pecl install swoole 注意,php版本必须是7.0以及7.0以上的版本. 方法2:编译源码 ...

  7. 自签名证书 nginx tomcat

    给Nginx配置一个自签名的SSL证书 - 廖雪峰的官方网站 https://www.liaoxuefeng.com/article/0014189023237367e8d42829de24b6eaf ...

  8. asp.net mvc Dateset读取Excel数据

    //处理Excel //读取Excel [NonAction] public static DataSet ExcelToDS(string Path) { //根据情况选择合适的连接字符,参考msd ...

  9. 【Python3练习题 002】企业发放的奖金根据利润提成

    # [Python练习题 002]企业发放的奖金根据利润提成.# 利润(I)低于或等于10万元时,奖金可提10%:利润高于10万元,低于20万元时,低于10万元的部分按10%提成,高于10万元的部分, ...

  10. 使用PL/SQL连接Oracle时报错ORA-12541: TNS: 无监听程序

    因公司需求,安装oracle数据库,oracle数据库用账号密码可以登录,然后在pl/sql里面不能登录,显示无监听程序. 这就说明可能有些服务没有启动,开始运行services.msc ,进入后寻找 ...