首页 > 其他分享 >Luogu P3980 [NOI2008]志愿者招募

Luogu P3980 [NOI2008]志愿者招募

时间:2022-10-25 14:03:56浏览次数:66  
标签:fr int Luogu dis P3980 add inf include NOI2008


题目链接:​​传送门​

别人家的建图~~~~
好神奇
很容易想到志愿者的起始时间和终止时间连边,费用就是他的费用
但是每个点还有一个人数限制
必须要有那么多个人
也就是那么大的流量流过这个点
网上普遍的做法是
起点S为0号点,终点为n+2号点
S向1号点连容量inf,费用0的边
n+1号点向T连容量inf,费用为0的边
对于每个志愿者就那样连
从S连到T+1,容量inf,费用为C
对于人数限制
让每个i号点向i+1号点连边,容量inf-限制,费用为0
这样的意思就是
你一定要把减去的限制流满
也就满足了题目要求
因为初始的流量为inf
到了这里就要分流
人不够的就只能走带权边

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <complex>
#include <algorithm>
#include <climits>
#include <queue>
#include <map>
#include <set>
#include <vector>
#include <iomanip>
#define
#define

using namespace std;
typedef long long ll;
const int inf = 0x3f3f3f3f;
struct node {
int next, to, f, w;
}e[A];
int head[A], num = -1;
void add(int fr, int to, int f, int w) {
e[++num].next = head[fr];
e[num].to = to;
e[num].f = f;
e[num].w = w;
head[fr] = num;
}
int dep[A], vis[A], n, m, S, T, pre[A], dis[A], last[A], f[A], a, b, c;
bool spfa() {
memset(dis, 0x3f, sizeof dis);
memset(f, 0x3f, sizeof f);
memset(vis, 0, sizeof vis);
queue<int> q; q.push(S); dis[S] = 0; pre[T] = -1;
while (!q.empty()) {
int fr = q.front(); q.pop(); vis[fr] = 0;
for (int i = head[fr]; ~i; i = e[i].next) {
int ca = e[i].to;
if (e[i].f and dis[ca] > dis[fr] + e[i].w) {
dis[ca] = dis[fr] + e[i].w;
pre[ca] = fr; last[ca] = i;
f[ca] = min(f[fr], e[i].f);
if (!vis[ca]) {
vis[ca] = 1;
q.push(ca);
}
}
}
}
return pre[T] != -1;
}
int EK(int ans = 0) {
while (spfa()) {
int fr = T;
ans += f[T] * dis[T];
while (fr != S) {
e[last[fr]].f -= f[T];
e[last[fr] ^ 1].f += f[T];
fr = pre[fr];
}
}
return ans;
}

int main(int argc, char const *argv[]) {
memset(head, -1, sizeof head);
cin >> n >> m; S = 0; T = n + 2;
add(S, 1, inf, 0); add(1, S, 0, 0);
add(n + 1, T, inf, 0); add(T, n + 1, 0, 0);
for (int i = 1; i <= n; i++) {
cin >> a;
add(i, i + 1, inf - a, 0);
add(i + 1, i, 0, 0);
}
for (int i = 1; i <= m; i++) {
cin >> a >> b >> c;
add(a, b + 1, inf, c);
add(b + 1, a, 0, -c);
}
cout << EK() << endl;
}


标签:fr,int,Luogu,dis,P3980,add,inf,include,NOI2008
From: https://blog.51cto.com/lyle/5794667

相关文章

  • Luogu P2221 [HAOI2012]高速公路
    题目链接:​​传送门​​维护路径期望值,带区间修改看每条路径会被统计多少次贡献非常不显然是方案数就是上面的是分子下面的是分母现在要把上面的展开看怎么维护直接......
  • Luogu P2342 叠积木
    题目链接:​​传送门​​虽然看着很简单还是计较了好久!fa[]是这堆的最底下那块的编号siz[]是底块所在堆的sizeans[]就是ans,这块底下有多少块在find的时候沿路更新答案即......
  • Luogu P2150 [NOI2015]寿司晚宴
    题目链接:​​传送门​​太难了太难了题意就是问有多少种分案把一个到的排列分配为两组并使组间元素两两互质首先我们只需要考虑根号内的质因子对答案的影响,因为根号外的因......
  • Luogu P2515 [HAOI2010]软件安装
    题目链接:​​传送门​​很明显,如果图中有一个环那么这个环上的点必须都要选那我们一开始就直接缩点因为每个物品有价值有重量还有有重量限制所以是很明显的树上背包我......
  • Luogu P4421 [COCI2017-2018#1] Lozinke
    题目链接:​​传送门​​一开始直接AC自动机每个串暴力跳fail显然会T,44分#include<iostream>#include<cstdio>#include<cstring>#include<cstdlib>#include<complex>#i......
  • Luogu P3182 [HAOI2016]放棋子
    题目链接:​​传送门​​题目说了每行有一个障碍两个障碍不在同一行也不在同一列那障碍放哪里就没关系了矩阵都不用输入或者这样理解:交换矩阵的某两行对答案是没有影响......
  • Luogu P1772 [ZJOI2006]物流运输
    题目链接:​​传送门​​很麻烦也很难想的一道题数据很小大胆yy详细解释在代码里#include<iostream>#include<cstdio>#include<cstring>#include<cstdlib>#include<co......
  • Luogu P4149 [IOI2011]Race
    题目链接:​​传送门​​#include<iostream>#include<cstdio>#include<cstring>#include<cstdlib>#include<complex>#include<algorithm>#include<climits>#include<......
  • Luogu P2859 [USACO06FEB]摊位预订Stall Reservations
    题目链接:​​传送门​​很明显的要贪心从左右端点考虑先排序保证单调性每次往后看有没有能接上的单调性才保证了这个往后看的复杂度于是就很好写了/***@Date:2019......
  • Luogu P3488 [POI2009]LYZ-Ice Skates
    题目链接:​​传送门​​号脚的人可以穿大小的鞋设为号脚的人的数量假设选择区间就要满足把提出来即维护一个全局最大子段和就好了#include<iostream>#include<cstdi......