[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 ...
随机推荐
- 小程序var that=this
小程序的js函数中,一般第一句就是var that=this,那么此语句的必要性是什么呢?下面用一段代码来解释这个问题 Page({ //页面的初始数据 loadUsers: function () ...
- .net WebAPI返回xml、json格式
WebAPI返回xml.json格式简单示例 using System.Net.Http.Formatting; public class TestController : ApiController ...
- Python作业模拟登陆(第一周)
模拟登陆:1. 用户输入帐号密码进行登陆2. 用户信息保存在文件内3. 用户密码输入错误三次后锁定用户 思路: 1. 用户名密码文件为passwd,锁定用户文件为lock 2. 用户输入账号密码采用i ...
- Tomcat8配置默认项目
<!-- 配置默认访问项目 --> <Host name="localhost" appBase="webapps" unpackWARs=& ...
- pdf文件添加到word中
今天遇到了一个问题,如何把pdf文件添加到word中,而不是只添加图标,下面是解决方案: 1.用word 打开pdf文件: 2.打开word文件: 3.把1中的pdf文件复制粘贴 到2中的word文件 ...
- vue-实现倒计时功能
JavaScript 创建一个 countdown 方法,用于计算并在控制台打印距目标时间的日.时.分.秒数,每隔一秒递归执行一次. msec 是当前时间距目标时间的毫秒数,由时间戳相减得到,我们将以 ...
- Laravel 项目上线的一些注意事项
1.应用生产环境 在 .env 文件里设置 APP_ENV=production 2.关闭调试模式 在 .env 文件中设置 APP_DEBUG = false 3.生成 APP_KEY 使用 Art ...
- python基础===输入必须为数字的检验的另一种方法
print("[+]welcome to python3") while True: num = input("please input a num:") if ...
- vsftp 服务的启动与问题
一般系统用户是可以直接登入的如果不可以可能是selinux的原因 执行一下: 更改selinux的配置文件将其设为disable,可我不想重启服务器,有以下解决办法:执行命令:setenforce 0 ...
- python的时间和日期--time、datetime应用
time >>> import time >>> time.localtime() #以time.struct_time类型,打印本地时间 time.struct_ ...