[CODEVS1915] 分配问题(最小费用最大流)
2024-10-20 13:12:16
脑残题
建图都懒得说了
——代码
#include <queue>
#include <cstdio>
#include <cstring>
#include <iostream>
#define N 1000001
#define min(x, y) ((x) < (y) ? (x) : (y)) int n, cnt, s, t;
int a[][], dis[N], pre[N];
int head[N], to[N << ], val[N << ], cost[N << ], next[N << ];
bool vis[N]; inline int read()
{
int x = , f = ;
char ch = getchar();
for(; !isdigit(ch); ch = getchar()) if(ch == '-') f = -;
for(; isdigit(ch); ch = getchar()) x = (x << ) + (x << ) + ch - '';
return x * f;
} inline void add(int x, int y, int z, int c)
{
to[cnt] = y;
val[cnt] = z;
cost[cnt] = c;
next[cnt] = head[x];
head[x] = cnt++;
} inline bool spfa()
{
int i, u, v;
std::queue <int> q;
memset(vis, , sizeof(vis));
memset(pre, -, sizeof(pre));
memset(dis, / , sizeof(dis));
q.push(s);
dis[s] = ;
while(!q.empty())
{
u = q.front(), q.pop();
vis[u] = ;
for(i = head[u]; i ^ -; i = next[i])
{
v = to[i];
if(val[i] && dis[v] > dis[u] + cost[i])
{
dis[v] = dis[u] + cost[i];
pre[v] = i;
if(!vis[v])
{
q.push(v);
vis[v] = ;
}
}
}
}
return pre[t] ^ -;
} inline int dinic()
{
int i, d, sum = ;
while(spfa())
{
d = 1e9;
for(i = pre[t]; i ^ -; i = pre[to[i ^ ]]) d = min(d, val[i]);
for(i = pre[t]; i ^ -; i = pre[to[i ^ ]])
{
val[i] -= d;
val[i ^ ] += d;
}
sum += dis[t] * d;
}
return sum;
} int main()
{
int i, j, x;
n = read();
s = , t = n + n + ;
memset(head, -, sizeof(head));
for(i = ; i <= n; i++)
{
add(s, i, , );
add(i, s, , );
add(i + n, t, , );
add(t, i + n, , );
for(j = ; j <= n; j++)
{
a[i][j] = read();
add(i, j + n, , a[i][j]);
add(j + n, i, , -a[i][j]);
}
}
printf("%d\n", dinic());
cnt = ;
memset(head, -, sizeof(head));
for(i = ; i <= n; i++)
{
add(s, i, , );
add(i, s, , );
add(i + n, t, , );
add(t, i + n, , );
for(j = ; j <= n; j++)
{
add(i, j + n, , -a[i][j]);
add(j + n, i, , a[i][j]);
}
}
printf("%d\n", -dinic());
return ;
}
最新文章
- css字体家族
- BINARY SEARCH in read table statement
- iOS - Swift SingleClass		单例类
- 网络流(费用流)CodeForces 321B:Ciel and Duel
- 验证abc三列数字符合我的小弟要求
- JavaScript 运动框架 Step by step
- ios开发 oc 的类方法与对象方法
- python基础-------模块与包(三)正则表达式
- TinyMapper 使用总结
- dubbo源码分析1——SPI机制的概要介绍
- go import 使用方法记录
- git-stash用法小结
- Javascript深入理解构造函数和原型对象
- 《算法》第五章部分程序 part 7
- 关于QT和SQLite以及XML
- v-if案例解析(element-ui form-item 结合 v-if 动态生成rule规则\表单元素,表单无法验证问题剖析 )
- iOS 中的静态库与动态库,区别、制作和使用
- Python爬取猫眼top100排行榜数据【含多线程】
- layoutSubviews中判断横竖屏
- Linux内核(6) - 模块机制与“Hello World!
热门文章
- [Tracking] KCF + KalmanFilter目标跟踪
- ios基础学习
- Dojo的dojoConfig函数
- Codeforces Round 513 (Div.1+Div.2)
- Map集合应用 取出一个字符串中字母出现的次数。如:字符串:";abcdekka27qoq";&#160;,输出格式为:a(2)b(1)k(2)...
- 代码块(block)的使用
- Ubuntux下简单设置vim
- Yii2.0学习--目录结构
- Java 编辑html模板并生成pdf
- 七周成为数据分析师06_MySQL