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. Nodejs的npm安装模块时候报错:npm ERR! Error: CERT_UNTRUSTED的解决方法

    npm http GET https://registry.npmjs.org/grunt-cli npm http GET https://registry.npmjs.org/grunt-cli ...

  2. PyCharm配置MySQL

    有时候电脑没有安装Navicat,也想要操作数据库,那么PyCharm也能实现Navicat的一些功能.下面演示,用PyCharm如何连接数据库并执行操作: 1. 打开PyCharm,在右侧边栏打开D ...

  3. vim 快捷键(update)

    网上盗的图,233   2018年08月31日14:31:19 update: 批量行操作 esc->ctrl+v->选中批量行->shift+i->operation-> ...

  4. ZVAL——PHP源码分析

    基于 PHP 5.6.20 ZVAL——php变量实现的基础 _zval_struct 结构体的定义位于 Zend/zend.h 322 行 typedef union _zvalue_value { ...

  5. Stanford CS231n实践笔记(课时22卷积神经网络工程实践技巧与注意点 cnn in practise 上)

    本课主要2个实践内容: 1.keras中数据集丰富,从数据集中提取更多特征(Data augmentation) 2.迁移学习(Tranform learning) 代码:https://github ...

  6. hdu 4366 Successor - CDQ分治 - 线段树 - 树分块

    Sean owns a company and he is the BOSS.The other Staff has one Superior.every staff has a loyalty an ...

  7. Codeforces Round #424 (Div. 2, rated, based on VK Cup Finals) Problem C (Codeforces 831C) - 暴力 - 二分法

    Polycarp watched TV-show where k jury members one by one rated a participant by adding him a certain ...

  8. ubuntu 容器安装ping ifconfig ip命令

    进入容器测试ifconfig  ping 没有-------->>很尴尬 apt-get install net-tools ###   ifconfig apt-get install ...

  9. vector at()函数比 []运算符操作安全

    转载:https://blog.csdn.net/chenjiayi_yun/article/details/18507659 []操作符的源码 reference operator[](size_t ...

  10. php的__autoload和php的__call

    首先, __call是php语言自身就具有的一种语言features...,不是thinkphp的语言特征. 关于__call参考: http://love-love-l.blog.163.com/b ...