首页 技术 正文
技术 2022年11月6日
0 收藏 316 点赞 1,103 浏览 2020 个字

划分成两个集合使费用最小,可以转成最小割,既最大流。

//#pragma comment(linker, "/STACK:1024000000,1024000000")
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<algorithm>
#include<iostream>
#include<sstream>
#include<cmath>
#include<climits>
#include<string>
#include<map>
#include<queue>
#include<vector>
#include<stack>
#include<set>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> pii;
#define pb(a) push(a)
#define INF 0x1f1f1f1f
#define lson idx<<1,l,mid
#define rson idx<<1|1,mid+1,r
#define PI 3.1415926535898
template<class T> T min(const T& a,const T& b,const T& c)
{
return min(min(a,b),min(a,c));
}
template<class T> T max(const T& a,const T& b,const T& c)
{
return max(max(a,b),max(a,c));
}
void debug()
{
#ifdef ONLINE_JUDGE
#else
freopen("data.in","r",stdin);
// freopen("d:\\out1.txt","w",stdout);
#endif
}
int getch()
{
int ch;
while((ch=getchar())!=EOF)
{
if(ch!=' '&&ch!='\n')return ch;
}
return EOF;
}struct Edge
{
int to,cap;
};
const int maxn = ;
vector<int> g[maxn];
vector<Edge> edge;
int n,m,s,t;
int d[maxn];
int vis[maxn];
int cur[maxn];void add(int u,int v,int cap)
{
//printf("%d -> %d : %d\n", u, v, cap);
edge.push_back((Edge){v, cap});
g[u].push_back(edge.size() - );
edge.push_back((Edge){u, });
g[v].push_back(edge.size() - );
}
void build()
{
for(int i = ; i <= n; i++)
g[i].clear();
edge.clear();
s = n + ;
t = s + ;
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,w;
scanf("%d%d%d", &a, &b, &w);
add(a, b, w);
add(b, a, w);
}
}bool bfs()
{
memset(vis, , sizeof(vis));
d[s] = ;
queue<int> q;
q.push(s);
vis[s] = true;
while(!q.empty())
{
int x = q.front(); q.pop();
for (int i = ; i < g[x].size(); i++)
{
Edge &e = edge[g[x][i]];
if(e.cap> && !vis[e.to])
{
vis[e.to] = true;
q.push(e.to);
d[e.to] = d[x] + ;
}
}
}
return vis[t];
}int dfs(int u,int f)
{
if(u==t || f==) return f;
for(int &i = cur[u]; i < g[u].size(); i++)
{
Edge &e = edge[g[u][i]];
if(e.cap> && d[e.to] == d[u] + )
{
int d = dfs(e.to, min(f,e.cap));
if(d>)
{
e.cap -= d;
edge[g[u][i]^].cap += d;
return d;
}
}
}
return ;
}int max_flow()
{
int res = ;
while(bfs())
{
memset(cur, , sizeof(cur));
int d;
while(d=dfs(s,INF))
{
res += d;
}
}
return res;
}
int main()
{
debug();
while(scanf("%d%d", &n, &m) != EOF)
{
build();
printf("%d\n", max_flow());
}
return ;
}
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:8,983
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,500
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,344
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,127
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,761
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,838