博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Project Euler 345: Matrix Sum
阅读量:6572 次
发布时间:2019-06-24

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

思路:

将问题转化成最小费用流

代码:

#pragma GCC optimize(2)#pragma GCC optimize(3)#pragma GCC optimize(4)#include
using namespace std;#define y1 y11#define fi first#define se second#define pi acos(-1.0)#define LL long long//#define mp make_pair#define pb push_back#define ls rt<<1, l, m#define rs rt<<1|1, m+1, r#define ULL unsigned LL#define pll pair
#define pli pair
#define pii pair
#define piii pair
#define pdd pair
#define mem(a, b) memset(a, b, sizeof(a))#define debug(x) cerr << #x << " = " << x << "\n";#define fio ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);//headconst int N = 200 + 10;const int INF = 0x3f3f3f3f;int a[N][N];struct edge { int to, cap, cost, rev;};int V;vector
g[N];int h[N], dis[N], prevv[N], preve[N];void add_edge(int u, int v, int cap, int cost) { g[u].pb({v, cap, cost, g[v].size()}); g[v].pb({u, 0, -cost, g[u].size()-1});}int min_cost_flow(int s, int t, int f) { int res = 0; mem(h, 0); while(f > 0) { priority_queue
, greater
> q; mem(dis, 0x3f); dis[s] = 0; q.push({ 0, s}); while(!q.empty()) { pii p = q.top(); q.pop(); int v = p.se; if(dis[v] < p.fi) continue; for (int i = 0; i < g[v].size(); ++i) { edge &e = g[v][i]; if(e.cap > 0 && dis[e.to] > dis[v] + e.cost + h[v] - h[e.to]) { dis[e.to] = dis[v] + e.cost + h[v] - h[e.to]; prevv[e.to] = v; preve[e.to] = i; q.push({dis[e.to], e.to}); } } } if(dis[t] == INF) return -1; for (int v = 0; v < V; ++v) h[v] += dis[v]; int d = f; for (int v = t; v != s; v = prevv[v]) d = min(d, g[prevv[v]][preve[v]].cap); f -= d; res += d*h[t]; for (int v = t; v != s; v = prevv[v]) { edge &e = g[prevv[v]][preve[v]]; e.cap -= d; g[v][e.rev].cap += d; } } return res;}int main() { int n = 15; for (int i = 1; i <= n; ++i) { for (int j = 1; j <= n; ++j) { scanf("%d", &a[i][j]); add_edge(i, j+n, 1, 1000-a[i][j]); } } int s = 0, t = n+n+1; V = t+1; for (int i = 1; i <= n; ++i) add_edge(s, i, 1, 0); for (int i = 1; i <= n; ++i) add_edge(i+n, t, 1, 0); cout << 1000*n - min_cost_flow(s, t, n); return 0;}

 

转载于:https://www.cnblogs.com/widsom/p/10456032.html

你可能感兴趣的文章
Silverlight/WPF中DependencyProperty使用陷阱一枚
查看>>
转:一个Sqrt函数引发的血案
查看>>
国际音标遗漏
查看>>
c++ 编译时函数匹配和运行时类型识别
查看>>
Velocity - 单例还是非单例
查看>>
mysql 安装和修改编码(utf8mb4)
查看>>
Ethernet、VLAN、QinQ
查看>>
Cookie (设置与读取、超时设置、指定路径、显示用户上次登录时间)
查看>>
SQL中的ROW_NUMBER()和while循环对每一行执行操作
查看>>
Android Graphviz 安装
查看>>
DevExpreess汉化使用方法及汉化包
查看>>
31. Next Permutation (java 字典序生成下一个排列)
查看>>
同时装有py2 和3,运行scrapy如何区分
查看>>
Android开发之动态加载,运行未安装apk
查看>>
uva-10245-分治
查看>>
前台html基础标签7.6
查看>>
javascript arguments(转)
查看>>
Google maps API开发(一)(转)
查看>>
让MySQL支持InnoDB
查看>>
USACO 1.3.2
查看>>