codeforces 1082G - Petya and Graph 最大权闭合子图 网络流
2024-08-31 13:41:35
题意:
让你选一些边,选边的前提是端点都被选了,求所有的边集中,边权和-点权和最大的一个。
题解:
对于每个边建一个点,然后就是裸的最大权闭合子图,
结果比赛的时候我的板子太丑,一直T,(不会当前弧优化...)
当时补题用的是蔡队的Dinic当前弧优化板子
今天重写了一遍
#include <bits/stdc++.h>
#define endl '\n'
#define ll long long
#define all(x) x.begin(),x.end()
#define IO ios::sync_with_stdio(false);cin.tie(0);cout.tie(0)
#define rep(ii,a,b) for(int ii=a;ii<=b;++ii)
using namespace std;
const int mod=1e9+7;
const double PI=acos(-1.0);
const int maxn=1e6+7,maxm=2e6+7;
//head
ll n,m,s,t;
class graph{
public:
struct edge{
int from,to;ll cap,flow;
edge(int a,int b,ll c,ll d){from=a,to=b,cap=c,flow=d;}
};
vector<vector<edge>> node;
graph(int n=maxn){node.resize(n+2);}
void add(int a,int b,ll c,ll d){
node[a].emplace_back(a,b,c,d);
}
}; const ll INF = 1e18;
class dinic:public graph{
public:
int n,m,s,t;
vector<edge> edges;
vector<vector<int>> v;
vector<bool> vis;
vector<int> dis,cur;
dinic(int nn=maxn){
node.resize(nn+2);
v.resize(nn+2);
n=nn;
m=0;
}
void add(int a, int b, ll c){
edges.emplace_back(a,b,c,0);
edges.emplace_back(b,a,0,0);
v[a].emplace_back(m++);
v[b].emplace_back(m++);
//node[a].emplace_back(a,b,c,0);
//node[b].emplace_(b,a,0,0);
}
bool bfs(){
fill(all(vis),false);
fill(all(dis),0);
queue<int> q;
q.push(s);
dis[s]=0;
vis[s]=1;
while(!q.empty()) {
int now=q.front();q.pop();
for(auto &i:v[now]) {
edge &e=edges[i];
if(!vis[e.to]&&e.cap>e.flow){
vis[e.to]=1;
dis[e.to]=dis[now]+1;
q.push(e.to);
}
}
}
return vis[t];
}
ll dfs(int now, ll a){
if (now==t||a==0) return a;
ll flow=0,f;
int mm=v[now].size();
for(int &i=cur[now];i<mm;i++){
edge &e=edges[v[now][i]];
if(dis[now]+1==dis[e.to]&&(f= dfs(e.to,min(a,e.cap-e.flow)))>0){
e.flow+=f;
edges[v[now][i]^1].flow-=f;
flow+=f;
a-=f;
if(a==0) break;
}
}
return flow;
}
ll mf(int ss,int tt){
s=ss,t=tt;
dis.resize(n+2);
cur.resize(n+2);
vis.resize(n+2);
ll flow=0;
while(bfs()){
fill(all(cur),0);
flow+=dfs(s,INF);
}
return flow;
}
}; int main() {
IO;
cin>>n>>m;
int tt=n+m+1,ss=0;
dinic g(n+m+2);
rep(i,1,n) {
ll a;cin>>a;
if(a>0) g.add(i,tt,a);
}
ll sum=0;
rep(i,1,m) {
int a,b,c;cin>>a>>b>>c;
sum+=c;
g.add(i+n,a,INF);
g.add(i+n,b,INF);
g.add(ss,i+n,c);
}
cout<<sum-g.mf(ss,tt)<<endl;
return 0;
}
最新文章
- 计算机程序的思维逻辑 (49) - 剖析LinkedHashMap
- 烂泥:python2.7和python3.5源码安装
- 对,这是http处理层
- Android开发--仿微信语音对讲录音
- HDU 2096 小明A+B --- 水题
- Erlang库 -- 有意思的库汇总
- DP(斜率优化):HDU 3507 Print Article
- Android签名详解(debug和release)
- c#错误捕获如何定位到某一行?
- Css3新特性应用之过渡与动画
- 安装JDK,配置环境变量有感
- 两百条微信小程序跳坑指南(不定时更新)
- TOMCAT闪退。cmd执行startup.bat保错:the CATALINA_HOME environment variable is not defined correctly
- 项目Alpha冲刺Day3
- Spring+Spring MVC+Mybatis 框架整合开发(半注解半配置文件)
- Beamer制作索引
- Python快速学习03:运算 &; 缩进和选择
- 【二】Spring Cloud 入门
- eclipse html 打开方式
- EDK II之USB设备驱动程序的加载与运行