CodeForces 1187G Gang Up 费用流
题解:
先按时间轴将一个点拆成100个点。 第一个点相当于第一秒, 第二个点相当于第二秒。
在这些点之间连边, 每1流量的费用为c。
再将图上的边也拆开。
将 u_i 向 v_i+1 建边。
将 v_i 向 u_i+1 建边。
在上面的建边过程中:
假设最多一条路只会走20个人。
将这个东西拆成20条边。
第i条的流量为1, 费用为 c + ( i*i - (i-1)*(i-1)) * d
这样就建好图了。
然后再跑最小流就好了。
代码:
#include<bits/stdc++.h>
using namespace std;
#define Fopen freopen("_in.txt","r",stdin); freopen("_out.txt","w",stdout);
#define LL long long
#define ULL unsigned LL
#define fi first
#define se second
#define pb push_back
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define lch(x) tr[x].son[0]
#define rch(x) tr[x].son[1]
#define max3(a,b,c) max(a,max(b,c))
#define min3(a,b,c) min(a,min(b,c))
typedef pair<int,int> pll;
const int inf = 0x3f3f3f3f;
const int _inf = 0xc0c0c0c0;
const LL INF = 0x3f3f3f3f3f3f3f3f;
const LL _INF = 0xc0c0c0c0c0c0c0c0;
const LL mod = (int)1e9+;
const int Z = ;
const int N = Z * Z;
const int M = Z * Z * Z;
int px[N], py[N];
int head[N], to[M], ct[M], w[M], nt[M];
int d[N], vis[N];
int pre[N], id[N];
int s, t, tot; void add(int u, int v, int flow, int cost){
to[tot] = v; ct[tot] = cost;
w[tot] = flow; nt[tot] = head[u]; head[u] = tot++; to[tot] = u; ct[tot] = -cost;
w[tot] = ; nt[tot] = head[v]; head[v] = tot++;
}
void init(){
memset(head, -, sizeof(head));
tot = ;
}
int spfa(int s, int t){
queue<int> q;
memset(d, inf, sizeof(d));
memset(vis, , sizeof(vis));
memset(pre, -, sizeof(pre));
d[s] = ;
q.push(s);
while(!q.empty()){
int u = q.front(); q.pop();
vis[u] = ;
for(int i = head[u]; ~i; i = nt[i]){
if(w[i] > && d[to[i]] > d[u] + ct[i]){
d[to[i]] = d[u] + ct[i];
pre[to[i]] = u;
id[to[i]] = i;
if(!vis[to[i]]){
vis[to[i]] = ;
q.push(to[i]);
}
}
} }
return d[t] < inf;
}
int MinCostFlow(int s, int t){
int Mi = inf;
int sum = ;
int tt = ;
while(spfa(s, t)){
Mi = inf;
for(int i = t; i != s; i = pre[i])
Mi = min(Mi, w[id[i]]);
for(int i = t; i != s; i = pre[i]){
w[id[i]] -= Mi;
w[id[i]^] += Mi;
}
tt += Mi;
sum += d[t] * Mi;
}
return sum;
}
int main(){
init();
int n, m, k, c, d;
scanf("%d%d%d%d%d", &n, &m, &k, &c, &d);
int s = , t = n * + ;
for(int i = ; i <= ; ++i)
add(i, t, , );
for(int i = , v; i <= k; ++i){
scanf("%d" , &v);
add(s, (v-)*+, , );
}
for(int i = ; i <= n; ++i){
for(int j = ; j < ; ++j){
add((i-)* + j, (i-)* + j + , , c);
}
}
for(int i = , u, v; i <= m; ++i){
scanf("%d%d", &u, &v);
for(int j = ; j < ; ++j){
int idx = (u-) * + j;
int idy = (v-) * + j + ;
for(int k = ; k <= ; ++k){
add(idx, idy, , c + (k * k - (k-) * (k-))* d) ;
}
}
for(int j = ; j < ; ++j){
int idx = (v-) * + j;
int idy = (u-) * + j + ;
for(int k = ; k <= ; ++k){
add(idx, idy, , c + (k * k - (k-) * (k-))* d) ;
}
}
}
printf("%d\n", MinCostFlow(s, t));
return ;
}
CodeForces 1187G Gang Up 费用流的更多相关文章
- CodeForces 164C Machine Programming 费用流
Machine Programming 题目连接: http://codeforces.com/problemset/problem/164/B Descriptionww.co One remark ...
- Codeforces 708D 上下界费用流
给你一个网络流的图 图中可能会有流量不平衡和流量>容量的情况存在 每调整一单位的流量/容量 需要一个单位的花费 问最少需要多少花费使得原图调整为正确(可行)的网络流 设当前边信息为(u,v,f, ...
- codeforces gym 100357 I (费用流)
题目大意 给出一个或与表达式,每个正变量和反变量最多出现一次,询问是否存在一种方案使得每个或式中有且仅有一个变量的值为1. 解题分析 将每个变量拆成三个点x,y,z. y表示对应的正变量,z表示对应的 ...
- Codeforces Gym 100002 E "Evacuation Plan" 费用流
"Evacuation Plan" Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/10 ...
- Codeforces Gym 101190M Mole Tunnels - 费用流
题目传送门 传送门 题目大意 $m$只鼹鼠有$n$个巢穴,$n - 1$条长度为$1$的通道将它们连通且第$i(i > 1)$个巢穴与第$\left\lfloor \frac{i}{2}\rig ...
- Codeforces 280D k-Maximum Subsequence Sum [模拟费用流,线段树]
洛谷 Codeforces bzoj1,bzoj2 这可真是一道n倍经验题呢-- 思路 我首先想到了DP,然后矩阵,然后线段树,然后T飞-- 搜了题解之后发现是模拟费用流. 直接维护选k个子段时的最优 ...
- BZOJ 3836 Codeforces 280D k-Maximum Subsequence Sum (模拟费用流、线段树)
题目链接 (BZOJ) https://www.lydsy.com/JudgeOnline/problem.php?id=3836 (Codeforces) http://codeforces.com ...
- Codeforces 730I [费用流]
/* 不要低头,不要放弃,不要气馁,不要慌张 题意: 给两行n个数,要求从第一行选取a个数,第二行选取b个数使得这些数加起来和最大. 限制条件是第一行选取了某个数的条件下,第二行不能选取对应位置的数. ...
- Codeforces 362E Petya and Pipes 费用流建图
题意: 给一个网络中某些边增加容量,增加的总和最大为K,使得最大流最大. 费用流:在某条边增加单位流量的费用. 那么就可以2个点之间建2条边,第一条给定边(u,v,x,0)这条边费用为0 同时另一条边 ...
随机推荐
- python 爬取豆瓣电影评论,并进行词云展示及出现的问题解决办法
本文旨在提供爬取豆瓣电影<我不是药神>评论和词云展示的代码样例 1.分析URL 2.爬取前10页评论 3.进行词云展示 1.分析URL 我不是药神 短评 第一页url https://mo ...
- http协议(一):http协议基础知识
1 协议类型 l HTTP 超文本传输协议 通过浏览器和服务器进行数据交互,进行超文本(文本.图片.视频等)传输的规定 l HTTPS 安全超文本传输协议 l FTP 文本传输协议 l ...
- Hexo结合github制作博客
https://blog.csdn.net/Hoshea_chx/article/details/78826689 hexo(themes) vuePress jekylly
- xpath beautiful pyquery三种解析库
这两天看了一下python常用的三种解析库,写篇随笔,整理一下思路.太菜了,若有错误的地方,欢迎大家随时指正.......(conme on.......) 爬取网页数据一般会经过 获取信息-> ...
- OV SSL证书有哪些功能?网站安装OV SSL证书的好处
OV SSL证书英文名称为Organization Validation SSL Certificate,申请OV SSL证书需要审核申请者对域名是否拥有控制权,同时审核申请者是否为一个合法登记.真实 ...
- selenium操作cookies实现免密登录,自动发微博
一直想用selenium实现个小功能,比如发微博之类的,但是有的网站在登录会有验证码,没想到太好的方法解决,于是想到利用cookies来登录网站 第一步:获取一个可用的cookies,获取的cooki ...
- 关于p标签不能嵌套div标签引发的标签嵌套问题总结
问题由来:<p>中嵌套<div>标签,两个都是块级元素,按理应该可以正常显示,但是最后的结果居然是多出来一段<p>的效果,所以就在网上找了许多关于标签嵌套规则的资料 ...
- ASP.NET Core MVC 之过滤器(Filter)
ASP.NET MVC 中的过滤器允许在执行管道中的特定阶段之前或之后运行代码.可以对全局,也可以对每个控制器或每个操作配置过滤器. 1.过滤器如何工作 不同的过滤器类型在管道中的不同阶段执行,因此具 ...
- Spring系列(四):Spring AOP详解
一.AOP是什么 AOP(面向切面编程),可以说是一种编程思想,其中的Spring AOP和AspectJ都是现实了这种编程思想.相对OOP(面向过程编程)来说,提供了另外一种编程方式,对于OOP过程 ...
- 从IDEA角度来看懂UML图
前言 我们目前已经学习了设计模式的7种设计原则.下面本该是直接进入具体的设计模式系列文章. 但是呢在我们学习设计模式之前我们还是有必要了解一下uml图.因为后续的设计模式文章不出意外应该会很多地方使用 ...