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. exchange 2010入门到精通

    exchange 2010入门到精通 Exchange产品介绍和功能演示 Exchange是什么 目前最受欢迎企业级邮件服务器产品 市场占有率70%(2011数据) 微软消息协作平台中核心产品 Exc ...

  2. LAG函数实现环比

    ,)OVER(ORDER BY 年月) 环比金额 from( 年, 季度, 年月 ,SUM(金额本位币) 金额 FROM ( SELECT * FROM [dbo].[T_output] ) cb_v ...

  3. bootstrap-table删除指定行注意事项

    方法有两种: 1.使用官方文档的数据(反正我试了2个小时都不行,如有大神请指导下):使用events和operate相结合的方式 2.不使用events,在formatter里面定义事件的实现. 上面 ...

  4. NEO4J (二)

    README 整理 kernel.impl.coreapi 这个包包含核心API的直接实现.核心API是org.neo4j中定义的API.graphdb及其子包. 这里的类是实现细节,可能会在不通知的 ...

  5. 【AtCoder】ARC065

    ARC065 C - 白昼夢 / Daydream 直接递推就好 #include <bits/stdc++.h> #define fi first #define se second # ...

  6. Hello TypeScript

    ⒈TypeScript简介 1.JavaScript的超集 2.支持ECMAScript6标准,并支持输出ECMAScript3/5/6标准的纯JavaScript代码 3.支持ECMAScript未 ...

  7. Kubernetes---Service(SVC)服务

    ⒈介绍 kubernetes 通过标签选择的方式来匹配一组pod,然后提供对外访问的一种机制 一组pod可以对应到多个svc的 每一个service(svc)都可以理解为一个微服务 Service有且 ...

  8. fiddler笔记:统计选项卡(Statistics)

    Request Count 选中的Session数. Bytes sent Http请求头和请求体中向外发送的字节总数. Bytes received HTTP请求头和请求体中接收到的所有字节数. R ...

  9. 【Python基础】05_Python中的while循环

    1.程序的三大流程介绍 顺序 —— 从上到下,顺序执行 分支 —— 根据条件判断,决定代码的分支 循环 —— 让特定代码执行 2.while 基本语法 while 条件(判断 计数器 是否达到 目标次 ...

  10. Get HttpWebResponse and HttpClient Return String by proxy

    #region Get HttpClient Return String /// <summary> /// Get HttpClient Return String /// </s ...