Transportation

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 2670    Accepted Submission(s): 1157

Problem Description
There
are N cities, and M directed roads connecting them. Now you want to
transport K units of goods from city 1 to city N. There are many robbers
on the road, so you must be very careful. The more goods you carry, the
more dangerous it is. To be more specific, for each road i, there is a
coefficient ai. If you want to carry x units of goods along this road, you should pay ai * x2 dollars to hire guards to protect your goods. And what’s worse, for each road i, there is an upper bound Ci, which means that you cannot transport more than Ci units of goods along this road. Please note you can only carry integral unit of goods along each road.
You should find out the minimum cost to transport all the goods safely.
 
Input
There
are several test cases. The first line of each case contains three
integers, N, M and K. (1 <= N <= 100, 1 <= M <= 5000, 0
<= K <= 100). Then M lines followed, each contains four integers
(ui, vi, ai, Ci), indicating there is a directed road from city ui to vi, whose coefficient is ai and upper bound is Ci. (1 <= ui, vi <= N, 0 < ai <= 100, Ci <= 5)
 
Output
Output
one line for each test case, indicating the minimum cost. If it is
impossible to transport all the K units of goods, output -1.

 
Sample Input
2 1 2
1 2 1 2
2 1 2
1 2 1 1
2 2 2
1 2 1 2
1 2 2 2
 
Sample Output
4
-1
3
 
Source
 
题意:现在有一个人要从1号点运送k个单位的货物到n号点,每一条边都有一个系数a,从第i条边运送x个单位的货物所需的费用是 ai*x*x,第i条边有个容量上限Ci,问运送这k个单位的货物所需的最小费用,如果不能运送,输出-1。
题解:参考自刘汝佳的<算法竞赛-训练指南>,由于每个边的容量上限不会超过5,而我们每次运送的也是整数,所以可以利用拆边来表示一条容量为Ci的边能够运送的所有可能,假设Ci==5,那么拆成5条容量为1的边,费用分别为 1*ai,3*ai,5*ai,7*ai,9*ai,那么所有的 x*x 都可以由这几条边组合而成,然后设定超级源点和1号点的容量为 k,n号点和超级汇点的容量为k ,这样的话就限制了最大流不会超过k.然后跑一遍MCMF,判断一下maxflow是否为k,是的话,输出mincost,不是的话,输出 -1。
  1. #include <cstdio>
  2. #include <cstring>
  3. #include <queue>
  4. #include <algorithm>
  5. using namespace std;
  6. const int INF = ;
  7. const int N = ;
  8. const int M = ;
  9. struct Edge{
  10. int u,v,cap,cost,next;
  11. }edge[M];
  12. int head[N],tot,low[N],pre[N];
  13. int total ;
  14. bool vis[N];
  15. void addEdge(int u,int v,int cap,int cost,int &k){
  16. edge[k].u=u,edge[k].v=v,edge[k].cap = cap,edge[k].cost = cost,edge[k].next = head[u],head[u] = k++;
  17. edge[k].u=v,edge[k].v=u,edge[k].cap = ,edge[k].cost = -cost,edge[k].next = head[v],head[v] = k++;
  18. }
  19. void init(){
  20. memset(head,-,sizeof(head));
  21. tot = ;
  22. }
  23. bool spfa(int s,int t,int n){
  24. memset(vis,false,sizeof(vis));
  25. for(int i=;i<=n;i++){
  26. low[i] = INF;
  27. pre[i] = -;
  28. }
  29. queue<int> q;
  30. low[s] = ;
  31. q.push(s);
  32. while(!q.empty()){
  33. int u = q.front();
  34. q.pop();
  35. vis[u] = false;
  36. for(int k=head[u];k!=-;k=edge[k].next){
  37. int v = edge[k].v;
  38. if(edge[k].cap>&&low[v]>low[u]+edge[k].cost){
  39. low[v] = low[u] + edge[k].cost;
  40. pre[v] = k; ///v为终点对应的边
  41. if(!vis[v]){
  42. vis[v] = true;
  43. q.push(v);
  44. }
  45. }
  46. }
  47. }
  48. if(pre[t]==-) return false;
  49. return true;
  50. }
  51. int MCMF(int s,int t,int n){
  52. int mincost = ,minflow,flow=;
  53. while(spfa(s,t,n))
  54. {
  55. minflow=INF+;
  56. for(int i=pre[t];i!=-;i=pre[edge[i].u])
  57. minflow=min(minflow,edge[i].cap);
  58. flow+=minflow;
  59. for(int i=pre[t];i!=-;i=pre[edge[i].u])
  60. {
  61. edge[i].cap-=minflow;
  62. edge[i^].cap+=minflow;
  63. }
  64. mincost+=low[t]*minflow;
  65. }
  66. total=flow;
  67. return mincost;
  68. }
  69. int n,m,k;
  70. bool flag[N][N];
  71. int main(){
  72. while(scanf("%d%d%d",&n,&m,&k)!=EOF){
  73. init();
  74. memset(flag,-,sizeof(flag));
  75. int src = ,des = n+;
  76. for(int i=;i<=m;i++){
  77. int u,v,a,c;
  78. scanf("%d%d%d%d",&u,&v,&a,&c);
  79. for(int j=;j<c;j++){
  80. addEdge(u,v,,(*j+)*a,tot);
  81. }
  82. }
  83. addEdge(src,,k,,tot);
  84. addEdge(n,des,k,,tot);
  85. int mincost = MCMF(src,des,n+);
  86. if(total<k) printf("-1\n");
  87. else printf("%d\n",mincost);
  88. }
  89. }

