POJ 3469.Dual Core CPU 最大流dinic算法模板
Posted GeekZRF
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了POJ 3469.Dual Core CPU 最大流dinic算法模板相关的知识,希望对你有一定的参考价值。
Time Limit: 15000MS | Memory Limit: 131072K | |
Total Submissions: 24830 | Accepted: 10756 | |
Case Time Limit: 5000MS |
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 as Ai and Bi. 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 extra w 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
Source
#include<iostream> #include<cstdio> #include<cmath> #include<cstring> #include<algorithm> #include<set> #include<map> #include<queue> #include<stack> #include<vector> using namespace std; #define PI acos(-1.0) typedef long long ll; typedef pair<int,int> P; const int maxn=1e5+100,maxm=1e5+100,inf=0x3f3f3f3f,mod=1e9+7; const ll INF=1e18+7; priority_queue<P,vector<P>,greater<P> >que; struct edge { int from,to; int cap; int rev;///方向边的编号 }; vector<edge>G[maxn];///邻接表存图 int iter[maxn];///当前弧,在其之前的边已经没有了作用 int level[maxn];///层次 void addedge(int u,int v,int c) { edge e; e.from=u,e.to=v,e.cap=c,e.rev=G[v].size(); G[u].push_back(e); e.from=v,e.to=u,e.cap=0,e.rev=G[u].size()-1; G[v].push_back(e); } int bfs(int s,int t) { memset(level,-1,sizeof(level)); queue<int>q; level[s]=0; q.push(s); while(!q.empty()) { int u=q.front(); q.pop(); for(int i=0; i<G[u].size(); i++) { edge e=G[u][i]; if(e.cap>0&&level[e.to]<0) { level[e.to]=level[u]+1; q.push(e.to); } } } if(level[t]<0) return 0; else return 1; } int dfs(int u,int t,int f) { if(u==t||f==0) return f; int flow=0; for(int &i=iter[u]; i<G[u].size(); i++) { edge e=G[u][i]; if(e.cap>0&&level[u]+1==level[e.to]) { int d=dfs(e.to,t,min(f,e.cap)); if(d>0) { G[u][i].cap-=d; G[e.to][e.rev].cap+=d; flow+=d; f-=d; if(f==0) break; } } } return flow; } int max_flow(int s,int t) { int flow=0; while(bfs(s,t)) { memset(iter,0,sizeof(iter)); flow+=dfs(s,t,inf); } return flow; } int main() { int n,m; scanf("%d%d",&n,&m); int s=0,t=n+1; for(int i=1; i<=n; i++) { int a,b; scanf("%d%d",&a,&b); addedge(s,i,a); addedge(i,t,b); } for(int i=1; i<=m; i++) { int a,b,w; scanf("%d%d%d",&a,&b,&w); addedge(a,b,w); addedge(b,a,w); } cout<<max_flow(s,t)<<endl; return 0; }
以上是关于POJ 3469.Dual Core CPU 最大流dinic算法模板的主要内容,如果未能解决你的问题,请参考以下文章