您的位置:首页 > 其它

poj 3469 Dual Core CPU

2017-01-22 11:44 519 查看
Dual Core CPU

Time Limit: 15000MS Memory Limit: 131072K

Total Submissions: 23776 Accepted: 10335

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

【分析】

dinic多路增广好(裸)题

【代码】

//poj 3469
#include<iostream>
#include<cstring>
#include<cstdio>
#include<queue>
#define inf 1e9+7
#define fo(i,j,k) for(i=j;i<=k;i++)
using namespace std;
const int mxn=200005;
int cnt,n,m,ans,tans;
int head[mxn],dis[mxn];
queue <int> q;
struct node {int next,to,flow;} f[mxn<<3];
inline void add(int u,int v,int flow)
{
f[++cnt].to=v;
f[cnt].flow=flow;
f[cnt].next=head[u];
head[u]=cnt;
}
inline bool bfs()
{
memset(dis,-1,sizeof dis);
q.push(0);
dis[0]=0;
while(!q.empty())
{
int u=q.front();
q.pop();
for(int i=head[u];i;i=f[i].next)
{
int v=f[i].to;
if(dis[v]<0 && f[i].flow>0)
{
q.push(v);
dis[v]=dis[u]+1;
}
}
}
if(dis[n+1]>0) return 1;
return 0;
}
inline int find(int u,int now)
{
int a,v,i,sum=0;
if(u==n+1) return now;
for(i=head[u];i;i=f[i].next)
{
v=f[i].to;
if(now>sum && f[i].flow>0 && dis[v]==dis[u]+1 && (a=find(v,min(now-sum,f[i].flow))))
{
f[i].flow-=a;
if(i&1) f[i+1].flow+=a;
else f[i-1].flow+=a;
sum+=a;
}
}
return sum;
}
int main()
{
int i,j,u,v,w;
scanf("%d%d",&n,&m);
fo(i,1,n)
{
scanf("%d%d",&u,&v);
add(0,i,u),add(i,0,0);
add(i,n+1,v),add(n+1,i,0);
}
fo(i,1,m)
{
scanf("%d%d%d",&u,&v,&w);
add(u,v,w),add(v,u,w);
}
while(bfs())
while(tans=find(0,inf))
ans+=tans;
printf("%d\n",ans);
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: