transaction transaction transaction

Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 132768/132768 K (Java/Others)
Total Submission(s): 1003    Accepted Submission(s):
488

Problem Description
Source
直接构图 费用流跑一遍
  1. #include<iostream>
  2. #include<cstdio>
  3. #include<algorithm>
  4. #include<cstring>
  5. #include<cstdlib>
  6. #include<string.h>
  7. #include<set>
  8. #include<vector>
  9. #include<queue>
  10. #include<stack>
  11. #include<map>
  12. #include<cmath>
  13. typedef long long ll;
  14. typedef unsigned long long LL;
  15. using namespace std;
  16. const double PI=acos(-1.0);
  17. const double eps=0.0000000001;
  18. const int INF=0x3f3f3f3f;
  19. const int N=+;
  20. int a[N];
  21. int head[N];
  22. int dis[N];
  23. int pre[N];
  24. int vis[N];
  25. int tot;
  26. int m,n;
  27. struct node{
  28. int from,to,next,flow,cost;
  29. }edge[N<<];
  30. void init(){
  31. memset(head,-,sizeof(head));
  32. tot=;
  33. }
  34. void add(int u,int v,int c,int cost){
  35. edge[tot].from=u;
  36. edge[tot].to=v;
  37. edge[tot].flow=c;
  38. edge[tot].cost=cost;
  39. edge[tot].next=head[u];
  40. head[u]=tot++;
  41. edge[tot].from=v;
  42. edge[tot].to=u;
  43. edge[tot].flow=;
  44. edge[tot].cost=-cost;
  45. edge[tot].next=head[v];
  46. head[v]=tot++;
  47. }
  48. int spfa(int s,int t){
  49. memset(pre,-,sizeof(pre));
  50. memset(dis,INF,sizeof(dis));
  51. memset(vis,,sizeof(vis));
  52. queue<int>q;
  53. dis[s]=;
  54. vis[s]=;
  55. q.push(s);
  56. while(!q.empty()){
  57. int x=q.front();
  58. q.pop();
  59. vis[x]=;
  60. for(int i=head[x];i!=-;i=edge[i].next){
  61. int v=edge[i].to;
  62. if(edge[i].flow&&dis[v]>dis[x]+edge[i].cost){
  63. dis[v]=edge[i].cost+dis[x];
  64. pre[v]=i;
  65. if(vis[v]==){
  66. vis[v]=;
  67. q.push(v);
  68. }
  69.  
  70. }
  71. }
  72. }
  73. if(pre[t]==-)return ;
  74. return ;
  75. }
  76. int MCMF(int s,int t){
  77. int flow=;
  78. int cost=;
  79. while(spfa(s,t)){
  80. int minn=INF;
  81. for(int i=pre[t];i!=-;i=pre[edge[i].from]){
  82. minn=min(minn,edge[i].flow);
  83. }
  84. for(int i=pre[t];i!=-;i=pre[edge[i].from]){
  85. edge[i].flow=edge[i].flow-minn;
  86. edge[i^].flow=edge[i^].flow+minn;
  87. cost=edge[i].cost+cost;
  88. // cout<<cost<<endl;
  89. }
  90. flow=flow+minn;
  91. }
  92. return cost;
  93. }
  94. int main(){
  95. int tt;
  96. scanf("%d",&tt);
  97. while(tt--){
  98. init();
  99. scanf("%d",&n);
  100. for(int i=;i<=n;i++){
  101. scanf("%d",&a[i]);
  102. }
  103. int s=;
  104. int s1=n+;
  105. int t=n+;
  106. add(s,s1,,);
  107. for(int i=;i<=n;i++){
  108. add(s1,i,,-a[i]);
  109. }
  110. for(int i=;i<=n;i++){
  111. add(i,t,,a[i]);
  112. }
  113. for(int i=;i<=n-;i++){
  114. int u,v,cost;
  115. scanf("%d%d%d",&u,&v,&cost);
  116. add(u,v,,cost);
  117. add(v,u,,cost);
  118. }
  119. cout<<abs(MCMF(s,t))<<endl;
  120. }
  121. }

hdu 6201(最小费用最大流)的更多相关文章

  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 3667(最小费用最大流+拆边)

    Transportation Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)To ...

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

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

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

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

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

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

  9. hdu 2686 Matrix 最小费用最大流

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2686 Yifenfei very like play a number game in the n*n ...

随机推荐

  1. [转]Js获取当前日期时间及其它操作

    转载自:http://www.cnblogs.com/carekee/articles/1678041.html Js获取当前日期时间及其它操作 var myDate = new Date();myD ...

  2. 用Docker构建Tomcat镜像

    构建tomcat镜像 创建工作目录 [root@elk-node2 tomcat]# mkdir tomcat [root@elk-node2 tomcat]# cd tomcat [root@elk ...

  3. poj - 3254 - Corn Fields (状态压缩)

    poj - 3254 - Corn Fields (状态压缩)超详细 参考了 @外出散步 的博客,在此基础上增加了说明 题意: 农夫有一块地,被划分为m行n列大小相等的格子,其中一些格子是可以放牧的( ...

  4. 木块问题(The Blocks Problem,Uva 101)

    不定长数组:vector vector就是一个不定长数组.不仅如此,它把一些常用操作“封装”在了vector类型内部. 例如,若a是一个vector,可以用a.size( )读取它的大小,a.resi ...

  5. HDU4496 D-City【基础并查集】

    Problem Description Luxer is a really bad guy. He destroys everything he met.  One day Luxer went to ...

  6. reading/writing files in Python

    file types: plaintext files, such as .txt .py Binary files, such as .docx, .pdf, iamges, spreadsheet ...

  7. led1,1s取反,led2计数10次取反

    1 //利用定时器0 1s,led1取反,利用计数器1,跳10,取反 #include<reg52.h> #define uchar unsigned char #define uint ...

  8. 基于XML文档的声明式事务配置

    <!-- 配置事务切面 --> <aop:config> <aop:pointcut expression="execution(* com.atguigu.t ...

  9. 【00】angular学习网站

    [00]   学习资料:   http://angularjs.cn/   英文API:http://docs.angularjs.cn/api     中文API;http://www.apjs.n ...

  10. Ural 1114 Boxes

    Boxes Time Limit: 600ms Memory Limit: 16384KB This problem will be judged on Ural. Original ID: 1114 ...