poj 1149 : PIGS

思路:

0.从源点向每个猪圈引一条边,容量为猪圈里的猪的数量。

1. 对于每个猪圈的顾客,从该猪圈向他连一条边,容量为猪圈里的猪的数量。

2. 对于每个猪圈,如果不是第一个顾客,则上一个打开这个猪圈的顾客向这个顾客连一条边,容量为 +∞。

3. 每个顾客到汇点连一条边,容量为各个顾客能买的数量。

 1 #include<iostream>
2 #include<map>
3 #include<string>
4 #include<queue>
5 using namespace std;
6
7 int c[1101][1101];
8 int flow[1101][1101];
9 int N,M;
10 int pre[1101],lastCus2Pighouse[1201] ;
11 int node;
12 void Init()
13 {
14 int i,j;
15 //freopen("a.txt","r",stdin);
16 memset(lastCus2Pighouse,0,sizeof(lastCus2Pighouse));
17 memset(c,0,sizeof(c));
18 cin>>M>>N;
19 node=2;
20 for(i=1;i<=M;++i)
21 {//建立源点到顾客的关系
22 cin>>c[0][node++];
23 }
24
25 for(i=M+2;i<=N+M+1;++i)
26 {
27 int A,key;
28 cin>>A;
29 for(j=1;j<=A;j++)
30 {
31 cin>>key;
32 c[key+1][node]=c[0][key+1];//pigHouse[key];
33 if(lastCus2Pighouse[key]!=0)
34 c[lastCus2Pighouse[key]][node]=INT_MAX;
35 lastCus2Pighouse[key]=i;
36 }
37 cin>>c[node++][1];
38 }
39 }
40
41 int Edmods_Karp()
42 {
43
44 int d[1000];//d是增广路长度,pre记录前驱
45 int i,j;
46
47 memset(flow,0,sizeof(flow));
48 while(true)
49 {
50 memset(pre,-1,sizeof(pre));
51 d[0]=0x7fffffff;
52 queue<int> que;
53 que.push(0);
54 while(!que.empty() && pre[1]<0)
55 {
56 int t=que.front();
57 que.pop();
58 for(i=0;i<node;++i)
59 {
60 if(pre[i]<0 && (j=c[t][i]-flow[t][i]))
61 {
62 pre[i]=t;
63 que.push(i);
64 d[i]=min(d[t],j);
65 }
66 }
67
68 }
69 if(pre[1]<0) break;
70 for(i=1;i!=0;i=pre[i])
71 {
72 flow[pre[i]][i]+=d[1];
73 flow[i][pre[i]]-=d[1];
74 }
75 }
76 for(j=i=0;i<node;j+=flow[0][i++]);
77 return j;
78 }
79 int main()
80 {
81 Init();
82 cout<<Edmods_Karp()<<endl;
83 return 0;
84 }

(ac了,不过差点超时,代码有很多地方值得优化,先记录下来)

原文地址:https://www.cnblogs.com/redlight/p/2429679.html