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. VB6_小林的气象类模块

    前言. [如果使用过程有什么问题可以QQ或邮箱联系我. 1919988942  | w2638301509@gmail.com] ___________________________________ ...

  2. Note 2: Complain

    Note 2: Complain 1. The collection of Linkun's [1]: 1.1suck If someone says that something sucks, th ...

  3. js 监听音频视频控件是否播放

    监听onplaying: var myVideo=document.getElementById("video1"); myVideo.addEventListener(" ...

  4. IOI 2005/bzoj 1812:riv 河流

    Description 几乎整个Byteland王国都被森林和河流所覆盖.小点的河汇聚到一起,形成了稍大点的河.就这样,所有的河水都汇聚并流进了一条大河,最后这条大河流进了大海.这条大河的入海口处有一 ...

  5. ubuntu配置kvm服务

    虚拟化第一弹,lei了lei了~ 首先,简单介绍一下KVM服务. KVM 全称是 Kernel-Based Virtual Machine,它是一种常用的虚拟化工具.是基于linux内核所开发的虚拟平 ...

  6. P61IDEA的常用快捷键

    Alt+Enter   导入包,自动修正代码 Ctrl+Y  删除光标所在行 Ctrl+D 复制光标所在行的内容,插入光标位置下面 Ctrl+Alt+L  格式化代码 Ctrl+/   单行注释 Al ...

  7. 深入理解JS的事件绑定、事件流模型

     一.JS事件 (一)JS事件分类 1.鼠标事件: click/dbclick/mouseover/mouseout 2.HTML事件: onload/onunload/onsubmit/onresi ...

  8. #【Python】【demo实验34】【练习实例】【设置文本的颜色】

    原题: 文本颜色设置. 我的代码 #!/usr/bin/python # encoding=utf-8 # -*- coding: UTF-8 -*- # 文本颜色设置. class bcolors: ...

  9. # 滚动Hash

    滚动Hash 假设字符串\(C=c_1*c_2*...c_m\),定义Hash函数\(H(C)=(C_1*b^{m-1}+C_2*b^{m-2}+...C_m*b^{0})mod\; h\) 从k开始 ...

  10. 【第二季】CH11_ ZYNQ软硬调试高级技巧

    [第二季]CH11_ ZYNQ软硬调试高级技巧 软件和硬件的完美结合才是SOC的优势和长处,那么开发ZYNQ就需要掌握软件和硬件开发的调试技巧,这样才能同时分析软件或者硬件的运行情况,找到问题,最终解 ...