# 囤题 [补档]

## 图论

### 网络流相关

CF 653 D

#include <cstdio>
#include <cctype>
#include <cstring>
#include <climits>
#include <deque>
#include <algorithm>

using namespace std;
inline int getInt()
{
int a = 0, sgn = 1; char c;
while (! isdigit(c = getchar())) if (c == '-') sgn *= -1;
while (isdigit(c)) a = a * 10 + c - '0', c = getchar();
return a * sgn;
}
typedef double D;
typedef long long LL;
const D EPS = 1e-8;
const LL INF = (LL)1e18;
const int N = 50, M = 500;
int n, m, x;
struct Record
{
int u, v, w;
}rec[M + 7];
struct edge
{
int v, nxt;
LL w;
}edg[M * 2 + 7];
int tot, hd[N + 7], dis[N + 7];
inline void addEdge(int u, int v, LL w) { edg[tot].v = v; edg[tot].w = w; edg[tot].nxt = hd[u]; hd[u] = tot ++; }
inline int BFS()
{
memset(dis, -1, sizeof dis); dis[1] = 0;
deque<int> que; que.push_back(1);
while (! que.empty())
{
int u = que.front(); que.pop_front();
if (u == n) return 1;
for (int p = hd[u]; ~ p; p = edg[p].nxt) if (edg[p].w && dis[edg[p].v] == -1) dis[edg[p].v] = dis[u] + 1, que.push_back(edg[p].v);
}
return 0;
}
LL DFS(int u, LL flw)
{
if (u == n) return flw;
LL usd = 0;
for (int p = hd[u]; ~ p; p = edg[p].nxt) if (edg[p].w && dis[edg[p].v] == dis[u] + 1)
{
int v = edg[p].v;
LL cur = DFS(v, min(edg[p].w, flw - usd));
edg[p].w -= cur; edg[p ^ 1].w += cur;
usd += cur;
if (usd == flw) return usd;
}
return usd;
}
inline LL flow()
{
LL sum = 0;
while (BFS()) sum += DFS(1, INF);
return sum;
}
int main()
{

#ifndef ONLINE_JUDGE

freopen("bear.in", "r", stdin);
freopen("bear.out", "w", stdout);

#endif

n = getInt(); m = getInt(); x = getInt();
for (int i = 0; i < m; ++ i) rec[i]. u = getInt(), rec[i].v = getInt(), rec[i].w = getInt();
D L = 1e-5, R = 1e6;
while (R - L > EPS)
{
D mid = (L + R) / 2;
memset(hd, -1, sizeof hd);
tot = 0; for (int i = 0; i < m; ++ i) addEdge(rec[i].u, rec[i].v, (LL)rec[i].w / mid), addEdge(rec[i].v, rec[i].u, 0);
if (flow() < x) R = mid; else L = mid;
}
printf("%.7lf", L * (D)x);
}

CF 884 F

#include <cstdio>
#include <cstring>
#include <deque>
#include <climits>

using namespace std;
const int N = 100;
const int V = N / 2 + 107;
const int E = 31 * N + 107;
int n;
int cnt[27];
int str[N + 7], b[N + 7];
struct edge
{
int v, w, c, nxt;
}edg[E];
int hd[V], tot;
int pre[V], rec[V], inQueue[V], dis[V];
inline void addEdge(int u, int v, int w, int c)
{
edg[tot].v = v; edg[tot].w = w; edg[tot].c = c; edg[tot].nxt = hd[u]; hd[u] = tot ++;
edg[tot].v = u; edg[tot].w = 0; edg[tot].c = - c; edg[tot].nxt = hd[v]; hd[v] = tot ++;
}
inline int SPFA()
{
int t = 26 + n / 2 + 1;
memset(pre, -1, sizeof pre);
deque<int> que; que.push_back(0);
memset(inQueue, 0, sizeof inQueue); inQueue[0] = 1;
memset(dis, -127, sizeof dis); dis[0] = 0;
while (! que.empty())
{
int u = que.front(); que.pop_front(); inQueue[u] = 0;
for (int p = hd[u]; ~ p; p = edg[p].nxt) if (edg[p].w && dis[u] + edg[p].c > dis[edg[p].v])
{
int v = edg[p].v;
dis[v] = dis[u] + edg[p].c; pre[v] = u; rec[v] = p;
if (! inQueue[v]) inQueue[v] = 1, que.push_back(v);
}
}
int mn = INT_MAX;
for (int p = t; ~ pre[p]; p = pre[p]) mn = min(mn, edg[rec[p]].w);
for (int p = t; ~ pre[p]; p = pre[p])
edg[rec[p]].w -= mn, edg[rec[p] ^ 1].w += mn;
return dis[t];
}
inline int flow()
{
int sum = 0;
while (1) { int tmp = SPFA(); if (tmp < - (int)2e9) return sum; sum += tmp; }
}
int main()
{

#ifndef ONLINE_JUDGE

freopen("anti.in", "r", stdin);
freopen("anti.out", "w", stdout);

#endif

scanf("%d\n", &n);
for (int i = 1; i <= n; ++ i) ++ cnt[str[i] = (getchar() - 'a' + 1)];
for (int i = 1; i <= n; ++ i) scanf("%d", b + i);
memset(hd, -1, sizeof hd); tot = 0;
for (int i = 1; i <= 26; ++ i) addEdge(0, i, cnt[i], 0);
for (int i = 1; i <= 26; ++ i) for (int j = 1; j <= n >> 1; ++ j)
addEdge(i, 26 + j, 1, max((str[j] == i) * b[j], (str[n - j + 1] == i) * b[n - j + 1]));
for (int i = 1; i <= n >> 1; ++ i) addEdge(26 + i, 26 + n / 2 + 1, 2, 0);
printf("%d\n", flow());
}


• 0
点赞
• 0
收藏
• 0
评论
08-13 454
09-13 68
08-13 313
05-30 111
01-10 49
09-02 1219
07-27 143
03-30 548
05-29 476
08-20 184
12-23 251
11-05 82

• 非常没帮助
• 没帮助
• 一般
• 有帮助
• 非常有帮助