1. #include <iostream>
  2. #include <queue>
  3. #include <stdio.h>
  4. #include <string.h>
  5. #include <algorithm>
  6. #define inf 1000001
  7. using namespace std;
  8. struct node
  9. {
  10. int x,y,c,w;
  11. int next;
  12. } eg[];
  13. int n,m,K,s,t,tt,head[],dis[],pre[],v[];
  14. void init()
  15. {
  16. memset(head,-,sizeof(head));
  17. tt=;
  18. s=;
  19. t=n+m+;
  20. }
  21. void add(int xx,int yy,int cc,int ww)
  22. {
  23. eg[tt].x=xx;
  24. eg[tt].y=yy;
  25. eg[tt].c=cc;
  26. eg[tt].w=ww;
  27. eg[tt].next=head[xx];
  28. head[xx]=tt++;
  29. eg[tt].x=yy;
  30. eg[tt].y=xx;
  31. eg[tt].c=;
  32. eg[tt].w=-ww;
  33. eg[tt].next=head[yy];
  34. head[yy]=tt++;
  35. }
  36. int spfa(int s,int t)
  37. {
  38. for(int i=s; i<=t; i++)
  39. {
  40. v[i]=;
  41. dis[i]=inf;
  42. pre[i]=-;
  43. }
  44. queue<int>q;
  45. q.push(s);
  46. dis[s]=;
  47. while(!q.empty())
  48. {
  49. int f=q.front();
  50. q.pop();
  51. v[f]=;
  52. for(int i=head[f]; i!=-; i=eg[i].next)
  53. {
  54. int w1=eg[i].y;
  55. if(eg[i].c&&dis[w1]>dis[f]+eg[i].w)
  56. {
  57. dis[w1]=dis[f]+eg[i].w;
  58. pre[w1]=i;
  59. if(!v[w1])
  60. {
  61. v[w1]=;
  62. q.push(w1);
  63. }
  64. }
  65. }
  66. }
  67. if(dis[t]==inf)
  68. {
  69. return ;
  70. }
  71. return ;
  72. }
  73. void KM(int s,int t)
  74. {
  75. int minx,ans=,V=;
  76. while(spfa(s,t)==)
  77. {
  78. minx=inf;
  79. for(int i=pre[t]; i!=-; i=pre[eg[i].x])
  80. {
  81. minx=min(minx,eg[i].c);
  82. }
  83. for(int i=pre[t]; i!=-; i=pre[eg[i].x])
  84. {
  85. eg[i].c-=minx;
  86. eg[i+].c+=minx;
  87. }
  88. V+=minx;
  89. ans+=minx*dis[t];
  90. }
  91. if(V>=n)
  92. printf("%d\n",-ans);
  93. else printf("-1\n");
  94. return ;
  95. }
  96. int main()
  97. {
  98. int xx,yy,zz,T=;
  99. while(scanf("%d%d%d",&n,&m,&K)!=EOF)
  100. {
  101. init();
  102. while(K--)
  103. {
  104. scanf("%d%d%d",&xx,&yy,&zz);
  105. xx++;
  106. yy++;
  107. if(zz<)continue;
  108. add(xx,yy+n,,-zz);
  109. }
  110. for(int i=; i<=n; i++)
  111. {
  112. add(s,i,,);
  113. }
  114. for(int j=n+; j<=n+m; j++)
  115. {
  116. add(j,t,,);
  117. }
  118. printf("Case %d: ",++T);
  119. KM(s,t);
  120. }
  121. return ;
  122. }

