【费用流】【线性规划】志愿者招募(luogu 3980)
生活随笔
收集整理的這篇文章主要介紹了
【费用流】【线性规划】志愿者招募(luogu 3980)
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
正題
luogu 3980
題目大意
有n個時刻,第i個時刻需要aia_iai?個志愿者,有m類志愿者,第j類可以從ljl_jlj?做到rjr_jrj?,代價為wjw_jwj?,數量無限,問你使所有時刻志愿者個數都足夠的最小代價
解題思路
經典的線性規劃
從i到i+1連一條流量inf?aiinf-a_iinf?ai?費用0的邊,然后每類志愿者從ljl_jlj?向rjr_jrj?連流量inf,費用wjw_jwj?的邊
然后s到1,n+1到t連流量inf費用0的邊(n+1是因為n的需求要存到n→n+1n\to n+1n→n+1的邊上)
跑費用流,這樣使得總流量為inf,費用最小
代碼
#include<queue> #include<cstdio> #include<cstring> #include<iostream> #include<algorithm> #define ll long long #define N 1010 using namespace std; int n, m, x, y, s, t, tot, p[N], h[N], fr[N]; ll z, ans, b[N], f[N]; queue<int>d; struct rec {int to, next;ll f, c; }e[N<<5]; const ll inf = 1e15; void add(int x, int y, ll z, ll k) {e[++tot].to = y;e[tot].f = z;e[tot].c = k;e[tot].next = h[x];h[x] = tot;e[++tot].to = x;e[tot].f = 0;e[tot].c = -k;e[tot].next = h[y];h[y] = tot;return; } bool bfs() {memset(b, 0x7f, sizeof(b));memset(f, 0, sizeof(f));d.push(s);p[s] = 1;b[s] = 0;f[s] = inf;while(!d.empty()){int u = d.front();d.pop();for (int i = h[u]; i; i = e[i].next){int v = e[i].to;if (e[i].f && b[u] + e[i].c < b[v]){b[v] = b[u] + e[i].c;f[v] = min(f[u], e[i].f);fr[v] = i;if (!p[v]){p[v] = 1;d.push(v);}}}p[u] = 0;}return f[t]; } void dfs() {int now = t;while(fr[now]){e[fr[now]].f -= f[t];e[fr[now]^1].f += f[t];now = e[fr[now]^1].to;}ans += f[t] * b[t]; } int main() {scanf("%d%d", &n, &m);s = n + 2;t = n + 3;tot = 1;for (int i = 1; i <= n; ++i){scanf("%lld", &z);add(i, i + 1, inf - z, 0);}add(s, 1, inf, 0);add(n + 1, t, inf, 0);for (int i = 1; i <= m; ++i){scanf("%d%d%lld", &x, &y, &z);add(x, y + 1, inf, z);}while(bfs())dfs();printf("%lld", ans);return 0; }總結
以上是生活随笔為你收集整理的【费用流】【线性规划】志愿者招募(luogu 3980)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 安卓手机怎样当作无线路由器安卓手机有办法
- 下一篇: 【Trie】【费用流】管道监控(loj