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 同时另一条边 ...
随机推荐
- 【MySQL】日常小技巧汇总,更新中……
创建表时修改自增主键,添加 AUTO_INCREMENT=<Number> ,例如: CREATE TABLE `table_name` ( `id` int(11) unsigned N ...
- Selenium+java - 弹出框处理
一.弹出框分类: 弹出框分为两种,一种基于原生JavaScript写出来的弹窗,另一种是自定义封装好的样式的弹出框,本文重点介绍原生JavaScript写出来的弹窗,另一种弹窗用click()基本就能 ...
- solr使用心得
/** * @author zhipeng * @date 创建时间:2015-10-10 下午12:15:35 * @parameter * @return */ publ ...
- FormLayout and FormData
FormLayout通过为小窗口部件创建四边的Form附加值(attachment)来进行工作,并且把这些Form附加值存储在布局数据中.一个附加值让一个小窗口部件指定的一边粘贴(attach)到父C ...
- CEPH RGW多 ZONE的配置
相关的名称解释 Region :可以理解为区域,是基于地理位置的逻辑划分:如:华南,华北之类,包含多个region的Ceph集群必须指定一个master region,一个region可以包含一个或者 ...
- python_0基础开始_day05
第五节 一.字典 python的数据结构之一 字典 —— dict 定义:dic = {"key":"dajjlad"} 作用:存储数据,大量,将数据和数据起到 ...
- 第四章 文件的基本管理和XFS文件系统备份恢复 随堂笔记
第四章 文件的基本管理和XFS文件系统备份恢复 本节所讲内容: 4.1 Linux系统目录结构和相对/绝对路径. 4.2 创建/复制/删除文件,rm -rf / 意外事故 4.3 查看文件内容的命令 ...
- nginx负载均衡策略url_hash配置方法
参考文章: https://docs.nginx.com/nginx/admin-guide/load-balancer/http-load-balancer/ 根据路径,进行一致性hash,具体的配 ...
- 利用tensorboard可视化checkpoint模型文件参数分布
写在前面: 上周微调一个文本检测模型seglink,将特征提取层进行冻结,只训练分类回归层,然而查看tensorboard发现里面有histogram显示模型各个参数分布,看了目前这个训练模型参数分布 ...
- Jenkins持续集成项目搭建——基于Python Selenium自动化测试
参考链接:https://www.liaoxuefeng.com/article/1083282007018592 第一步:去官网https://jenkins.io/下载最新的war包 第二步:安装 ...