Dual Core CPU
Time Limit: 15000MS   Memory Limit: 131072K
Total Submissions: 25576   Accepted: 11033
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: abw. 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

题目大意:一个cpu有两个核,有n个任务,m个要求,第i个任务如果用第一个核处理需要花费ai,第二个需要花费bi,对于m个要求:如果任务i和j不在同一个核上运行,则要额外花费c,求最小花费.
分析:感觉又像是匹配问题,关系比较复杂,不能用二分图来做,于是就用网络流.
          如果考虑流的意义,图比较难建,考虑最小割的意义.删掉权值和最小的边使得源点汇点不连通.为了保证每个任务只会选一个核,将A核作为源点,B核作为汇点,源点向每个任务连一条容量为A花费的边,每个任务向汇点连一条容量为B花费的边,这样如果没有m个要求的话,最小割=答案便成立.如果有要求a,b,c.则在a,b之间互相连权值为c的边.这样如果a,b用的不是同一核,删掉两条边后会因为新加的这条边而继续相连.所以最小割=答案. 
          最大流问题建模考虑答案与最大流或最小割之间的关系.
#include <cstdio>
#include <queue>
#include <cstring>
#include <iostream>
#include <algorithm> using namespace std; const int maxn = ,inf = 0x7fffffff; int n,f,d,S,T,head[maxn],to[maxn],nextt[maxn],tot = ,w[maxn],ans,m;
int vis[maxn]; void add(int x,int y,int z)
{
w[tot] = z;
to[tot] = y;
nextt[tot] = head[x];
head[x] = tot++; w[tot] = ;
to[tot] = x;
nextt[tot] = head[y];
head[y] = tot++;
} bool bfs()
{
queue <int> q;
memset(vis,-,sizeof(vis));
vis[S] = ;
q.push(S);
while (!q.empty())
{
int u = q.front();
q.pop();
if (u == T)
return true;
for (int i = head[u];i;i = nextt[i])
{
int v = to[i];
if (vis[v] == - && w[i])
{
vis[v] = vis[u] + ;
q.push(v);
}
}
}
return false;
} int dfs(int u,int f)
{
int res = ;
if (u == T)
return f;
for (int i = head[u];i;i = nextt[i])
{
int v = to[i];
if (w[i] && vis[v] == vis[u] + )
{
int temp = dfs(v,min(f - res,w[i]));
res += temp;
w[i] -= temp;
w[i ^ ] += temp;
if (res == f)
return res;
}
}
if (!res)
vis[u] = -;
return res;
} void dinic()
{
while (bfs())
ans += dfs(S,inf);
} int main()
{
scanf("%d%d",&n,&m);
S = ;
T = n + ;
for (int i = ; i <= n; i++)
{
int a,b;
scanf("%d%d",&a,&b);
add(S,i,a);
add(i,T,b);
}
for (int i = ; i <= m; i++)
{
int a,b,c;
scanf("%d%d%d",&a,&b,&c);
add(a,b,c);
add(b,a,c);
}
dinic();
printf("%d\n",ans); return ;
}

最新文章

  1. ASP.NET MVC原理
  2. canvas实现类似弹窗广告效果
  3. 树形DP(01组合背包The Ghost Blows Light HDU4276)
  4. Ultra-QuickSort 分类: POJ 排序 2015-08-03 15:39 2人阅读 评论(0) 收藏
  5. sass学习(2)——关于变量
  6. JavaScript学习笔记(12)——JavaScript内置对象
  7. 【我们都爱Paul Hegarty】斯坦福IOS8公开课个人笔记2 Xcode、Auto Layout及MVC
  8. 14周事情总结-机器人-大数据hadoop
  9. 《sed的流艺术之一》-linux命令五分钟系列之二十一
  10. curl批处理从官方demo封装
  11. java中的基本数据类型的转换
  12. SpringBoot与Mybatis整合的设置
  13. FM(工程实现)
  14. HDU 1880 魔咒词典 (字符串hash)
  15. mongo中的游标与数据一致性的取舍
  16. Python Flask框架
  17. 日常遇错之ModuleNotFoundError: No module named request
  18. 安装mysql解压版时遇到的错误
  19. ORA-30377 MV_CAPABILITIES_TABLE not found
  20. Netty简单的HTTP服务器

热门文章

  1. CSP201604-2:俄罗斯方块
  2. thinkphp5框架生成二维码(二)
  3. Java实现在线预览功能
  4. Elasticsearch 统计代码例子
  5. JDK,JRE,JVM之间的关系
  6. 关于jsp之间href传参(中文)乱码问题
  7. C++Primer第五版——习题答案和解析
  8. HDU 5496 Beauty of Sequence
  9. django-registration中的问题
  10. Java Map获取key和value 以及String字符串转List方法