poj 1149 pigs 增广路 ford
来源:互联网 发布:电驴下载软件 编辑:程序博客网 时间:2024/04/30 08:50
题目关键是建模,之后就是简单地增广路。
#include<stdio.h>#include<string.h>#include<algorithm>#include<vector>#include<queue>using namespace std;const int N=1024;const int inf=1<<24;struct{ int c,f;}g[1024][1024];int n,m,s,t;queue<int>q;void ford(){ int a[N],vis[N],p[N],maxflow=0; while(1) { memset(a,0,sizeof(a)); memset(p,0,sizeof(p)); memset(vis,0,sizeof(vis)); while(!q.empty()) q.pop(); q.push(0); a[0]=inf; vis[0]=1; p[0]=0; while(!q.empty()&&vis[t]==0) { int x=q.front(); //printf("%d\n",x); q.pop(); for(int i=0;i<=t;i++) { if(vis[i]!=0) continue; //printf("%d %d\n",i,g[x][i]); if(g[x][i].c<inf&&g[x][i].c>g[x][i].f) { p[i]=x; a[i]=min(a[x],g[x][i].c-g[x][i].f); vis[i]=1; q.push(i); } else if(g[i][x].c<inf&&g[i][x].f>0) { p[i]=x; a[i]=min(a[x],g[i][x].f); vis[i]=1; q.push(i); } } } if(vis[t]==0||a[t]==0) break; int k1=t,k2=p[k1]; int add=a[t]; while(1) { if(g[k2][k1].f<inf) g[k2][k1].f+=add; else g[k1][k2].f-=add; if(k2==0) break; k1=k2; k2=p[k1]; } } for(int i=0;i<t;i++) if(g[i][t].f<inf) maxflow+=g[i][t].f; printf("%d\n",maxflow);}int main(){ int i,j,num,k,a[1024],last[1024]; while(~scanf("%d%d",&m,&n)) { for(i=1;i<=m;i++) scanf("%d",&a[i]); for(i=0;i<1024;i++) for(j=0;j<1024;j++) g[i][j].c=g[i][j].f=inf; memset(last,0,sizeof(last)); s=0; t=n+1; for(i=1;i<=n;i++) { scanf("%d",&num); for(j=0;j<num;j++) { scanf("%d",&k); if(last[k]==0) { if(g[0][i].c==inf) g[0][i].c=a[k]; else g[0][i].c+=a[k]; g[0][i].f=0; } else g[last[k]][i].c=inf/2,g[last[k]][i].f=0; last[k]=i; } scanf("%d",&g[i][t].c); g[i][t].f=0; } ford(); } return 0;}
0 0
- poj 1149 pigs 增广路 ford
- poj 1149 PIGS(网络流+最大费用流+ford)
- 增广路算法Ford-Fulkerson
- PIGS(最大流_增广路算法)
- POJ 1149-PIGS(Ford-Fulkerson 标号法求网络最大流)
- hdu 1532 Drainage Ditches 增广路 ford
- POJ 1149 PIGS
- poj 1149 PIGS
- POJ 1149 PIGS //MAXFLOW
- POJ 1149 PIGS
- POJ 1149 PIGS
- poj 1149 pigs
- POJ 1149 PIGS
- poj 1149 PIGS
- POJ 1149 PIGS
- POJ-1149-PIGS
- POJ 1149 PIGS
- poj 1149 : PIGS
- 清远代办医院诊断书
- OC学习笔记七 Category
- 东莞代办医院诊断书
- 浅谈文字编码和Unicode
- 潮州代办医院诊断书
- poj 1149 pigs 增广路 ford
- 天声人語 20150813
- 南宁代办医院诊断书
- 柳州代办医院诊断书
- leetcode 114: Flatten Binary Tree to Linked List
- 桂林代办医院诊断书
- 【usaco/codevs2033/codevs1047/NOIP1999TG】 邮票问题浅谈
- POJ 2485 Highways(最小生成树prim算法)
- 梧州代办医院诊断书