Codeforces 1187 G - Gang Up
思路:
每个点按时间拆点建边,然后跑最小费用流
一次走的人不能太多,假设每次走的人为k
(k*k-(k-1)*(k-1))*d <= c+d
k <= 24
代码:
#pragma GCC optimize(2)
#pragma GCC optimize(3)
#pragma GCC optimize(4)
#include<bits/stdc++.h>
using namespace std;
#define y1 y11
#define fi first
#define se second
#define pi acos(-1.0)
#define LL long long
//#define mp make_pair
#define pb push_back
#define ls rt<<1, l, m
#define rs rt<<1|1, m+1, r
#define ULL unsigned LL
#define pll pair<LL, LL>
#define pli pair<LL, int>
#define pii pair<int, int>
#define piii pair<pii, int>
#define pdd pair<double, double>
#define mem(a, b) memset(a, b, sizeof(a))
#define debug(x) cerr << #x << " = " << x << "\n";
#define fio ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
//head int n, m, k, c, d, a[], x, y;
const int N = + ;
const int INF = 0x3f3f3f3f;
struct edge {
int to, cap, cost, rev;
};
int V;
vector<edge>g[N];
int h[N], dis[N], prevv[N], preve[N];
void add_edge(int u, int v, int cap, int cost) {
g[u].pb({v, cap, cost, g[v].size()});
g[v].pb({u, , -cost, g[u].size()-});
}
int min_cost_flow(int s, int t, int f) {
int res = ;
mem(h, );
while(f > ) {
priority_queue<pii, vector<pii>, greater<pii> > q;
mem(dis, 0x3f);
dis[s] = ;
q.push({, s});
while(!q.empty()) {
pii p = q.top();
q.pop();
int v = p.se;
if(dis[v] < p.fi) continue;
for (int i = ; i < g[v].size(); ++i) {
edge &e = g[v][i];
if(e.cap > && dis[e.to] > dis[v] + e.cost + h[v] - h[e.to]) {
dis[e.to] = dis[v] + e.cost + h[v] - h[e.to];
prevv[e.to] = v;
preve[e.to] = i;
q.push({dis[e.to], e.to});
}
}
}
if(dis[t] == INF) return -;
for (int v = ; v < V; ++v) h[v] += dis[v];
int d = f;
for (int v = t; v != s; v = prevv[v]) d = min(d, g[prevv[v]][preve[v]].cap);
f -= d;
res += d*h[t];
for (int v = t; v != s; v = prevv[v]) {
edge &e = g[prevv[v]][preve[v]];
e.cap -= d;
g[v][e.rev].cap += d;
}
}
return res;
}
int s, t;
int main() {
scanf("%d %d %d %d %d", &n, &m, &k, &c, &d);
for (int i = ; i <= k; ++i) scanf("%d", &a[i]);
s = , t = *n+;
for (int i = ; i <= k; ++i) add_edge(s, (a[i]-)*+, , );
for (int i = ; i <= ; ++i) add_edge(i, t, k, );
for (int i = ; i <= n; ++i) {
for (int j = ; j < ; ++j)
add_edge((i-)*+j, (i-)*+j+, k, c);
}
for (int i = ; i <= m; ++i) {
scanf("%d %d", &x, &y);
for (int j = ; j < ; ++j) {
for (int k = ; k <= ; ++k) {
add_edge((x-)*+j, (y-)*+j+, , (k*k-(k-)*(k-))*d+c);
add_edge((y-)*+j, (x-)*+j+, , (k*k-(k-)*(k-))*d+c);
}
}
}
V = *n+;
printf("%d\n", min_cost_flow(s, t, k));
return ;
}
Codeforces 1187 G - Gang Up的更多相关文章
- [codeforces 549]G. Happy Line
[codeforces 549]G. Happy Line 试题描述 Do you like summer? Residents of Berland do. They especially love ...
- CodeForces 794 G.Replace All
CodeForces 794 G.Replace All 解题思路 首先如果字符串 \(A, B\) 没有匹配,那么二元组 \((S, T)\) 合法的一个必要条件是存在正整数对 \((x,y)\), ...
- Codeforces 1207 G. Indie Album
Codeforces 1207 G. Indie Album 解题思路 离线下来用SAM或者AC自动机就是一个单点加子树求和,套个树状数组就好了,因为这个题广义SAM不能存在 \(len[u] = l ...
- codeforces 659 G. Fence Divercity 组合数学 dp
http://codeforces.com/problemset/problem/659/G 思路: f(i,0/1,0/1) 表示到了第i个,要被切的块开始了没有,结束了没有的状态的方案数 递推看代 ...
- Codeforces 803 G. Periodic RMQ Problem
题目链接:http://codeforces.com/problemset/problem/803/G 大致就是线段树动态开节点. 然后考虑到如果一个点还没有出现过,那么这个点显然未被修改,就将这个点 ...
- Codeforces 954 G. Castle Defense
http://codeforces.com/problemset/problem/954/G 二分答案 检验的时候,从前往后枚举,如果发现某个位置的防御力<二分的值,那么新加的位置肯定是越靠后越 ...
- Codeforces 746 G. New Roads
题目链接:http://codeforces.com/contest/746/problem/G mamaya,不知道YY了一个什么做法就这样过去了啊 2333 首先我显然可以随便构造出一棵树满足他所 ...
- Codeforces 724 G Xor-matic Number of the Graph 线性基+DFS
G. Xor-matic Number of the Graph http://codeforces.com/problemset/problem/724/G 题意:给你一张无向图.定义一个无序三元组 ...
- codeforces 626 G. Raffles(线段树+思维+贪心)
题目链接:http://codeforces.com/contest/626/problem/G 题解:这题很明显买彩票肯定要买贡献最大的也就是说买p[i]*(num[i]+1)/(num[i]+a[ ...
随机推荐
- manjaro Docker环境配置记
1.系统配置如下: ‘by: /home/inkhin/桌面/深度截图_选择区域_20191004145104.png [吐槽: ChromeLinux版居然不能用博客园TinyMce的上传图片功能] ...
- HIVE配置mysql metastore
HIVE配置mysql metastore hive中除了保存真正的数据以外还要额外保存用来描述库.表.数据的数据,称为hive的元数据.这些元数据又存放在何处呢? 如果不修改配置hive ...
- 第8课.第一个ARM裸板程序(点亮led)及申引
1.原理图 2.芯片手册 3.几条汇编代码 1.ldr:读内存 ldr R0, [R1] 假设R1的值是x,读取地址x上的数据(4字节),保存到R0中 ldr R0, =0x12345678 (4字节 ...
- Orderly Class
题目链接: https://nanti.jisuanke.com/t/40449 题目大意:给出两个长度相同的不同字符串A, B.可以对A的任意长度区间进行一次翻转,问有多少种方法可以使得翻转后两字符 ...
- js中的var a = new A;与var a = new A()的区别
JavaScript 中的new关键字与C#,JAVA中的概念完全不一样. 例:var a=new A(); 让我们来看看在JavaScript中的new发生了什么? var a={};//建立 ...
- 【AtCoder】ARC069
ARC069 C - Scc Puzzle --不说了 #include <bits/stdc++.h> #define fi first #define se second #defin ...
- class CAdoInterface
AdoInterface.h #pragma once //#pragma warning (push) //以防会产生警告 C4018,但这个警告是没关系的,所以使用下一句代码屏蔽它 //#prag ...
- C++ 继承 - 在派生类中对基类初始化
构造函数与基类的其他成员不同,不能被派生类继承,因此为了初始化基类中的成员变量,需要在派生类中调用基类的构造函数(即显式调用),如果派送类没有调用则默认调用基类的无参构造函数(即隐式调用). 显式调用 ...
- S03_CH04_AXI_DMA_OV5640摄像头采集系统
S03_CH04_AXI_DMA_OV5640摄像头采集系统 4.1概述 本课程讲解如何搭建基于DMA的图形系统,方案原理和搭建7725的一样,只是OV5640显示的分辨率是1280X720如下,只是 ...
- 第六章 ZYNQ-MIZ701 GPIO使用之MIO
6.0 本章难度系数★★☆☆☆☆☆ 6.1 GPIO简介 Zynq7000系列芯片有54个MIO(multiuse I/O),它们分配在 GPIO 的Bank0 和Bank1隶属于PS部分,这些I ...