题目地址:POJ 3469
建图思路:建源点与汇点,源点与CPU1相连,汇点与CPU2相连,对共享数据的之间连无向边。
我的ISAP过这题还是毫无时间压力的嘛。。。
代码如下:
#include <iostream> #include <cstdio> #include <string> #include <cstring> #include <stdlib.h> #include <math.h> #include <ctype.h> #include <queue> #include <map> #include <set> #include <algorithm> using namespace std; const int INF=0x3f3f3f3f; int head[30000], source, sink, nv, cnt; int cur[30000], num[30000], d[30000], pre[30000]; struct node { int u, v, cap, next; } edge[1000000]; void add(int u, int v, int cap) { edge[cnt].v=v; edge[cnt].cap=cap; edge[cnt].next=head[u]; head[u]=cnt++; edge[cnt].v=u; edge[cnt].cap=0; edge[cnt].next=head[v]; head[v]=cnt++; } void bfs() { memset(num,0,sizeof(num)); memset(d,-1,sizeof(d)); queue<int>q; q.push(sink); d[sink]=0; num[0]=1; while(!q.empty()) { int u=q.front(); q.pop(); for(int i=head[u]; i!=-1; i=edge[i].next) { int v=edge[i].v; if(d[v]==-1) { d[v]=d[u]+1; num[d[v]]++; q.push(v); } } } } void isap() { memcpy(cur,head,sizeof(cur)); int flow=0, u=pre[source]=source, i; bfs(); while(d[source]<nv) { if(u==sink) { int f=INF, pos; for(i=source; i!=sink; i=edge[cur[i]].v) { if(f>edge[cur[i]].cap) { f=edge[cur[i]].cap; pos=i; } } for(i=source; i!=sink; i=edge[cur[i]].v) { edge[cur[i]].cap-=f; edge[cur[i]^1].cap+=f; } flow+=f; u=pos; } for(i=cur[u]; i!=-1; i=edge[i].next) { if(d[edge[i].v]+1==d[u]&&edge[i].cap) break; } if(i!=-1) { cur[u]=i; pre[edge[i].v]=u; u=edge[i].v; } else { if(--num[d[u]]==0) break; int mind=nv; for(i=head[u]; i!=-1; i=edge[i].next) { if(mind>d[edge[i].v]&&edge[i].cap) { mind=d[edge[i].v]; cur[u]=i; } } d[u]=mind+1; num[d[u]]++; u=pre[u]; } } printf("%d\n",flow); } int main() { int n, m, i, x, y, z; scanf("%d%d",&n,&m); memset(head,-1,sizeof(head)); cnt=0; source=0; sink=n+1; nv=sink+1; for(i=1;i<=n;i++) { scanf("%d%d",&x,&y); add(source,i,x); add(i,sink,y); } while(m--) { scanf("%d%d%d",&x,&y,&z); add(x,y,z); add(y,x,z); } isap(); return 0; }
POJ 3469 Dual Core CPU(网络流之最小割)
时间: 2024-10-05 23:55:06