博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
运输问题 费用流
阅读量:6097 次
发布时间:2019-06-20

本文共 4524 字,大约阅读时间需要 15 分钟。

和luogu的分配问题一样,建边也非常简单,基本和[分配问题]一模一样;

#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
//#include
//#pragma GCC optimize(2)using namespace std;#define maxn 20005#define inf 0x7fffffff//#define INF 1e18#define rdint(x) scanf("%d",&x)#define rdllt(x) scanf("%lld",&x)#define rdult(x) scanf("%lu",&x)#define rdlf(x) scanf("%lf",&x)#define rdstr(x) scanf("%s",x)#define mclr(x,a) memset((x),a,sizeof(x))typedef long long ll;typedef unsigned long long ull;typedef unsigned int U;#define ms(x) memset((x),0,sizeof(x))const long long int mod = 1e9 + 7;#define Mod 1000000000#define sq(x) (x)*(x)#define eps 1e-5typedef pair
pii;#define pi acos(-1.0)//const int N = 1005;#define REP(i,n) for(int i=0;i<(n);i++)typedef pair
pii;inline int rd() { int x = 0; char c = getchar(); bool f = false; while (!isdigit(c)) { if (c == '-') f = true; c = getchar(); } while (isdigit(c)) { x = (x << 1) + (x << 3) + (c ^ 48); c = getchar(); } return f ? -x : x;}ll gcd(ll a, ll b) { return b == 0 ? a : gcd(b, a%b);}int sqr(int x) { return x * x; }/*ll ans;ll exgcd(ll a, ll b, ll &x, ll &y) { if (!b) { x = 1; y = 0; return a; } ans = exgcd(b, a%b, x, y); ll t = x; x = y; y = t - a / b * y; return ans;}*/bool vis[maxn];int n, m, s, t;int x, y, f, z;int dis[maxn], pre[maxn], last[maxn], flow[maxn];int maxflow, mincost;struct node { int to, nxt, flow, dis;}edge[maxn << 2];int head[maxn], cnt;void addedge(int from, int to, int flow, int dis) { edge[++cnt].to = to; edge[cnt].flow = flow; edge[cnt].dis = dis; edge[cnt].nxt = head[from]; head[from] = cnt;}bool spfa1(int s, int t) { memset(dis, 0x7f, sizeof(dis)); memset(flow, 0x7f, sizeof(flow)); ms(vis);queue
q; q.push(s); vis[s] = 1; dis[s] = 0; pre[t] = -1; while (!q.empty()) { int now = q.front(); q.pop(); vis[now] = 0; for (int i = head[now]; i != -1; i = edge[i].nxt) { if (edge[i].flow > 0 && dis[edge[i].to] > dis[now] + edge[i].dis) { dis[edge[i].to] = edge[i].dis + dis[now]; pre[edge[i].to] = now; last[edge[i].to] = i; flow[edge[i].to] = min(flow[now], edge[i].flow); if (!vis[edge[i].to]) { vis[edge[i].to] = 1; q.push(edge[i].to); } } } } return pre[t] != -1;}bool spfa2(int s, int t) { memset(dis, 0x7f, sizeof(dis)); memset(flow, 0x7f, sizeof(flow)); ms(vis); queue
q; q.push(s); vis[s] = 1; dis[s] = 0; pre[t] = -1; while (!q.empty()) { int now = q.front(); q.pop(); vis[now] = 0; for (int i = head[now]; i != -1; i = edge[i].nxt) { if (edge[i].flow > 0 && dis[edge[i].to] > dis[now] + edge[i].dis) { dis[edge[i].to] = edge[i].dis + dis[now]; pre[edge[i].to] = now; last[edge[i].to] = i; flow[edge[i].to] = min(flow[now], edge[i].flow); if (!vis[edge[i].to]) { vis[edge[i].to] = 1; q.push(edge[i].to); } } } } return pre[t] != -1;}void mincost_maxflow() { while (spfa1(s, t)) { int now = t; maxflow += flow[t]; mincost += flow[t] * dis[t]; while (now != s) { edge[last[now]].flow -= flow[t]; edge[last[now] ^ 1].flow += flow[t]; now = pre[now]; } }}void maxcost_maxflow() { while (spfa2(s, t)) { int now = t; maxflow += flow[t]; mincost += flow[t] * dis[t]; while (now != s) { edge[last[now]].flow -= flow[t]; edge[last[now] ^ 1].flow += flow[t]; now = pre[now]; } }}int A[maxn], B[maxn];int C[102][102];int main(){ // ios::sync_with_stdio(0); mclr(head, -1); cnt = 1; m = rd(); n = rd(); for (int i = 1; i <= m; i++)A[i] = rd(); for (int i = 1; i <= n; i++)B[i] = rd(); for (int i = 1; i <= m; i++) { for (int j = 1; j <= n; j++)C[i][j] = rd(); } int ans1 = 0, ans2 = 0; s = 0; t = n + m + 2; for (int i = 1; i <= m; i++) { addedge(s, i, A[i], 0); addedge(i, s, 0, 0); } for (int i = 1; i <= n; i++) { addedge(i + m, t, B[i], 0); addedge(t, i + m, 0, 0); } for (int i = 1; i <= m; i++) { for (int j = 1; j <= n; j++) { addedge(i, j + m, A[i], C[i][j]); addedge(j + m, i, 0, -C[i][j]); } } mincost_maxflow(); ans1 = mincost; mclr(head, -1); cnt = 1; ms(edge); ms(pre); ms(last); mincost = maxflow = 0; for (int i = 1; i <= m; i++) { addedge(s, i, A[i], 0); addedge(i, s, 0, 0); } for (int i = 1; i <= n; i++) { addedge(i + m, t, B[i], 0); addedge(t, i + m, 0, 0); } for (int i = 1; i <= m; i++) { for (int j = 1; j <= n; j++) { addedge(i, j + m, A[i], -C[i][j]); addedge(j + m, i, 0, C[i][j]); } } maxcost_maxflow(); ans2 = -mincost; cout << ans1 << endl; cout << ans2 << endl; return 0;}

 

转载于:https://www.cnblogs.com/zxyqzy/p/10384420.html

你可能感兴趣的文章
记一次eclipse无法启动的排查过程
查看>>
【转】jmeter 进行java request测试
查看>>
读书笔记--MapReduce 适用场景 及 常见应用
查看>>
SignalR在Xamarin Android中的使用
查看>>
走过电竞之路的程序员
查看>>
Eclipse和MyEclipse使用技巧--Eclipse中使用Git-让版本管理更简单
查看>>
[转]响应式表格jQuery插件 – Responsive tables
查看>>
8个3D视觉效果的HTML5动画欣赏
查看>>
C#如何在DataGridViewCell中自定义脚本编辑器
查看>>
【linux】crontab定时命令
查看>>
Android UI优化——include、merge 、ViewStub
查看>>
Office WORD如何取消开始工作右侧栏
查看>>
Android Jni调用浅述
查看>>
CodeCombat森林关卡Python代码
查看>>
第一个应用程序HelloWorld
查看>>
(二)Spring Boot 起步入门(翻译自Spring Boot官方教程文档)1.5.9.RELEASE
查看>>
Android Annotation扫盲笔记
查看>>
React 整洁代码最佳实践
查看>>
聊聊架构设计做些什么来谈如何成为架构师
查看>>
Java并发编程73道面试题及答案
查看>>