Link:Http://poj.org/problem? Id = 3469
Question:There is a dual-core CPU, N modules need to be processed on the CPU, the consumption of running on two cores is Ai and Bi, and m-to-module needs to share data, if they run on the same CPU, the cost of data sharing is negligible. Otherwise, additional costs are required. Calculates the minimum total consumption.
Ideas:Two CPUs are regarded as the source and sink points, and the module is considered as the vertex in the figure. For each AI and Bi, an arc with a capacity of AI can be connected from the source point to I, from I to connect an arc with a capacity of Bi to a sink point, when data needs to be shared between two modules, two arcs are connected between them, forward and reverse, and the capacity is additional cost, at this time, each vertex is connected to the Source Vertex and sink vertex, that is, each vertex can run in any CPU.
In this way, for any cut in the graph, the source and sink are not connected, so each vertex cannot be connected to the source and sink at the same time, that is, each vertex can only run in one CPU. At this time, the cost is the cut capacity. To minimize the cut capacity, you only need the maximum stream of the source image according to the max flow min cut theorem.
This time I wrote down yesterday's optimization, t... I used the original code, but AC. Although it was still relatively slow for nearly 10 s, I decided to use the original method to write more pictures with more sides in the future, less vertices are written in a way similar to multi-channel augmented optimization.
#include<cstring>#include<string>#include<fstream>#include<iostream>#include<iomanip>#include<cstdio>#include<cctype>#include<algorithm>#include<queue>#include<map>#include<set>#include<vector>#include<stack>#include<ctime>#include<cstdlib>#include<functional>#include<cmath>using namespace std;#define PI acos(-1.0)#define MAXN 500100#define eps 1e-7#define INF 0x7FFFFFFF#define LLINF 0x7FFFFFFFFFFFFFFF#define seed 131#define mod 1000000007#define ll long long#define ull unsigned ll#define lson l,m,rt<<1#define rson m+1,r,rt<<1|1struct node{ int u,w,next;}edge[MAXN];int head[20100],dist[20100];int cnt,n,m,src,sink;void add_edge(int a,int b,int c){ edge[cnt].u = b; edge[cnt].w = c; edge[cnt].next = head[a]; head[a] = cnt++;}bool bfs(){ int i,j; memset(dist,-1,sizeof(dist)); dist[src] = 1; queue<int>q; q.push(src); while(!q.empty()){ int u = q.front(); q.pop(); for(i=head[u];i!=-1;i=edge[i].next){ int v = edge[i].u; if(dist[v]==-1&&edge[i].w){ dist[v] = dist[u] + 1; q.push(v); } } } if(dist[sink]==-1) return false; else return true;}int dfs(int u,int delta){ int i,j,dd; if(u==sink) return delta; int ret = 0; for(i=head[u];i!=-1;i=edge[i].next){ int v = edge[i].u; if(dist[v]==dist[u]+1&&edge[i].w){ dd = dfs(v,min(edge[i].w,delta)); edge[i].w -= dd; edge[i^1].w += dd; ret += dd; delta -= dd; } } return ret;}int main(){ int i,j; int a,b,c; memset(head,-1,sizeof(head)); scanf("%d%d",&n,&m); cnt = 0; src = 0; sink = n + 1; for(i=1;i<=n;i++){ scanf("%d%d",&a,&b); add_edge(src,i,a); add_edge(i,src,0); add_edge(i,sink,b); add_edge(sink,i,0); } for(i=0;i<m;i++){ scanf("%d%d%d",&a,&b,&c); add_edge(a,b,c); add_edge(b,a,c); } int ans = 0; while(bfs()){ ans += dfs(src,INF); } printf("%d\n",ans); return 0;}