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的更多相关文章

  1. [codeforces 549]G. Happy Line

    [codeforces 549]G. Happy Line 试题描述 Do you like summer? Residents of Berland do. They especially love ...

  2. CodeForces 794 G.Replace All

    CodeForces 794 G.Replace All 解题思路 首先如果字符串 \(A, B\) 没有匹配,那么二元组 \((S, T)\) 合法的一个必要条件是存在正整数对 \((x,y)\), ...

  3. Codeforces 1207 G. Indie Album

    Codeforces 1207 G. Indie Album 解题思路 离线下来用SAM或者AC自动机就是一个单点加子树求和,套个树状数组就好了,因为这个题广义SAM不能存在 \(len[u] = l ...

  4. codeforces 659 G. Fence Divercity 组合数学 dp

    http://codeforces.com/problemset/problem/659/G 思路: f(i,0/1,0/1) 表示到了第i个,要被切的块开始了没有,结束了没有的状态的方案数 递推看代 ...

  5. Codeforces 803 G. Periodic RMQ Problem

    题目链接:http://codeforces.com/problemset/problem/803/G 大致就是线段树动态开节点. 然后考虑到如果一个点还没有出现过,那么这个点显然未被修改,就将这个点 ...

  6. Codeforces 954 G. Castle Defense

    http://codeforces.com/problemset/problem/954/G 二分答案 检验的时候,从前往后枚举,如果发现某个位置的防御力<二分的值,那么新加的位置肯定是越靠后越 ...

  7. Codeforces 746 G. New Roads

    题目链接:http://codeforces.com/contest/746/problem/G mamaya,不知道YY了一个什么做法就这样过去了啊 2333 首先我显然可以随便构造出一棵树满足他所 ...

  8. Codeforces 724 G Xor-matic Number of the Graph 线性基+DFS

    G. Xor-matic Number of the Graph http://codeforces.com/problemset/problem/724/G 题意:给你一张无向图.定义一个无序三元组 ...

  9. codeforces 626 G. Raffles(线段树+思维+贪心)

    题目链接:http://codeforces.com/contest/626/problem/G 题解:这题很明显买彩票肯定要买贡献最大的也就是说买p[i]*(num[i]+1)/(num[i]+a[ ...

随机推荐

  1. 学习Yii(3)

    组件 Component的或其子类的实例.属性和事件 组件的属性.(名称大小写不敏感) 可读可写,就像对象的公共成员变量. 通过$component->属性名.获取属性值.$component- ...

  2. 十步学习法 -- 来自<<软技能>>一书的学习方法论

    <<软技能>>第三篇“学习”,作者讲述了自己的学习方法:十步学习法.下面我用编程语言的方式来介绍. 十步学习法 伪代码介绍 # **这一步的目的不是要掌握整个主题,而是对相关内 ...

  3. Insomni’hack CTF-l33t-hoster复现分析

    题目地址: https://github.com/eboda/insomnihack/tree/master/l33t_hoster 源码如下: <?php if (isset($_GET[&q ...

  4. tensorflow搭建神经网络

    最简单的神经网络 import tensorflow as tf import numpy as np import matplotlib.pyplot as plt date = np.linspa ...

  5. beego conf配置文件

    1. 多个配置文件通过include引入 自定义配置文件mysql.conf 在app.conf 中引入mysql.conf include "mysql.conf"

  6. list列表相关操作

    ] ] ] : :-]print(s10)# a.sort(reve# rse=True)# print(a)# a.reverse()# print(a) lst = [], 'wusir','cg ...

  7. 0《STL源码剖析》简介

    STL源码剖析 ----侯捷 STL主要包括六个组件: 1.配置器:负责空间配置和管理. 2.迭代器:扮演容器和算法之前的胶合剂,所谓“泛型指针”. 3.容器:各种数据结构,如vector,list, ...

  8. 局部内部类的final问题

    局部内部类,如果希望访问所在方法的局部变量,那么这个局部变量就必须是final的(或者只赋值一次) 从Java8开始,只要局部变量事实不变那么final关键字可以省略 为什么需要保证变量为final, ...

  9. kafka server.properties 配置文件详解(二)

    虽然在前面一部分我们启动了kafka集群,并通过控制台的方式实现了producer和consumer,但是我们还是了解一下kafka单个节点是的配置参数属性, 也只有了解了这些参数的配置,才能将kaf ...

  10. Python解Leetcode: 226. Invert Binary Tree

    leetcode 226. Invert Binary Tree 倒置二叉树 思路:分别倒置左边和右边的结点,然后把根结点的左右指针分别指向右左倒置后返回的根结点. # Definition for ...