[xsy1232]Magic
题意:一个无向图,每个点有$a_i,b_i$,对任意点$i$你都可以花费$b_i$的费用将$a_i$变为$0$,最后你还要付出$\sum\limits_{i=1}^n\max\limits_{(i,j)}a_j$的费用,求最小费用
把每个点拆成两个点,对每条边新建一个点,将一个点的出边按终点的$a$从大到小排序后建图如下

只有让某个前缀$a_i$全部变为$0$,一个点产生的费用才会改变,这个建图保证了如果要割$a_i$就必须要割掉$b_{1\cdots i-1}$,并且不会割掉两个$a$
听myh说是套路但我不会...
#include<stdio.h>
#include<algorithm>
#include<vector>
#include<string.h>
using namespace std;
const int inf=2147483647;
int h[52010],nex[204010],to[204010],cap[204010],M=1,S,T;
void ins(int a,int b,int c){
M++;
to[M]=b;
cap[M]=c;
nex[M]=h[a];
h[a]=M;
}
void add(int a,int b,int c){
ins(a,b,c);
ins(b,a,0);
}
int dis[52010],q[52010];
bool bfs(){
int head,tail,x,i;
memset(dis,-1,sizeof(dis));
head=tail=1;
q[1]=S;
dis[S]=0;
while(head<=tail){
x=q[head++];
for(i=h[x];i;i=nex[i]){
if(cap[i]&&dis[to[i]]==-1){
dis[to[i]]=dis[x]+1;
if(to[i]==T)return 1;
q[++tail]=to[i];
}
}
}
return 0;
}
int cur[52010];
int dfs(int x,int flow){
if(x==T)return flow;
int us=0,i,t;
for(i=cur[x];i&&flow;i=nex[i]){
if(cap[i]&&dis[to[i]]==dis[x]+1){
t=dfs(to[i],min(flow,cap[i]));
cap[i]-=t;
cap[i^1]+=t;
us+=t;
flow-=t;
if(cap[i])cur[x]=i;
}
}
if(us==0)dis[x]=-1;
return us;
}
int dicnic(){
int ans=0;
while(bfs()){
memcpy(cur,h,sizeof(h));
ans+=dfs(S,inf);
}
return ans;
}
vector<int>g[1010];
int a[1010],b[1010];
bool cmp(int x,int y){return a[x]>a[y];}
int main(){
int n,m,i,x,y,N;
scanf("%d%d",&n,&m);
for(i=1;i<=n;i++)scanf("%d",a+i);
for(i=1;i<=n;i++)scanf("%d",b+i);
for(i=1;i<=m;i++){
scanf("%d%d",&x,&y);
g[x].push_back(y);
}
N=2*n;
S=N+m+1;
T=S+1;
for(i=1;i<=n;i++)add(S,i,inf);
for(x=1;x<=n;x++){
sort(g[x].begin(),g[x].end(),cmp);
for(i=0;i<(int)g[x].size();i++){
N++;
add(i?N-1:x,N,a[g[x][i]]);
add(N,g[x][i]+n,inf);
}
}
for(i=1;i<=n;i++)add(i+n,T,b[i]);
printf("%d",dicnic());
}
[xsy1232]Magic的更多相关文章
- 【xsy1232】Magic 最小割
题目大意:给你一个$n$个点,$m$条有向边的图,每个点有一个点权$a_i$,同时你可以用$b_i$的代价将$a_i$变为$0$ 另外你要付出$\sum\limits_{i=1}^n\max\limi ...
- Codeforces CF#628 Education 8 D. Magic Numbers
D. Magic Numbers time limit per test 2 seconds memory limit per test 256 megabytes input standard in ...
- [8.3] Magic Index
A magic index in an array A[0...n-1] is defined to be an index such that A[i] = i. Given a sorted ar ...
- Python魔术方法-Magic Method
介绍 在Python中,所有以"__"双下划线包起来的方法,都统称为"Magic Method",例如类的初始化方法 __init__ ,Python中所有的魔 ...
- 【Codeforces717F】Heroes of Making Magic III 线段树 + 找规律
F. Heroes of Making Magic III time limit per test:3 seconds memory limit per test:256 megabytes inpu ...
- 2016中国大学生程序设计竞赛 - 网络选拔赛 C. Magic boy Bi Luo with his excited tree
Magic boy Bi Luo with his excited tree Problem Description Bi Luo is a magic boy, he also has a migi ...
- 一个快速double转int的方法(利用magic number)
代码: int i = *reinterpret_cast<int*>(&(d += 6755399441055744.0)); 知识点: 1.reinterpret_cast&l ...
- MAGIC XPA最新版本Magic xpa 2.4c Release Notes
New Features, Feature Enhancements and Behavior ChangesSubforms – Behavior Change for Unsupported Ta ...
- Magic xpa 2.5发布 Magic xpa 2.5 Release Notes
Magic xpa 2.5發佈 Magic xpa 2.5 Release Notes Magic xpa 2.5 Release NotesNew Features, Feature Enhance ...
随机推荐
- 复现VGG19训练自定义图像分类
1.复现VGG训练自定义图像分类,成功了哈哈. 需要代码工程可联系博主qq号,在左边连接可找到. 核心代码: # coding:utf-8 import tensorflow as tf import ...
- EditText中inputType详解
<EditText Android:layout_width="fill_parent" android:layout_height="wrap_content&q ...
- Vue组件-使用插槽分发内容
在使用组件时,我们常常要像这样组合它们: <app> <app-header></app-header> <app-footer></app-fo ...
- Vue组件-组件的属性
在html中使用元素,会有一些属性,如class,id,还可以绑定事件,自定义组件也是可以的.当在一个组件中,使用了其他自定义组件时,就会利用子组件的属性和事件来和父组件进行数据交流. 比如,子组件需 ...
- Revison
- [Leetcode Week14]Path Sum II
Path Sum II 题解 原创文章,拒绝转载 题目来源:https://leetcode.com/problems/path-sum-ii/description/ Description Giv ...
- Yii 1.1.17 三、数据库连接、定义模型、数据查询、验证登录、SESSION使用与URL生成
一.数据库连接 1.配置连接参数 在database.php里面开启: 'db' => array( 'connectionString' => 'mysql:host=127.0.0.1 ...
- 2017多校第8场 HDU 6138 Fleet of the Eternal Throne AC自动机或者KMP
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6138 题意:给n个串,每次询问x号串和y号串的最长公共子串的长度,这个子串必须是n个串中某个串的前缀 ...
- 2015多校第9场 HDU 5405 Sometimes Naive 树链剖分
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5405 题意: 给你一棵n个节点的树,有点权. 要求支持两种操作: 操作1:更改某个节点的 ...
- vue 的过滤器
1.0版本: limitBy filteBy orderBy lowerBy upperBy json currency capitalize pluralize debounce 2.0版本: 要自 ...