您的位置:首页 > 理论基础 > 计算机网络

poj 3469 Dual Core CPU (网络流模型)

2014-11-07 10:00 274 查看
Description

AsmoreandmorecomputersareequippedwithdualcoreCPU,SetagLilb,theChiefTechnologyOfficerofTinySoftCorporation,decidedtoupdatetheirfamousproduct-SWODNIW.

TheroutineconsistsofNmodules,andeachofthemshouldruninacertaincore.Thecostsforalltheroutinestoexecuteontwocoreshasbeenestimated.Let'sdefinethemasAiandBi.Meanwhile,
Mpairsofmodulesneedtodosomedata-exchange.Iftheyarerunningonthesamecore,thenthecostofthisactioncanbeignored.Otherwise,someextracostareneeded.Youshouldarrangewiselytominimizethetotalcost.

Input

Therearetwointegersinthefirstlineofinputdata,NandM(1≤N≤20000,1≤M≤200000).

ThenextNlines,eachcontainstwointeger,Aiand
Bi.

InthefollowingMlines,eachcontainsthreeintegers:a,
b,w.Themeaningisthatifmoduleaandmodulebdon'texecuteonthesamecore,youshouldpayextrawdollarsforthedata-exchangebetweenthem.

Output

Outputonlyoneinteger,theminimumtotalcost.

SampleInput

31
110
210
103
231000


SampleOutput

13


题意:一台双核电脑,给你多个任务,分别给出每个任务在第一个核和第二个核上运行的消耗。后面的m行输入是给出两个任务在两个不同核上运行需要付出的额外消耗。
建图:把每个任务作为节点,在超级源点与任务间的连一条边,其容量为给任务在核1上运行的消耗,在该任务节点与超级汇点之间连一条边,容量为该任务在核2上运行的消耗。
在任务之间连接无向边,容量为两个任务不在同一核上运行的额外消耗。


#include<iostream>
#include<queue>
#include<string.h>
usingnamespacestd;
constintMAXN=20010;//点数的最大值
constintMAXM=880010;//边数的最大值
constintINF=0x7fffffff;
structnode
{
intst,en,con,next;
}edge[MAXM];
intN,M,s,t;
intn;//n是总的点的个数,包括源点和汇点
inttot=0;//边表的下标即边的个数
inthead[MAXN];//head[u]表示顶点u第一条邻接边的序号,若head[u]=-1,u没有邻接边
intdep[MAXN];
intgap[MAXN];//gap[x]=y:说明残留网络中dep[i]==x的个数为y
voidaddedge(intu,intv,intw)
{
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++;
}
voidBFS(intstart,intend)
{
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())
{
intu=Q.front();
Q.pop();
for(inti=head[u];i!=-1;i=edge[i].next)
{
intv=edge[i].en;
if(dep[v]!=-1)continue;
Q.push(v);
dep[v]=dep[u]+1;
++gap[dep[v]];
}
}
}
intSAP(intstart,intend)
{
intres=0;
BFS(start,end);
intcur[MAXN];
intS[MAXN];
inttop=0;
memcpy(cur,head,sizeof(head));
intu=start;
inti;
while(dep[start]<n)
{
if(u==end)
{
inttemp=INF;
intinser;
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
{
intmin=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;
}
}
returnres;
}
intmain()
{
cin>>N>>M;
s=0;t=N+1;n=N+1;
intAi,Bi,a,b,w;
memset(head,-1,sizeof(head));
for(inti=1;i<=N;i++)
{
cin>>Ai>>Bi;
addedge(s,i,Ai);
addedge(i,t,Bi);
}
for(inti=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;
return0;
}


-------------------------------------------------------------------------------------------------------------


#include<iostream>
#include<string.h>
#include<queue>
#defineM6050000
#defineN320000
#defineinf0x3f3f3f3f
usingnamespacestd;
structnode
{
intv,w,next;
}edge[M];
inthead
,l
;
intcnt,n,m,s,t;//cnt是边的个数,s是源点,t是汇点

voidadd(intu,intv,intw)
{
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++;
}
intbfs()
{
memset(l,-1,sizeof(l));
l[s]=0;
inti,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);
}
}
}
returnl[t]>0;
}
intdfs(intu,intf)
{
inta,flow=0;
if(u==t)returnf;
for(inti=head[u];i!=-1;i=edge[i].next)
{
intv=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;//当前弧优化
returnflow;
}
intdinic()
{
inta,ans=0;
while(bfs())
while(a=dfs(s,inf))
ans+=a;
returnans;
}
intmain()
{
inti,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;
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: