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 同时另一条边 ...
随机推荐
- 【JDK】JDK源码分析-LinkedList
概述 相较于 ArrayList,LinkedList 在平时使用少一些. LinkedList 内部是一个双向链表,并且实现了 List 接口和 Deque 接口,因此它也具有 List 的操作以及 ...
- 【Spring】The matching wildcard is strict……
applicationContext.xml 文件抛出了这个异常信息. 解决方法: 需要在 namespace 后加上对应的 schemaLocation,如下所示: <?xml version ...
- Extjs的使用总结笔记
一:Extjs自带验证 1.alpha //只能输入字母,无法输入其他(如数字,特殊符号等) 2.alphanum//只能输入字母和数字,无法输入其他 3.email//email验证,要求的格式是& ...
- FTP服务端部署
FTP服务端搭建(本地用户登入:使用本地用户和密码登入)1.文件配置:vsftpd.conf: 主配置文件ftpusers: 指定哪些用户不能访问FTP服务器user_list: 指定的用户是否可以访 ...
- 配置VNC并远程控制服务器(电脑)
先象征性介绍一下: VNC (Virtual Network Console)是虚拟网络控制台的缩写, 它是一款基于 UNIX 和 Linux 操作系统的优秀.免费.开源的远程控制工具软件. 然后开始 ...
- kubernetes集群升级的正确姿势
kubernetes社区非常活跃,每季度都会发布一个release.但是线上集群业务可用性要求较高,场景复杂,任何微小的变更都需要非常小心,此时跟随社区版本进行升级略显吃力.但是为了能够使用到最新的一 ...
- appcan IDE 无法 请求数据
我们4月27号从4.0.1升级到4.0.2后,IDE本地预览get请求不到数据.但是在线打包安装到手机又是正常的. 先下载 "uexXmlHttpMgr.rar",下载链接:htt ...
- koa2图片上传成功后返回服务器地址,实时显示服务器图片
版本:node(8.5.0); koa(2.4.1); koa-router(7.3.0); koa-body(2.5.0); koa-static(4.0.2); 代码实现 const fs = r ...
- Android Bluetooth Low Energy (BLE)简单方便的蓝牙开源库——EasyBLE
源码传送门 最新版本 功能 支持多设备同时连接 支持广播包解析 支持连接同时配对 支持搜索系统已连接设备 支持搜索器设置 支持自定义搜索过滤条件 支持自动重连.最大重连次数限制.直接重连或搜索到设备再 ...
- kpm字符串匹配算法
首先是简单的朴素匹配算法 /* * 返回子串t在主串s的位置,若不存在则返回0 */ public static int index(String s, String t) { int i = 0;/ ...