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. 【专家坐堂Q&A】在 petalinux-config 中选择外部来源时,可将符号链路添加内核来源目录树

    问题描述 作为 petalinux-config 菜单的一部分,现在可以将 Linux 内核指定为外部来源. 如果选择了该选项,可为内核来源目录树添加两个符号链路. 这会带来两个问题: 1. 符号链路 ...

  2. bzoj 4318 OSU! - 动态规划 - 概率与期望

    Description osu 是一款群众喜闻乐见的休闲软件.  我们可以把osu的规则简化与改编成以下的样子:  一共有n次操作,每次操作只有成功与失败之分,成功对应1,失败对应0,n次操作对应为1 ...

  3. chrome浏览器中的百度搜索引擎总是先自动跳转到百度首页怎么办?

    1.修改百度搜索引擎代码如下: http://www.baidu.com/s?wd=%s&ie={inputEncoding} 备注:现在chorme不能直接在默认的百度搜索引擎上修改,而且自 ...

  4. 【做题】neerc2017的A、C、I、L

    A - Archery Tournament 一开始往化简公式的方向去想,结果没什么用. 考虑与一条垂线相交的圆的个数.不难YY,当圆的个数最多时,大概就是这个样子的: 我们稍微推一下式子,然后就能发 ...

  5. sql -- 移除数据中的换行符和回车符

    https://blog.csdn.net/jcx5083761/article/details/40185795 --移除回车符 update master_location SET street_ ...

  6. js变量按照存储方式区分,有哪些类型,并表述其特点

    // 值类型 拷贝形式 不像引用类型是指针指向,共用空间 值类型有 undefined string number Boolean var a = 100; var b = a; var a = 20 ...

  7. Docker 开发最佳实践

    Docker development best practices The following development patterns have proven to be helpful for p ...

  8. OAuth的MVC实现(微软)

    LoginController中: 第三方登陆 public ActionResult LogOn() { string liveUrl = string.Format( "https:// ...

  9. Android 使alertDialog.builder不会点击外面和按返回键消失

    这个问题之前一直困扰我,我的需求就是点击对话框外面和按返回键对话框不会消失,按返回键还好解决,拦截下返回键就OK了. 但是点击外面不好解决.之前有人说模态对话框,我看了一会,觉得不是我想要的效果.po ...

  10. commons-beanutils使用介绍

    commons-beanutils是Apache开源组织提供的用于操作JAVA BEAN的工具包.使用commons-beanutils,我们可以很方便的对bean对象的属性进行操作.今天为大家介绍一 ...