POJ 3469 Dual Core CPU
來源:程序員人生 發(fā)布時間:2014-10-02 08:00:01 閱讀次數(shù):2571次
一堆任務(wù)分配到2個不同的芯片上運行,一個任務(wù)在不同芯片上運行的時間不一樣,有一些任務(wù)組如果分配到不同的芯片上運行會產(chǎn)生額外的時間....
用最小的費用將不同對象劃分到兩個集合 , 最小割問題 .
建圖:
用源點匯點代表兩個芯片
對某個任務(wù) , 在 A 芯片上運行時間 t1 . 則從源點連一條 到 這個任務(wù)容量為 t1 的邊 . 對 B 芯片上運行時間同理
如果兩個任務(wù)間有聯(lián)系,著再倆個任務(wù)間連邊.
原問題就轉(zhuǎn)化成了, 將圖分成兩部分,所切割的容量最少.
最小割==最大流 isap 解決
Dual Core CPU
Time Limit: 15000MS |
|
Memory Limit: 131072K |
Total Submissions: 19127 |
|
Accepted: 8263 |
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
POJ Monthly--2007.11.25, Zhou Dong
|
[Submit] [Go Back]
[Status] [Discuss]
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
const int maxn=20200;
const int maxm=600300;
const int INF=0x3f3f3f3f;
struct Edge
{
int to,next,cap,flow;
}edge[maxm];
int Size,Adj[maxn];
int gap[maxn],dep[maxn],pre[maxn],cur[maxn];
void init()
{
Size=0;
memset(Adj,-1,sizeof(Adj));
}
void addedge(int u,int v,int w,int rw=0)
{
edge[Size].to=v; edge[Size].cap=w; edge[Size].next=Adj[u];
edge[Size].flow=0; Adj[u]=Size++;
edge[Size].to=u; edge[Size].cap=rw; edge[Size].next=Adj[v];
edge[Size].flow=0; Adj[v]=Size++;
}
int sap(int start,int end,int N)
{
memset(gap,0,sizeof(gap));
memset(dep,0,sizeof(dep));
memcpy(cur,Adj,sizeof(Adj));
int u=start;
pre[u]=-1;gap[0]=N;
int ans=0;
while(dep[start]<N)
{
if(u==end)
{
int Min=INF;
for(int i=pre[u];~i;i=pre[edge[i^1].to])
if(Min>edge[i].cap-edge[i].flow)
Min=edge[i].cap-edge[i].flow;
for(int i=pre[u];~i;i=pre[edge[i^1].to])
{
edge[i].flow+=Min;
edge[i^1].flow-=Min;
}
u=start;
ans+=Min;
continue;
}
bool flag=false;
int v;
for(int i=cur[u];~i;i=edge[i].next)
{
v=edge[i].to;
if(edge[i].cap-edge[i].flow&&dep[v]+1==dep[u])
{
flag=true;
cur[u]=pre[v]=i;
break;
}
}
if(flag)
{
u=v;
continue;
}
int Min=N;
for(int i=Adj[u];~i;i=edge[i].next)
{
if(edge[i].cap-edge[i].flow&&dep[edge[i].to]<Min)
{
Min=dep[edge[i].to];
cur[u]=i;
}
}
gap[dep[u]]--;
if(!gap[dep[u]]) return ans;
dep[u]=Min+1;
gap[dep[u]]++;
if(u!=start) u=edge[pre[u]^1].to;
}
return ans;
}
int n,m;
int main()
{
while(scanf("%d%d",&n,&m)!=EOF)
{
init();
for(int i=1;i<=n;i++)
{
int a,b;
scanf("%d%d",&a,&b);
addedge(0,i,a);
addedge(i,n+1,b);
}
for(int i=0;i<m;i++)
{
int u,v,w;
scanf("%d%d%d",&u,&v,&w);
addedge(u,v,w,w);
}
printf("%d
",sap(0,n+1,n+2));
}
return 0;
}
生活不易,碼農(nóng)辛苦
如果您覺得本網(wǎng)站對您的學(xué)習(xí)有所幫助,可以手機掃描二維碼進行捐贈