CF1082G:G. Petya and Graph(裸的最大闭合权图)
Petya has a simple graph (that is, a graph without loops or multiple edges) consisting of n n vertices and m m edges.
The weight of the i i -th vertex is a i ai .
The weight of the i i -th edge is w i wi .
A subgraph of a graph is some set of the graph vertices and some set of the graph edges. The set of edges must meet the condition: both ends of each edge from the set must belong to the chosen set of vertices.
The weight of a subgraph is the sum of the weights of its edges, minus the sum of the weights of its vertices. You need to find the maximum weight of subgraph of given graph. The given graph does not contain loops and multiple edges.
Input
The first line contains two numbers n n and m m (1≤n≤10 3 ,0≤m≤10 3 1≤n≤103,0≤m≤103 ) - the number of vertices and edges in the graph, respectively.
The next line contains n n integers a 1 ,a 2 ,…,a n a1,a2,…,an (1≤a i ≤10 9 1≤ai≤109 ) - the weights of the vertices of the graph.
The following m m lines contain edges: the i i -e edge is defined by a triple of integers v i ,u i ,w i vi,ui,wi (1≤v i ,u i ≤n,1≤w i ≤10 9 ,v i ≠u i 1≤vi,ui≤n,1≤wi≤109,vi≠ui ). This triple means that between the vertices v i vi and u i ui there is an edge of weight w i wi . It is guaranteed that the graph does not contain loops and multiple edges.
Output
Print one integer — the maximum weight of the subgraph of the given graph.
Examples
4 5
1 5 2 2
1 3 4
1 4 4
3 4 5
3 2 2
4 2 2
8
3 3
9 7 8
1 2 1
2 3 2
1 3 3
0
题意:让你选一些边,选边的前提是端点都被选了,求最大的边权和-点权和。
思路:这不是BZOJ原题嘛.jpg。比如BZOJ3438:小M的作物,BZOJ3894:文理分科。
就是先得到所有的贡献sum,然后减去最小割。因为求最小割的过程,其实是一个取min的过程。
(福利题啊!CF损失50分的感觉
#include<bits/stdc++.h>
#define ll long long
using namespace std;
const int maxn=;
const ll inf=1e18;
int N,M,S,T,cnt=; ll ans,maxflow,cap[maxn];
int Laxt[maxn],To[maxn],Next[maxn],vd[maxn],dis[maxn];
void add(int u,int v,ll c)
{
Next[++cnt]=Laxt[u];Laxt[u]=cnt; To[cnt]=v; cap[cnt]=c;
Next[++cnt]=Laxt[v];Laxt[v]=cnt; To[cnt]=u; cap[cnt]=;
}
ll sap(int u,ll flow)
{
if(flow==||u==T) return flow;
ll tmp,delta=;
for(int i=Laxt[u];i;i=Next[i]){
if(dis[u]==dis[To[i]]+&&cap[i]>){
tmp=sap(To[i],min(cap[i],flow-delta));
delta+=tmp; cap[i]-=tmp; cap[i^]+=tmp;
if(delta==flow||dis[S]>T+) return delta;
}
}
vd[dis[u]]--;
if(vd[dis[u]]==) dis[S]=T+;
vd[++dis[u]]++;
return delta;
}
int main()
{
int u,v,x; ll c; scanf("%d%d",&N,&M);
S=; T=N+M+;
for(int i=;i<=N;i++){
scanf("%d",&x);
add(i,T,x);
}
for(int i=;i<=M;i++){
scanf("%d%d%lld",&u,&v,&c); ans+=c;
add(N+i,u,inf); add(N+i,v,inf); add(S,N+i,c);
}
while(dis[S]<=T) maxflow+=sap(S,inf);
printf("%lld\n",ans-maxflow);
return ;
}
CF1082G:G. Petya and Graph(裸的最大闭合权图)的更多相关文章
- Codeforces 1082 G - Petya and Graph
G - Petya and Graph 思路: 最大权闭合子图 对于每条边,如果它选了,那么它连的的两个点也要选 边权为正,点权为负,那么就是求最大权闭合子图 代码: #pragma GCC opti ...
- CodeForces 1082 G Petya and Graph 最大权闭合子图。
题目传送门 题意:现在有一个图,选择一条边,会把边的2个顶点也选起来,最后会的到一个边的集合 和一个点的集合 , 求边的集合 - 点的集合最大是多少. 题解:裸的最大权闭合子图. 代码: #inclu ...
- G. Petya and Graph(经典项目与项目消耗问题)(网络流)
题:https://codeforces.com/contest/1082/problem/G 题意:给定有边权和点权的图,问你选一些边,然sum边-sum点最大(点权被多次用为公共点只会减一次) 分 ...
- Petya and Graph/最大权闭合子图、最小割
原题地址:https://codeforces.com/contest/1082/problem/G G. Petya and Graph time limit per test 2 seconds ...
- Petya and Graph(最小割,最大权闭合子图)
Petya and Graph http://codeforces.com/contest/1082/problem/G time limit per test 2 seconds memory li ...
- NEERC 2016-2017 Probelm G. Game on Graph
title: NEERC 2016-2017 Probelm G. Game on Graph data: 2018-3-3 22:25:40 tags: 博弈论 with draw 拓扑排序 cat ...
- [LeetCode]Copy List with Random Pointer &Clone Graph 复杂链表的复制&图的复制
/** * Definition for singly-linked list with a random pointer. * struct RandomListNode { * int label ...
- Nebula Graph 技术总监陈恒:图数据库怎么和深度学习框架进行结合?
引子 Nebula Graph 的技术总监在 09.24 - 09.30 期间同开源中国·高手问答的小伙伴们以「图数据库的设计和实践」为切入点展开讨论,包括:「图数据库的存储设计」.「图数据库的计算设 ...
- CF1082G Petya and Graph(最小割,最大权闭合子图)
QWQ嘤嘤嘤 感觉是最水的一道\(G\)题了 顺便记录一下第一次在考场上做出来G qwqqq 题目大意就是说: 给你n个点,m条边,让你选出来一些边,最大化边权减点权 \(n\le 1000\) QW ...
随机推荐
- h5前端项目常见问题汇总
原文作者:FrontEndZQ 原文链接:https://github.com/FrontEndZQ/HTML5-FAQ H5项目常见问题及注意事项 Meta基础知识: H5页面窗口自动调整到设备宽度 ...
- mac常用操作:
Mac常用软件需要熟悉 常用操作: command + w 关闭窗口 + n 最小化当前窗口 + m 关闭所有窗口 + + w command + c 复制 command + v 粘贴 co ...
- 图:无向图(Graph)基本方法及Dijkstra算法的实现 [Python]
一般来讲,实现图的过程中需要有两个自定义的类进行支撑:顶点(Vertex)类,和图(Graph)类.按照这一架构,Vertex类至少需要包含名称(或者某个代号.数据)和邻接顶点两个参数,前者作为顶点的 ...
- java Pattern(正则)类
Pattern的静态方法matches 用于快速匹配字符串,该方法适合用于只匹配一次,且匹配全部字符串. Boolean b=Pattern.matches("^((13[0-9])|(15 ...
- [one day one question] express 不缓存如何实现
问题描述: express 默认缓存,这怎么破? 解决方案: apiRoutes.use(function (req, res, next) { res.setHeader('Cache-Contro ...
- Refactoring #001 Extract Method
Example public void startup() { ServerSocket serverSocket = null; try { serverSocket = new ServerSoc ...
- Xcode7.2与iOS9之坑 (持续更新)
GitHub地址 前几天升级OS X EI Capitan 10.11.1, 以及Xcode7.1,正好赶上公司新产品上线,要做iOS9的适配,遇到各种坑,各种查资料,随之记录总结一下遇到的坑. 先说 ...
- npm 安装私有 git 包
npm 安装私有 git 包 npm 对于前端开发来说是一种必备的工具,对于开源项目来说,完全没有任何问题,安装包的依赖直接依赖于 Npm 即可. 对于公司内网的一些项目就不是太方便了,因为我们通常会 ...
- 不能访问虚拟机上的nginx网站
VMware虚拟机上配置nginx后,本机无法访问问题 转自:http://www.server110.com/nginx/201407/10794.html 把nginx装在CentOS上,用本机访 ...
- 前端jsp页面script引入url项目名使用${appName}
<script src="/${appName}/commons/jslib/CommonValue.js"></script> 新建一个com.autum ...