题解:

先按时间轴将一个点拆成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. 【SVN】eclipse 安装 SVN 插件

    链接:eclipse中svn插件的安装 SVN 插件地址:http://subclipse.tigris.org/servlets/ProjectProcess;jsessionid=8EB28B11 ...

  2. Github上fork的项目如何merge原Git项目

    问题场景 小明在Github上fork了一个大佬的项目,并clone到本地开发一段时间,再提交merge request到原Git项目,过了段时间,原作者联系小明,扔给他下面这幅截图并告知合并处理冲突 ...

  3. 自定义SWT控件二之自定义多选下拉框

    2.自定义下拉多选框 package com.view.control.select; import java.util.ArrayList; import java.util.HashMap; im ...

  4. 使用Jasypt对SpringBoot配置文件加密

    # **前言** 在日前安全形势越来越严重的情况下,让我意识到在项目中存在一个我们经常忽略的漏洞,那就是我们的项目的配置文件中配置信息的安全,尤其是数据库连接的用户名和密码的安全.所以这里我们就需要对 ...

  5. 使用Typora编写博客并发布

    前言 用CSDN写了一段时间,广告漫天飞舞.... 于是在博客园申请了一个账号,然后看见markdown编辑页面的第一眼: 再见^_^ 搜索一波,凭着博客园强大的生态,30多万的用户,第三方的支持应接 ...

  6. 高性能MySQL之事物

    一.概念 事务到底是什么东西呢?想必大家学习的时候也是对事务的概念很模糊的.接下来通过一个经典例子讲解事务. 银行在两个账户之间转账,从A账户转入B账户1000元,系统先减少A账户的1000元,然后再 ...

  7. 深度学习模型训练技巧 Tips for Deep Learning

    一.深度学习建模与调试流程 先看训练集上的结果怎么样(有些机器学习模型没必要这么做,比如决策树.KNN.Adaboost 啥的,理论上在训练集上一定能做到完全正确,没啥好检查的) Deep Learn ...

  8. SimpleDateFormat线程不安全问题解决及替换方法

    场景:在多线程情况下为避免多次创建SimpleDateForma实力占用资源,将SimpleDateForma对象设置为static. 出现错误:SimpleDateFormat定义为静态变量,那么多 ...

  9. 科普向 + 折腾向 ——你了解磁盘、分区、文件系统、GPT、UEFI吗?在笔记本上安装五个系统是怎样的体验?

    [Windows 7 + Windows 8 (PE) + Windows 10 + deepin-Linux + MacOS X] 前言:随着软硬件技术的发展UEFI引导逐渐取代传统BIOS引导,最 ...

  10. Spring 集成Kafka(完整版)

    前面的文章我们已经完成了Kafka基于Zookeeper的集群的搭建了.Kafka集群搭建请点我.记过几天的研究已经实现Spring的集成了.本文重点 jar包准备 集成是基于spring-integ ...