hdu 3667(最小费用最大流+拆边)的更多相关文章

  1. 【网络流#2】hdu 1533 - 最小费用最大流模板题

    最小费用最大流,即MCMF(Minimum Cost Maximum Flow)问题 嗯~第一次写费用流题... 这道就是费用流的模板题,找不到更裸的题了 建图:每个m(Man)作为源点,每个H(Ho ...

  2. hdu 1533(最小费用最大流)

    Going Home Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total ...

  3. hdu 4862KM&最小费用最大流

    /*最小K路径覆盖的模型,用费用流或者KM算法解决, 构造二部图,X部有N*M个节点,源点向X部每个节点连一条边, 流量1,费用0,Y部有N*M个节点,每个节点向汇点连一条边,流量1, 费用0,如果X ...

  4. HDU 1533 最小费用最大流(模板)

    http://acm.hdu.edu.cn/showproblem.php?pid=1533 这道题直接用了模板 题意:要构建一个二分图,家对应人,连线的权值就是最短距离,求最小费用 要注意void ...

  5. hdu 6437 /// 最小费用最大流 负花费 SPFA模板

    题目大意: 给定n,m,K,W 表示n个小时 m场电影(分为类型A.B) K个人 若某个人连续看了两场相同类型的电影则失去W 电影时间不能重叠 接下来给定m场电影的 s t w op 表示电影的 开始 ...

  6. hdu 4067(最小费用最大流)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4067 思路:很神奇的建图,参考大牛的: 如果人为添加t->s的边,那么图中所有顶点要满足的条件都 ...

  7. hdu 2485(最小费用最大流)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2485 思路:题目的意思是删除最少的点使1,n的最短路大于k.将点转化为边,容量为1,费用为0,然后就是 ...

  8. hdu 6201(最小费用最大流)

    transaction transaction transaction Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 132768/1 ...

  9. hdu 3667(拆边+最小费用最大流)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3667 思路:由于花费的计算方法是a*x*x,因此必须拆边,使得最小费用流模板可用,即变成a*x的形式. ...

随机推荐

  1. [学习笔记]NTT——快速数论变换

    先要学会FFT[学习笔记]FFT——快速傅里叶变换 一.简介 FFT会爆精度.而且浮点数相乘常数比取模还大. 然后NTT横空出世了 虽然单位根是个好东西.但是,我们还有更好的东西 我们先选择一个模数, ...

  2. mysql定时器,定时查询数据库,把查询结果插入到一张表中

    1.有两张表order_repayment_detail,msg_sim ,需要把前者中的按时间过滤出来的信息插入到短信发送表中,可以每晚12点钟查询执行一次查询. 创建存储过程,这里的存储过程主要提 ...

  3. CentOS安装pip

    环境 操作系统:CentOS 6.7 32-bit Python:2.6.6 安装 先安装setuptools和wget yum -y install wget wget https://pypi.p ...

  4. ubuntu16.04登录后无dash,无启动栏launch,无menu bar,只有桌面背景解决办法

    今天打开电脑,与往常一样输入用户名密码登录后,发现桌面上空空如也,启动栏launch,menu bar什么的都消失了,桌面上文件可以打开,但是无法拖动位置,无法关闭(因为menu bar没了,无法鼠标 ...

  5. rank() within group用法【转】

    参考:http://www.itpub.net/thread-241824-1-1.html  http://blog.itpub.net/13379967/viewspace-481811/ ) w ...

  6. android Handler post sendMessage

    Handler 为Android操作系统中的线程通信工具,包为android.os.Handler. 与Handler绑定的有两个队列,一个为消息队列,另一个为线程队列.Handler可以通过这两个队 ...

  7. [LeetCode] 19. Remove Nth Node From End of List ☆

    Given a linked list, remove the nth node from the end of list and return its head. For example, Give ...

  8. ZooKeeper翻译(一)

    欢迎来到Apache ZooKeeper的世界 Apache Zookeeper是一个为了开发和维护一个开源的服务的一个尝试,这个服务使高可用的分布式协作成为可能. ZooKeeper是什么? Zoo ...

  9. spring事务的一些注意点

    参考文章 http://blog.csdn.net/qq_34021712/article/details/75949779   ©王赛超 1.在需要事务管理的地方加@Transactional 注解 ...

  10. UVA 1640 The Counting Problem

    https://vjudge.net/problem/UVA-1640 题意:统计区间[l,r]中0——9的出现次数 数位DP 注意删除前导0 #include<cmath> #inclu ...