HDU2426:Interesting Housing Problem(还没过,貌似入门题)
#include <iostream>
#include <queue>
#include <stdio.h>
#include <string.h>
#include <algorithm>
#define inf 1000001
using namespace std;
struct node
{
int x,y,c,w;
int next;
} eg[];
int n,m,K,s,t,tt,head[],dis[],pre[],v[];
void init()
{
memset(head,-,sizeof(head));
tt=;
s=;
t=n+m+;
}
void add(int xx,int yy,int cc,int ww)
{
eg[tt].x=xx;
eg[tt].y=yy;
eg[tt].c=cc;
eg[tt].w=ww;
eg[tt].next=head[xx];
head[xx]=tt++;
eg[tt].x=yy;
eg[tt].y=xx;
eg[tt].c=;
eg[tt].w=-ww;
eg[tt].next=head[yy];
head[yy]=tt++;
}
int spfa(int s,int t)
{
for(int i=s; i<=t; i++)
{
v[i]=;
dis[i]=inf;
pre[i]=-;
}
queue<int>q;
q.push(s);
dis[s]=;
while(!q.empty())
{
int f=q.front();
q.pop();
v[f]=;
for(int i=head[f]; i!=-; i=eg[i].next)
{
int w1=eg[i].y;
if(eg[i].c&&dis[w1]>dis[f]+eg[i].w)
{
dis[w1]=dis[f]+eg[i].w;
pre[w1]=i;
if(!v[w1])
{
v[w1]=;
q.push(w1);
}
}
}
}
if(dis[t]==inf)
{
return ;
}
return ;
}
void KM(int s,int t)
{
int minx,ans=,V=;
while(spfa(s,t)==)
{
minx=inf;
for(int i=pre[t]; i!=-; i=pre[eg[i].x])
{
minx=min(minx,eg[i].c);
}
for(int i=pre[t]; i!=-; i=pre[eg[i].x])
{
eg[i].c-=minx;
eg[i+].c+=minx;
}
V+=minx;
ans+=minx*dis[t];
}
if(V>=n)
printf("%d\n",-ans);
else printf("-1\n");
return ;
}
int main()
{
int xx,yy,zz,T=;
while(scanf("%d%d%d",&n,&m,&K)!=EOF)
{
init();
while(K--)
{
scanf("%d%d%d",&xx,&yy,&zz);
xx++;
yy++;
if(zz<)continue;
add(xx,yy+n,,-zz);
}
for(int i=; i<=n; i++)
{
add(s,i,,);
}
for(int j=n+; j<=n+m; j++)
{
add(j,t,,);
}
printf("Case %d: ",++T);
KM(s,t);
}
return ;
}
HDU2426:Interesting Housing Problem(还没过,貌似入门题)的更多相关文章
- 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 ...
- HDU 2426 Interesting Housing Problem (最大权完美匹配)【KM】
<题目链接> 题目大意: 学校里有n个学生和m个公寓房间,每个学生对一些房间有一些打分,如果分数为正,说明学生喜欢这个房间,若为0,对这个房间保持中立,若为负,则不喜欢这个房间.学生不会住 ...
- HDU 2426 Interesting Housing Problem(二分图最佳匹配)
http://acm.hdu.edu.cn/showproblem.php?pid=2426 题意:每n个学生和m个房间,现在要为每个学生安排一个房间居住,每个学生对于一些房间有一些满意度,如果满意度 ...
- 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 ...
- A Simple Problem with Integers(线段树入门题)
Description You have N integers, A1, A2, ... , AN. You need to deal with two kinds of operations. On ...
- 在各方面还没准备好的时候,大家一定要慎用border-box样式!!!!
这几天,我被一个js问题困扰到癫狂了! 事情是这样的,我之前写了个功能非常复杂的纯jquery代码的前端gridview控件,实现了大量的功能和效果,在一些项目里也用得很好. 最近有个项目,样式做了调 ...
- 成功熬了四年还没死?一个IT屌丝创业者的深刻反思
三个IT屌丝创业的故事 从前有三个屌丝,聚在一起做网络.提供免费的网络服务,砸锅卖铁,通宵达旦,除了卖肾,啥都做了.3年后终于做到了五百万用户.对于年轻人来说,能把五百万人玩弄于鼓掌之间,已经是很牛逼 ...
- php大力力 [013节]mySQL数据库乱码问题我还没解决
<?php echo"测试<br>"; $sql_connection = mysql_connect("localhost","e ...
- 你好,C++(21)只要天还没黑,就一直在工地干活-4.3.1 while循环:只要…就一直…
4.3 循环控制语句 在现实世界中,有这样一类现象: 只要油箱中的当前油量小于油箱容量100升,就一直往油箱中加油: 一直不断地为祖国辛勤工作,只要我还活着: 公司100000位员工,每个人的工资都 ...
随机推荐
- error C2065:!错误:未定义标识符“pBuf);”
error C2065: “pBuf):”: 未声明的标识符 错误原因:第二个括号)使用的是中文符号!还有最后那个分号! 改回来就好了~ 原错误: 修正后错误消失:
- POSIX是什么?
1.什么是POSIX? POSIX是可移植操作系统接口(Portable Operating System Interface for UNIX)的缩写,是IEEE为了在各种UNIX操作系统上运行软件 ...
- VC++ ToolTip的简单使用
1.在基于对话框的MFC应用程序中使用Tooltip,首先在Dlg类的头文件中定义一个变量: CToolTipCtrl m_iToolTips; 2.在Dlg类的OnInitDialog中添加代码: ...
- Python图像处理库PIL的ImageSequence模块介绍
ImageSequence模块包括了一个wrapper类,它能够让用户迭代訪问图形序列中每一帧图像. 一.ImageSequence模块的函数 1. Iterator 定义:ImageSequenc ...
- org.springframework.beans.factory.xml.XmlBeanDefinitionStoreException: Line 12 in XML document from
org.springframework.beans.factory.xml.XmlBeanDefinitionStoreException: Line 12 in XML document from ...
- python2.0_s12_day9_协程&多线程和cpu,磁盘io之间的关系
事件驱动和异步io有什么直接关系. 当我们访问一个网页,不考虑网络问题.我们人类不觉得网页慢. 但是实际中对计算机来说还是慢.那慢在哪里.io io操作是整个网络操作中最慢的.比如你打开网页要是有2秒 ...
- Python 内部类
内部类也就是在类的内部再定义类,如下: #!/usr/bin/env python #-*- coding:utf-8 -*- class People(object): class Chinese( ...
- TextSwitcher实现文本自动垂直滚动
实现功能:用TextSwitcher实现文本自动垂直滚动,类似淘宝首页广告条. 实现效果: 注意:由于网上横向滚动的例子比较多,所以这里通过垂直的例子演示. 实现步骤:1.extends TextSw ...
- PyQt4文件对话框QFileDialog
文件对话框允许用户选择文件或文件夹,被选择的文件可进行读或写操作. #!/usr/bin/python # -*- coding: utf-8 -*- import sys from PyQt4 im ...
- LeetCode——Search a 2D Matrix II
Description: Write an efficient algorithm that searches for a value in an m x n matrix. This matrix ...