HDU2426:Interesting Housing Problem(还没过,貌似入门题)的更多相关文章

  1. hdu 2426 Interesting Housing Problem 最大权匹配KM算法

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2426 For any school, it is hard to find a feasible ac ...

  2. HDU 2426 Interesting Housing Problem (最大权完美匹配)【KM】

    <题目链接> 题目大意: 学校里有n个学生和m个公寓房间,每个学生对一些房间有一些打分,如果分数为正,说明学生喜欢这个房间,若为0,对这个房间保持中立,若为负,则不喜欢这个房间.学生不会住 ...

  3. HDU 2426 Interesting Housing Problem(二分图最佳匹配)

    http://acm.hdu.edu.cn/showproblem.php?pid=2426 题意:每n个学生和m个房间,现在要为每个学生安排一个房间居住,每个学生对于一些房间有一些满意度,如果满意度 ...

  4. Codeforces Round #603 (Div. 2) A. Sweet Problem(水.......没做出来)+C题

    Codeforces Round #603 (Div. 2) A. Sweet Problem A. Sweet Problem time limit per test 1 second memory ...

  5. A Simple Problem with Integers(线段树入门题)

    Description You have N integers, A1, A2, ... , AN. You need to deal with two kinds of operations. On ...

  6. 在各方面还没准备好的时候,大家一定要慎用border-box样式!!!!

    这几天,我被一个js问题困扰到癫狂了! 事情是这样的,我之前写了个功能非常复杂的纯jquery代码的前端gridview控件,实现了大量的功能和效果,在一些项目里也用得很好. 最近有个项目,样式做了调 ...

  7. 成功熬了四年还没死?一个IT屌丝创业者的深刻反思

    三个IT屌丝创业的故事 从前有三个屌丝,聚在一起做网络.提供免费的网络服务,砸锅卖铁,通宵达旦,除了卖肾,啥都做了.3年后终于做到了五百万用户.对于年轻人来说,能把五百万人玩弄于鼓掌之间,已经是很牛逼 ...

  8. php大力力 [013节]mySQL数据库乱码问题我还没解决

    <?php echo"测试<br>"; $sql_connection = mysql_connect("localhost","e ...

  9. 你好,C++(21)只要天还没黑,就一直在工地干活-4.3.1 while循环:只要…就一直…

    4.3  循环控制语句 在现实世界中,有这样一类现象: 只要油箱中的当前油量小于油箱容量100升,就一直往油箱中加油: 一直不断地为祖国辛勤工作,只要我还活着: 公司100000位员工,每个人的工资都 ...

随机推荐

  1. .NETFramework、C#、VisualStudio 这三者之间关系,你了解吗!

    .NetFrameWork 是微软开发的以"虚拟机"运行,以通用语言运行库为基础,在其上面进行各种语言开发的一个开发平台. C# 是一个和平台更好交互,以托管在虚拟机上的一个语法糖 ...

  2. ubuntu 安装source insight

    1. 首先安装wine sudo apt-get install wine 2.下载source insight 安装包(.exe) 3,将安装包放到已知的目录下. 4.在终端进行安装,wine Si ...

  3. 获取form表单数据

    var modelObj = {}; var modelFieldsArray = $('#AddMusic').serializeArray(); $.each(modelFieldsArray, ...

  4. Linux-Oracle

    1.使用Oracle登录,或者其他用户登录,切换到Oracle账户下: 2.登录后在Oracle主目录后使用vi创建.bashrc文件: 3.在文件中输入如下参数: export ORACLE_SID ...

  5. 07python之字符串的常用方法

    字符串作为python中常用的数据类型,掌握字符串的常用方法十分必要. 常用知识点: 1.字符串的3种格式化方法 2.字符串的strip()方法 3.字符串的join()方法 4.字符串可以切片 1. ...

  6. vs2010中使用luabind

    第一部分:在vs2010中生成luabind静态库和动态库 一.前期准备 1.安装boost 下载boost并解压到 D:\mylua\boost_1_56_0,进入 D:\mylua\boost_1 ...

  7. LeetCode——Pascal's Triangle II

    Description: Given an index k, return the kth row of the Pascal's triangle. For example, given k = 3 ...

  8. 使用Android Studio调试内存问题

    http://blog.csdn.net/yutao52shi/article/details/50055669 前言 内存问题对于Android开发者是永远的痛.如果一个android程序员说他没有 ...

  9. JZOJ.5328【NOIP2017模拟8.22】世界线

    Description

  10. 关于hql语句的一些问题

    1.student is not mapped问题: 在执行显示数据库数据的时候出错 大概提示说: errors: s.entr_Id student is not mapped 碰到这种情况一般是: ...