Description
As more and more computers are equipped with dual core CPU, SetagLilb, the Chief Technology Officer of TinySoft Corporation, decided to update their famous product - SWODNIW.
The routine consists of N modules, and each of them should run in a certain core. The costs for all the routines to execute on two cores has been estimated. Let's define them asAi andBi. Meanwhile, M pairs of modules need to do some data-exchange. If they are running on the same core, then the cost of this action can be ignored. Otherwise, some extra cost are needed. You should arrange wisely to minimize the total cost.
Input
There are two integers in the first line of input data, N and M (1 ≤N ≤ 20000, 1 ≤M ≤ 200000) .
The next N lines, each contains two integer, Ai and Bi.
In the following M lines, each contains three integers: a, b, w. The meaning is that if module a and module b don't execute on the same core, you should pay extraw dollars for the data-exchange between them.
Output
Output only one integer, the minimum total cost.
Sample Input
3 1 1 10 2 10 10 3 2 3 1000
Sample Output
13
题意:一台双核电脑,给你多个任务,分别给出每个任务在第一个核和第二个核上运行的消耗。后面的m行输入是给出两个任务在两个不同核上运行需要付出的额外消耗。
建图:把每个任务作为节点,在超级源点与任务间的连一条边,其容量为给任务在核1上运行的消耗,在该任务节点与超级汇点之间连一条边,容量为该任务在核2上运行的消耗。
在任务之间连接无向边,容量为两个任务不在同一核上运行的额外消耗。
#include <iostream> #include <queue> #include <string.h> using namespace std; const int MAXN=20010;//点数的最大值 const int MAXM=880010;//边数的最大值 const int INF=0x7fffffff; struct node { int st,en,con,next; }edge[MAXM]; int N,M,s,t; int n; //n是总的点的个数,包括源点和汇点 int tot=0; //边表的下标即边的个数 int head[MAXN]; // head[u]表示顶点u第一条邻接边的序号, 若head[u] = -1, u没有邻接边 int dep[MAXN]; int gap[MAXN];//gap[x]=y :说明残留网络中dep[i]==x的个数为y void addedge(int u,int v,int w) { edge[tot].st=u; //添加正向边u->v edge[tot].en=v; edge[tot].con=w; edge[tot].next=head[u]; head[u]=tot++; edge[tot].st=v; //添加反向边v->u edge[tot].en=u; edge[tot].con=0; edge[tot].next=head[v]; head[v]=tot++; }
void BFS(int start,int end) { memset(dep,-1,sizeof(dep)); memset(gap,0,sizeof(gap)); gap[0]=1; dep[end]=0; queue<int> Q; Q.push(end); while(!Q.empty()) { int u=Q.front(); Q.pop(); for(int i=head[u];i!=-1;i=edge[i].next) { int v=edge[i].en; if(dep[v]!=-1)continue; Q.push(v); dep[v]=dep[u]+1; ++gap[dep[v]]; } } }
int SAP(int start,int end) { int res=0; BFS(start,end); int cur[MAXN]; int S[MAXN]; int top=0; memcpy(cur,head,sizeof(head)); int u=start; int i; while(dep[start]<n) { if(u==end) { int temp=INF; int inser; for(i=0;i<top;i++) if(temp>edge[S[i]].con) { temp=edge[S[i]].con; inser=i; } for(i=0;i<top;i++) { edge[S[i]].con-=temp; edge[S[i]^1].con+=temp; } res+=temp; top=inser; u=edge[S[top]].st; } if(u!=end&&gap[dep[u]-1]==0)//出现断层,无增广路 break; for(i=cur[u];i!=-1;i=edge[i].next) if(edge[i].con!=0&&dep[u]==dep[edge[i].en]+1) break; if(i!=-1) { cur[u]=i; S[top++]=i; u=edge[i].en; } else { int min=n; for(i=head[u];i!=-1;i=edge[i].next) { if(edge[i].con==0)continue; if(min>dep[edge[i].en]) { min=dep[edge[i].en]; cur[u]=i; } } --gap[dep[u]]; dep[u]=min+1; ++gap[dep[u]]; if(u!=start)u=edge[S[--top]].st; } } return res; } int main() { cin>>N>>M; s=0; t=N+1; n=N+1; int Ai,Bi,a,b,w; memset(head,-1,sizeof(head)); for(int i=1;i<=N;i++) { cin>>Ai>>Bi; addedge(s,i,Ai); addedge(i,t,Bi); } for(int i=1;i<=M;i++) { cin>>a>>b>>w; edge[tot].st=a; //添加正向边u->v edge[tot].en=b; edge[tot].con=w; edge[tot].next=head[a]; head[a]=tot++; edge[tot].st=b; //添加反向边v->u edge[tot].en=a; edge[tot].con=w; edge[tot].next=head[b]; head[b]=tot++; } cout<<SAP(s,t)<<endl; return 0; }
-------------------------------------------------------------------------------------------------------------
#include <iostream> #include <string.h> #include <queue> #define M 6050000 #define N 320000 #define inf 0x3f3f3f3f using namespace std; struct node { int v,w,next; }edge[M]; int head[N],l[N]; int cnt,n,m,s,t; //cnt是边的个数,s是源点,t是汇点
void add(int u,int v,int w) { edge[cnt].v=v; //添加正向边 edge[cnt].w=w; edge[cnt].next=head[u]; head[u]=cnt++;
edge[cnt].v=u; //添加反向边 edge[cnt].w=0; edge[cnt].next=head[v]; head[v]=cnt++; }
int bfs() { memset(l,-1,sizeof(l)); l[s]=0; int i,u,v; queue<int >Q; Q.push(s); while(!Q.empty()) { u=Q.front(); Q.pop(); for(i=head[u]; i!=-1; i=edge[i].next) { v=edge[i].v; if(l[v]==-1&&edge[i].w) { l[v]=l[u]+1; Q.push(v); } } } return l[t]>0; }
int dfs(int u,int f) { int a,flow=0; if(u==t)return f; for(int i=head[u]; i!=-1; i=edge[i].next) { int v=edge[i].v; if(l[v]==l[u]+1&&edge[i].w&&(a=dfs(v,min(f,edge[i].w)))) { edge[i].w-=a; edge[i^1].w+=a; flow+=a;//多路增广 f-=a; if(!f)break; } } if(!flow)l[u]=-1;//当前弧优化 return flow; }
int dinic() { int a,ans=0; while(bfs()) while(a=dfs(s,inf)) ans+=a; return ans; }
int main() { int i,j,u,v,w; while(cin>>n>>m) { memset(head,-1,sizeof(head)); cnt=0; s=0; t=n+1; for(i=1; i<=n; i++) { cin>>u>>v; add(s,i,u); add(i,t,v); } for(i=1; i<=m; i++) { cin>>u>>v>>w; edge[cnt].v=v; edge[cnt].w=w; edge[cnt].next=head[u]; head[u]=cnt++;
edge[cnt].v=u; edge[cnt].w=w; edge[cnt].next=head[v]; head[v]=cnt++; } cout<<dinic()<<endl; } }