hdu2426Interesting Housing Problem
来源:互联网 发布:网络语言单词词缀 编辑:程序博客网 时间:2024/06/07 23:40
http://acm.hdu.edu.cn/showproblem.php?pid=2426
#include <iostream>#include <cstdio>using namespace std;const int oo=1e7;const int mm=111111;const int mn=8888;int node,src,dest,edge,ans;int ver[mm],flow[mm],cost[mm],next[mm];int head[mn],dis[mn],p[mn],q[mn],vis[mn],work[mn];void prepare(int _node,int _src,int _dest){ node=_node,src=_src,dest=_dest; for(int i=0;i<node;++i)head[i]=-1,vis[i]=0; edge=0;}void addedge(int u,int v,int f,int c){ ver[edge]=v,flow[edge]=f,cost[edge]=c,next[edge]=head[u],head[u]=edge++; ver[edge]=u,flow[edge]=0,cost[edge]=-c,next[edge]=head[v],head[v]=edge++;}bool spfa(){ int i,u,v,l,r=0,tmp; for(i=0;i<node;i++) dis[i]=-oo; dis[q[r++]=src]=0; p[src]=p[dest]=-1; for(l=0;l!=r;(++l>=mn)?l=0:l) for(i=head[u=q[l]],vis[u]=0;i>=0;i=next[i]) if(flow[i]&&dis[v=ver[i]]<(tmp=dis[u]+cost[i])) { dis[v]=tmp;//cout<<"u="<<u<<" v="<<v<<" tmp="<<tmp<<endl; p[v]=i^1; if(vis[v]) continue; vis[q[r++]=v]=1; if(r>=mn) r=0; } return p[dest]>-1;}int Spfaflow(){ int i,ret=0,delta; while(spfa()) { for(i=p[dest],delta=oo;i>=0;i=p[ver[i]]) if(flow[i^1]<delta) delta=flow[i^1]; ans+=delta; for(i=p[dest];i>=0;i=p[ver[i]]) flow[i]+=delta,flow[i^1]-=delta; ret+=delta*dis[dest];//cout<<"ret="<<ret<<endl; } return ret;}int main(){ int n,m,k,s,r,v,i,cas=0; while(~scanf("%d%d%d",&n,&m,&k)) { prepare(n+m+2,0,n+m+1); if(n>m) { printf("Case %d: -1\n",++cas); continue; } for(i=1;i<=n;i++) addedge(src,i,1,0); for(i=1;i<=m;i++) addedge(n+i,dest,1,0); while(k--) { scanf("%d%d%d",&s,&r,&v); s++; r++; if(v>=0) addedge(s,n+r,oo,v); } ans=0; int kk=Spfaflow(); if(ans==n) printf("Case %d: %d\n",++cas,kk); else printf("Case %d: -1\n",++cas); } return 0;}
- hdu2426Interesting Housing Problem
- Interesting Housing Problem----KM
- hdu2426 Interesting Housing Problem
- HDU 2426 Interesting Housing Problem
- HDU-2426 Interesting Housing Problem
- hdu 2426 Interesting Housing Problem
- HDU 2426 Interesting Housing Problem
- HDU 2426 Interesting Housing Problem
- hdu2426——Interesting Housing Problem
- hdu 2426 Interesting Housing Problem (KM)
- hdu2426 Interesting Housing Problem (KM邻接表)
- hdu 2426 Interesting Housing Problem (KM算法)
- hdu 2426 Interesting Housing Problem(KM)
- HDU2426 Interesting Housing Problem(KM匹配 )
- hdu 2426 Interesting Housing Problem【KM】
- POJ 2426 Interesting Housing Problem (最大费用最大流)
- hdu 2426 Interesting Housing Problem(最大权值匹配)
- HDU 2426 Interesting Housing Problem(二分图最优匹配)
- 根据前序遍历和中序遍历结果构造二叉树
- mini2440的串口在Qt上实现
- 禁闭岛》剧情全解,结局只有一个,导演小细节处...
- android openGL ES 01
- POJ 1753
- hdu2426Interesting Housing Problem
- ATL Internals 2ed复习.chapter 4.Creators
- Netbeans JVM设置
- 硬盘管理工具Acronis Disk Director Suite 10.0使用方法(图文介绍)
- 几种选择全排列实现的效率
- 中小型嵌入式系统开发常用开源资源导航
- 【动态规划】武器分配
- 笔记本外接显示器,同时使用双显示器
- C++中四种常用的强制转换关键字