G - Petya and Graph

思路:

最大权闭合子图

对于每条边,如果它选了,那么它连的的两个点也要选

边权为正,点权为负,那么就是求最大权闭合子图

代码:

#pragma GCC optimize(2)
#pragma GCC optimize(3)
#pragma GCC optimize(4)
#include<bits/stdc++.h>
using namespace std;
#define fi first
#define se second
#define pi acos(-1.0)
#define LL long long
//#define mp make_pair
#define pb push_back
#define ls rt<<1, l, m
#define rs rt<<1|1, m+1, r
#define ULL unsigned LL
#define pll pair<LL, LL>
#define pli pair<LL, int>
#define pii pair<int, int>
#define piii pair<pii, int>
#define mem(a, b) memset(a, b, sizeof(a))
#define fio ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define fopen freopen("in.txt", "r", stdin);freopen("out.txt", "w", stout);
//head const LL INF = 1LL<<;
const int N = 2e3 + ;
int level[N], iter[N];
struct edge {
int to;
LL w;
int rev;
};
vector<edge>g[N];
void add_edge(int u, int v, LL w) {
g[u].pb(edge{v, w, g[v].size()});
g[v].pb(edge{u, , g[u].size()-});
}
void bfs(int s) {
mem(level, -);
queue<int>q;
level[s] = ;
q.push(s);
while (!q.empty()) {
int u = q.front();
q.pop();
for (int i = ; i < g[u].size(); i++) {
edge e = g[u][i];
if(e.w > && level[e.to] < ) {
level[e.to] = level[u] + ;
q.push(e.to);
}
}
}
}
LL dfs(int u, int t, LL f) {
if(u == t ) return f;
for (int &i = iter[u]; i < g[u].size(); i++) {
edge &e = g[u][i];
if(e.w > && level[u] < level[e.to]) {
LL d = dfs(e.to, t, min(f, e.w));
if(d > ) {
e.w -= d;
g[e.to][e.rev].w +=d;
return d;
}
}
}
return ;
}
LL max_flow(int s, int t) {
LL flow = ;
while(true) {
bfs(s);
if(level[t] < ) return flow;
LL f;
mem(iter, );
while ((f = dfs(s, t, INF)) > ) {
flow += f;
}
}
}
int main() {
int n, m, w, u, v;
scanf("%d %d", &n, &m);
int s = , t = n+m+;
for (int i = ; i <= n; i++) {
scanf("%d", &w);
add_edge(i, t, w);
}
LL sum = ;
for (int i = ; i <= m; i++) {
scanf("%d %d %d", &u, &v, &w);
sum += w;
add_edge(i+n, u, INF);
add_edge(i+n, v, INF);
add_edge(s, i+n, w);
}
printf("%lld\n", sum - max_flow(s, t));
return ;
}

Codeforces 1082 G - Petya and Graph的更多相关文章

  1. CodeForces 1082 G Petya and Graph 最大权闭合子图。

    题目传送门 题意:现在有一个图,选择一条边,会把边的2个顶点也选起来,最后会的到一个边的集合 和一个点的集合 , 求边的集合 - 点的集合最大是多少. 题解:裸的最大权闭合子图. 代码: #inclu ...

  2. G. Petya and Graph(经典项目与项目消耗问题)(网络流)

    题:https://codeforces.com/contest/1082/problem/G 题意:给定有边权和点权的图,问你选一些边,然sum边-sum点最大(点权被多次用为公共点只会减一次) 分 ...

  3. CF1082G:G. Petya and Graph(裸的最大闭合权图)

    Petya has a simple graph (that is, a graph without loops or multiple edges) consisting of n n vertic ...

  4. CodeForces 1082 D Maximum Diameter Graph

    题目传送门 题意:现在有n个点,每个点的度数最大为di,现在要求你构成一棵树,求直径最长. 题解:把所有度数为2的点先扣出来,这些就是这颗树的主干,也就是最长的距离. 然后我们把度数为2的点连起来,之 ...

  5. Petya and Graph/最大权闭合子图、最小割

    原题地址:https://codeforces.com/contest/1082/problem/G G. Petya and Graph time limit per test 2 seconds ...

  6. Petya and Graph(最小割,最大权闭合子图)

    Petya and Graph http://codeforces.com/contest/1082/problem/G time limit per test 2 seconds memory li ...

  7. [codeforces 549]G. Happy Line

    [codeforces 549]G. Happy Line 试题描述 Do you like summer? Residents of Berland do. They especially love ...

  8. CodeForces 794 G.Replace All

    CodeForces 794 G.Replace All 解题思路 首先如果字符串 \(A, B\) 没有匹配,那么二元组 \((S, T)\) 合法的一个必要条件是存在正整数对 \((x,y)\), ...

  9. 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 ...

随机推荐

  1. hihocoder [Offer收割]编程练习赛14

    A.小Hi和小Ho的礼物 谜之第1题,明明是第1题AC率比C还要低.题目是求在n个不同重量袋子选4袋,2袋给A,2袋给B,使2人获得重量相同,求问方案数. 我也是一脸懵b...o(n2)暴力枚举发现把 ...

  2. django ORM常用查询条件

    假设有一个模型 class Article(models.Model): title=models.CharField(max_length=50) content=models.TextField( ...

  3. Java线程安全容器

    一.Java同步容器 同步容器是用来解决并发情况下的容器线程安全问题的.给多线程环境准备一个线程安全的容器对象. 线程安全的容器对象: Vector, Hashtable.线程安全容器对象,都是使用s ...

  4. Builgen 插件——IntelliJ IDEA和Eclipse Java Bean Builder模式代码生成器-比lombok更符合需求

    builder模式在越来越多的项目中使用,类似于alibaba fastjson JSONObject.fluentPut(),调用一个方法后返回这个对象本身,特别适合构建一些参数超级多的对象,代码优 ...

  5. VIM自动补全Python代码

    pydiction插件 https://codeload.github.com/rkulla/pydiction/zip/master 新建bundle文件夹 mkdir ~/.vim/bundle ...

  6. Android之Sqlite数据库

    数据库访问完毕后,游标必须也记得关闭 import com.huangzhong.love_power_model.UserInfoDto; import java.util.ArrayList; i ...

  7. Bugku-CTF之web基础$_GET

    Day3   web基础$_GET   http://123.206.87.240:8002/get/   打开之后是一段代码  

  8. CSV是什么文件格式【转】

    本文转载自:https://blog.csdn.net/huyanping/article/details/6384687 CSV即Comma Separate Values,这种文件格式经常用来作为 ...

  9. SCU 4439 Vertex Cover(二分图最小覆盖点)题解

    题意:每一条边至少有一个端点要涂颜色,问最少涂几个点 思路:最小顶点覆盖:用最少的点,让每条边都至少和其中一个点关联,显然是道裸最小顶点覆盖题: 参考:二分图 代码: #include<iost ...

  10. 卸载linux系统上自带的mysql

    步骤: 1.打开centos命令提示符,切换为root用户 2.输入rpm -qa|grep -i mysql命令以检查系统含有的mysql插件,回车,若没有则说明无自带mysql,系统很干净.若有显 ...