HYSBZ
来源:互联网 发布:plc与单片机的3000介绍 编辑:程序博客网 时间:2024/06/06 10:39
对偶图最小割 = 左下到右上边的最短路
#include <iostream>#include <cstdio>#include <cstdlib>#include <cstring>#include <string>#include <cmath>#include <set>#include <map>#include <stack>#include <queue>#include <ctype.h>#include <vector>#include <algorithm>#include <sstream>#define PI acos(-1.0)#define in freopen("in.txt", "r", stdin)#define out freopen("out.txt", "w", stdout)using namespace std;typedef long long ll;const int maxn = 2000000 + 7, INF = 0x3f3f3f3f, mod = 1e9 + 7;const int M = maxn * 3 + 700;int n, m;int u, v; struct edge { int v, cost; int next;} edge[M];int head[maxn], ednum; struct node { int v, w; bool operator < (const node b)const { return w > b.w; }} cur, tail; void add(int u, int v, int cost) { edge[ednum].v = v; edge[ednum].cost = cost; edge[ednum].next = head[u]; head[u] = ednum++; edge[ednum].v = u; edge[ednum].cost = cost; edge[ednum].next = head[v]; head[v] = ednum++;} int d[maxn], vis[maxn];void dijkstra(int u, int v) { for(int i = 0; i <= v; ++i) d[i] = INF; memset(vis, 0, sizeof vis); d[u] = 0; priority_queue<node > qu; cur.v = u; cur.w = 0; qu.push(cur); while(!qu.empty()) { cur = qu.top(); qu.pop(); int x = cur.v; if(vis[x]) continue; vis[x] = 1; for(int i = head[x]; i != -1; i = edge[i].next) { if(d[edge[i].v] > edge[i].cost + d[x]) { d[edge[i].v] = edge[i].cost + d[x]; tail.v = edge[i].v; tail.w = d[edge[i].v]; qu.push(tail); } } } printf("%d\n", d[v]);} void init() { int x, y, cost; for(int i = 1; i <= n; ++i) { for(int j = 1; j < m; ++j) { scanf("%d", &cost); x = (i == 1 ? u : (2*(i-1)-1)*(m-1)+j); y = (i == n ? v : 2*(i-1)*(m-1) + j); add(x, y, cost); } } for(int i = 1; i < n; ++i) { for(int j = 1; j <= m; ++j) { scanf("%d", &cost); x = (j == 1 ? v : 2*(i-1)*(m-1)+j-1); y = (j == m ? u : 2*(i-1)*(m-1)+j-1+m); add(x, y, cost); } } for(int i = 1; i < n; ++i) { for(int j = 1; j < m; ++j) { scanf("%d", &cost); x = (2*(i-1)*(m-1)+j); y = ((2*(i-1)+1)*(m-1)+j); add(x, y, cost); } }} int main() { while(scanf("%d %d", &n, &m) != EOF) { memset(head, -1, sizeof head); ednum = 0; // 结点个数 u = 0; // 起点 v = 2*(n-1)*(m-1)+1; // 终点 init(); dijkstra(u, v); } return 0;} /*3 45 6 44 3 17 5 35 6 7 88 7 6 55 5 56 6 6*/
阅读全文
1 0
- HYSBZ
- HYSBZ
- HYSBZ
- HYSBZ
- HYSBZ
- HYSBZ
- HYSBZ
- HYSBZ
- HYSBZ
- HYSBZ
- HYSBZ
- HYSBZ
- HYSBZ
- HYSBZ
- HYSBZ
- HYSBZ
- HYSBZ
- HYSBZ
- android 读取res/values/arrays中的数据
- LeetCode 62. Unique Paths (Medium)
- leetcode20. Valid Parentheses
- Spring学习笔记
- LeetCode--LRU Cache
- HYSBZ
- C 语言指针怎么理解?
- HTML5上传图片前实现预览(超简单)
- java基础[10]-函数式编程及Lambda
- MySQL 学习教程(三)【整理】-列类型( 数值型/ 日期时间型/ 字符串型)
- muduo库源码学习(base)ThreadLocal
- 神经网络简单实现和公式推导
- DirectX11笔记(三)--Direct3D初始化2
- 在ASP.NET MVC(Razor)中使用avalon2.x,@字符冲突的不靠谱解决方案