POJ 2426 Interesting Housing Problem (最大费用最大流)
来源:互联网 发布:vmware linux nat上网 编辑:程序博客网 时间:2024/05/20 07:53
POJ 2426 Interesting Housing Problem (最大费用最大流)
#include <iostream>#include <cstdio>#include <queue>#include <cstdlib>using namespace std;const int maxn=30000;const int inf=200000000;struct edge{int u,v,next,f,c;edge(int u0=0,int v0=0,int f0=0,int c0=0,int next0=0){u=u0,v=v0,f=f0,c=c0,next=next0;}}e[maxn*10];int head[maxn*2],visited[maxn*2],path[maxn*2],dist[maxn*2];int cnt,from,to,marked,maxflow,offflow,n,r,m,casen;void initial(){to=2*n+2*r+1;casen++;cnt=0;marked=1;maxflow=0;from=0;for(int i=0;i<=to;i++){head[i]=-1;visited[i]=0;}}void adde(int u,int v,int f,int c){e[cnt].u=u,e[cnt].v=v,e[cnt].f=f,e[cnt].c=c,e[cnt].next=head[u],head[u]=cnt++;e[cnt].u=v,e[cnt].v=u,e[cnt].f=0,e[cnt].c=-c,e[cnt].next=head[v],head[v]=cnt++;}void input(){for(int i=1;i<=n;i++){adde(0,i,1,0);adde(i,i+n,1,0);}for(int i=1;i<=r;i++){adde(2*n+i,2*n+r+i,1,0);adde(2*n+r+i,to,1,0);}while(m-- >0){int u0,v0,c0;scanf("%d%d%d",&u0,&v0,&c0);if(c0>=0) adde(u0+1+n,2*n+v0+1,1,c0);}}void bfs(){ for(int i=0;i<=to;i++){ dist[i]=-inf; path[i]=-1; } dist[0]=0; queue <int> q; q.push(0); marked++; visited[0]=marked; while(!q.empty()){ int s=q.front(); q.pop(); for(int i=head[s];i!=-1;i=e[i].next){ int d=e[i].v; if(e[i].f>0 && dist[s]+e[i].c>dist[d]){ dist[d]=dist[s]+e[i].c; path[d]=i; if(visited[d]!=marked){ visited[d]=marked; q.push(d); } } } visited[s]=-1; }}void computing(){printf("Case %d: ",casen);if(r<n){cout<<"-1"<<endl;return;}for(int k=0;k<n;k++){ bfs(); /*cout<<to; for(int i=to;i!=from;i=e[path[i]].u){ cout<<"->"<<e[path[i]].u; } cout<<" ans:"<<dist[to]<<endl;*/ if(dist[to]<=-inf){ cout<<"-1"<<endl;return; }maxflow+=dist[to];for(int i=to;i!=from;i=e[path[i]].u){ e[path[i]].f-=1; e[path[i]^1].f+=1;}} printf("%d\n",maxflow);}int main(){while(cin>>n>>r>>m){initial();input();computing();}return 0;}
- POJ 2426 Interesting Housing Problem (最大费用最大流)
- hdoj--2426--Interesting Housing Problem(最大费用流)
- HDU 2426 Interesting Housing Problem【最大费用最大流 && 常规题】
- HDU 2426 Interesting Housing Problem 最小费用最大流 or KM算法
- HDU 2426 Interesting Housing Problem 二分匹配(KM模板)或者最小费用最大流
- hdoj 2246 Interesting Housing Problem 【最大费用最大流 or KM算法】
- 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
- HDU 2426 Interesting Housing Problem
- hdu 2426 Interesting Housing Problem (KM)
- hdu 2426 Interesting Housing Problem (KM算法)
- hdu 2426 Interesting Housing Problem(KM)
- hdu 2426 Interesting Housing Problem【KM】
- Interesting Housing Problem----KM
- hdu2426 Interesting Housing Problem
- mysql 基本操作
- SQL 存储过程的事务
- 遇到的一个关于IFS的问题
- myeclipse快捷键大全
- NSNotification相关
- POJ 2426 Interesting Housing Problem (最大费用最大流)
- Oracle常用命令
- iphone开发十几个实用demo合集
- asp.net服务器控件与html服务器控件的区别
- 使用Ctrl+Enter提交表单
- php 执行shell命令的函数
- hdu 1161 Eddy's mistakes
- android仿path下拉及刷新
- PHP读取csv文件的内容