记事区 [置顶]CSP-S2023一等也不改签 [置顶]NOIP2023够蓝钩线也不改签(谁让我是初中牲呢) [置顶]进省队也不改签 费用流模板: #include <iostream> #include <queue> #include <memory.h> using namespace std; typedef long long ll; const ll N = 5003, M = 50003, inf = 3e9; ll x, y, z; ll cnt = 1; ll w, n, m, h, t, st, ed; ll cyf[N], pan[N], pre[N], dis[N], head[N]; ll mincost, maxflow; struct edge { ll to, next; ll w; ll flow; }es[M<<1]; queue<ll> qu; void addedge(ll x, ll y, ll z, ll w) { cnt++; es[cnt].flow = z; es[cnt].w = w; es[cnt].to = y; es[cnt].next = head[x]; head[x] = cnt; return ; } void add(ll a, ll b, ll flow, ll w) { addedge(a, b, flow, w); addedge(b, a, 0, -w); return ; } ll spfa(ll st, ll ed) { memset(dis, 0x7f, sizeof(dis)); memset(cyf, 0x7f, sizeof(cyf)); memset(pan, 0, sizeof(pan)); for(ll i = 0; i < ed; ++ i) { dis[i] =0; pan[i] = 0; } qu.push(st); pan[st] = 1; dis[st] = 0; cyf[st] = inf; while(!qu.empty()) { ll x = qu.front(); qu.pop(); pan[x] = 0; for(ll i = head[x], to; i; i = es[i].next) { if(es[i].flow && dis[to = es[i].to] > dis[x] + es[i].w) { dis[to] = dis[x] + es[i].w; pre[to] = i; cyf[to] = min(cyf[x], es[i].flow); if(!pan[to]) { pan[to] = 1; qu.push(to); } } } } return dis[ed] != inf; } void ek(ll st, ll ed) { while(spfa(st, ed)) { ll x = ed; maxflow = cyf[ed]; mincost += cyf[ed] * dis[ed]; while(x != st) { ll i = pre[x]; es[i].flow -= cyf[ed]; es[i^1].flow += cyf[ed]; x = es[i^1].to; } } } int main() { cin >> n >> m >> st >> ed; while(m --) { cin >> x >> y >> z >> w; add(x, y, z, w); } ek(st, ed); cout << maxflow << " " << mincost; return 0; } 首个没ctj切掉的黑题:AT_geocon2013_b,费用流115行。 首黑代码: //AT炸了? #include <iostream> #include <cmath> #include <queue> #include <memory.h> using namespace std; typedef long long ll; const ll N = 5003, M = 50003, inf = 1e18; ll cnt = 1; ll w, n, m, h, t, st, ed; ll pre[N], head[N], x[N], y[N]; bool pan[N]; ll maxflow; double dis[N]; double mincost; struct edge { ll to, next; double w; ll flow; }es[M<<1]; queue<ll> qu; void addedge(ll x, ll y, ll z, double w) { cnt++; es[cnt].flow = z; es[cnt].w = w; es[cnt].to = y; es[cnt].next = head[x]; head[x] = cnt; return ; } void add(ll a, ll b, ll flow, double w) { addedge(a, b, flow, w); addedge(b, a, 0, -w); return ; } bool spfa(ll st, ll ed) { memset(dis, 0x7f, sizeof(dis)); memset(pan, 0, sizeof(pan)); for(ll i = 0; i <= ed; ++ i) { dis[i] = inf; pan[i] = 0; } qu.push(st); dis[st] = 0; while(!qu.empty()) { ll x = qu.front(); qu.pop(); pan[x] = 0; for(ll i = head[x], to; i; i = es[i].next) { if(es[i].flow && dis[to = es[i].to] > dis[x] + es[i].w) { dis[to] = dis[x] + es[i].w; pre[to] = i; if(!pan[to]) { pan[to] = 1; qu.push(to); } } } } return dis[ed] != inf; } void ek() { while(spfa(0, (n << 1) + 1)) { mincost += dis[(n << 1) + 1]; int i = (n << 1) + 1; while(i) { es[pre[i]].flow --; es[pre[i] ^ 1].flow ++; i = es[pre[i] ^ 1].to; } } } int main() { cin >> n; for(int i = 1; i <= n; i ++) { cin >> x[i] >> y[i]; add(0, i, 1, 0); add(i + n, (n << 1) + 1, 1, 0); add(i, i + n, 1, abs(x[i])); } for (int i = 1; i <= n; ++ i) { for (int j = 1; j <= n; ++ j) { if (i != j) { add(i, j + n, 1, sqrt(pow((x[i] + x[j]), 2) + pow((y[i]-y[j]), 2)) / 2); } } } ek(); printf("%.7lf", mincost); return 0; } 计划:LCT,主席树,替罪羊树,树套树,偏序,斜率优化。 ------------------------------------------------------------------------------------------------------------------------------- 用户留言版 WA_Coding_Duck 到此一游 ——@WA_Coding_Duck
Note.ms
/yzwfq2000