题解:

先按时间轴将一个点拆成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 费用流的更多相关文章

  1. CodeForces 164C Machine Programming 费用流

    Machine Programming 题目连接: http://codeforces.com/problemset/problem/164/B Descriptionww.co One remark ...

  2. Codeforces 708D 上下界费用流

    给你一个网络流的图 图中可能会有流量不平衡和流量>容量的情况存在 每调整一单位的流量/容量 需要一个单位的花费 问最少需要多少花费使得原图调整为正确(可行)的网络流 设当前边信息为(u,v,f, ...

  3. codeforces gym 100357 I (费用流)

    题目大意 给出一个或与表达式,每个正变量和反变量最多出现一次,询问是否存在一种方案使得每个或式中有且仅有一个变量的值为1. 解题分析 将每个变量拆成三个点x,y,z. y表示对应的正变量,z表示对应的 ...

  4. Codeforces Gym 100002 E "Evacuation Plan" 费用流

    "Evacuation Plan" Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/10 ...

  5. Codeforces Gym 101190M Mole Tunnels - 费用流

    题目传送门 传送门 题目大意 $m$只鼹鼠有$n$个巢穴,$n - 1$条长度为$1$的通道将它们连通且第$i(i > 1)$个巢穴与第$\left\lfloor \frac{i}{2}\rig ...

  6. Codeforces 280D k-Maximum Subsequence Sum [模拟费用流,线段树]

    洛谷 Codeforces bzoj1,bzoj2 这可真是一道n倍经验题呢-- 思路 我首先想到了DP,然后矩阵,然后线段树,然后T飞-- 搜了题解之后发现是模拟费用流. 直接维护选k个子段时的最优 ...

  7. BZOJ 3836 Codeforces 280D k-Maximum Subsequence Sum (模拟费用流、线段树)

    题目链接 (BZOJ) https://www.lydsy.com/JudgeOnline/problem.php?id=3836 (Codeforces) http://codeforces.com ...

  8. Codeforces 730I [费用流]

    /* 不要低头,不要放弃,不要气馁,不要慌张 题意: 给两行n个数,要求从第一行选取a个数,第二行选取b个数使得这些数加起来和最大. 限制条件是第一行选取了某个数的条件下,第二行不能选取对应位置的数. ...

  9. Codeforces 362E Petya and Pipes 费用流建图

    题意: 给一个网络中某些边增加容量,增加的总和最大为K,使得最大流最大. 费用流:在某条边增加单位流量的费用. 那么就可以2个点之间建2条边,第一条给定边(u,v,x,0)这条边费用为0 同时另一条边 ...

随机推荐

  1. apache bench的简单使用

    ApacheBench是 Apache 附带的一个小工具,专门用于 HTTP Server 的benchmark testing,可以同时模拟多个并发请求. 需要针对web做压力测试,所以简单学习了一 ...

  2. zabbix监控WEB网站性能

    一直在纠结用什么实例来给大家演示呢?想来想去还是官方的好,那我们怎么用zabbix监控web性能和可用性呢?我们这边分为几个步骤:打开网站.登陆.登陆验证.退出,一共4个小step,看实例. 检测流程 ...

  3. 安装MySQL5.7 安装环境:CentOS7 64位 MINI版,

    安装环境:CentOS7 64位 MINI版,安装MySQL5.7 1.配置YUM源 在MySQL官网中下载YUM源rpm安装包:http://dev.mysql.com/downloads/repo ...

  4. Spring的数据库编程浅入浅出——不吹牛逼不装逼

    Spring的数据库编程浅入浅出——不吹牛逼不装逼 前言 上文书我写了Spring的核心部分控制反转和依赖注入,后来又衔接了注解,在这后面本来是应该写Spring AOP的,但我觉得对于初学者来说,这 ...

  5. java基础精选题

    Integer比较 看下面这段有意思的代码,对数字比较敏感的小伙伴有没有发现异常? public static void main(String[] args) { Integer a = 128,b ...

  6. 转载 | 一种让超大banner图片不拉伸、全屏宽、居中显示的方法

    现在很多网站的Banner图片都是全屏宽度的,这样的网站看起来显得很大气.这种Banner一般都是做一张很大的图片,然后在不同分辨率下都是显示图片的中间部分.实现方法如下: <html> ...

  7. Linux系统关机与重新引导流程简介

    引言 在<Linux启动之旅>中,我们了解了Linux的启动过程,下面我们一同来学习Linux关机与重新引导流程. 不同于桌面系统,作为服务器,我们较少对Linux系统进行系统重启,但在以 ...

  8. win7-BIOS中开启AHCI模式电脑蓝屏怎么办?

    win7-BIOS中开启AHCI模式电脑蓝屏怎么办? 来源:U大师 u盘装系统 不少网友都表示给电脑安装win7系统后,如果在BIOS中开启IDE模式就一切正常而为AHCI模式时就会出现蓝屏.其实那是 ...

  9. 不相交路径[BZOJ1471] 容斥原理 拓扑排序

    最近学容斥的时候又碰到一道类似的题目,所以想分享一个套路,拿这题来举例 [题目描述] 给出一个\(N(N\leq 150)\)个结点的有向无环简单图.给出4个不同的点\(a,b,c,d\),定义不相交 ...

  10. WPF中TimeSpan的坑

    记一次在WPF中,在将格式为“DD.HH:mm:ss”字符串转换成TimeSpan时遇到的坑 如果字符串为:DD.HH:mm:ss,转换结果正确.例如: var currentValue = &quo ...