博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
hdu 3870 Catch the Theves 最小割-》求最短路
阅读量:4344 次
发布时间:2019-06-07

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

题意:

给你一个n*n的矩阵,然后(0,0)为源点,(n - 1,n  - 1)为汇点。点(i,j)可以到达(i + 1,j) (i,j + 1) 花费为a[i][j]。求从(0,0)到(n - 1,n - 1)的最小割。

思路:

如果按照正常建图,Dinic求最大流会超时,这里要充分利用平面图的性质。然后将其转化为对偶图,然后将求最大流最小割转化为求该图的对偶图最短路问题。

详细请参考:

#include 
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#define CL(arr, val) memset(arr, val, sizeof(arr))#define lc l,m,rt<<1#define rc m + 1,r,rt<<1|1#define pi acos(-1.0)#define L(x) (x) << 1#define R(x) (x) << 1 | 1#define MID(l, r) (l + r) >> 1#define Min(x, y) (x) < (y) ? (x) : (y)#define Max(x, y) (x) < (y) ? (y) : (x)#define E(x) (1 << (x))#define iabs(x) (x) < 0 ? -(x) : (x)#define OUT(x) printf("%I64d\n", x)#define lowbit(x) (x)&(-x)#define Read() freopen("din.txt", "r", stdin)#define Write() freopen("dout.txt", "w", stdout)#define M 1000007#define N 407#define ll __int64using namespace std;const double eps = 1e-10;const int inf = 0x7f7f7f7f;struct node{ int v; int w; int next;}g[M];int head[N*N],ct;struct pnd{ int u; int ds; pnd() {} pnd(int x,int y) : u(x),ds(y){} bool operator < (const pnd &a) const { return ds > a.ds; }};int a[N][N];int n;int dis[N*N];bool vt[N*N];void add(int u,int v,int w){ g[ct].v = v; g[ct].w = w; g[ct].next = head[u]; head[u] = ct++; g[ct].v = u; g[ct].w = w; g[ct].next = head[v]; head[v] = ct++;}//int spfa(int s,int e)//{// int i;// for (i = 0; i <= e; ++i)// {// dis[i] = inf;// vt[i] = false;// }// dis[s] = 0;// vt[s] = true;// queue
Q;// Q.push(s);// while (!Q.empty())// {// int u = Q.front(); Q.pop();// for (i = head[u]; i != -1; i = g[i].next)// {// int v = g[i].v;// if (dis[v] > dis[u] + g[i].w)// {// dis[v] = dis[u] + g[i].w;// if (!vt[v])// {// vt[v] = true;// Q.push(v);// }// }// }// vt[u] = false;// }//// return dis[e];//}int Dijkstra(int s,int e){ priority_queue
pQ; for (int i = 0; i <= e; ++i) { dis[i] = inf; vt[i] = false; } dis[s] = 0; pQ.push(pnd(s,dis[s])); while (!pQ.empty()) { pnd ui = pQ.top(); pQ.pop(); if (vt[ui.u]) continue; vt[ui.u] = true; for (int i = head[ui.u]; i != -1; i = g[i].next) { int v = g[i].v; if (dis[v] > ui.ds + g[i].w) { dis[v] = ui.ds + g[i].w; pQ.push(pnd(v,dis[v])); } } } return dis[e];}int main(){// Read(); int T,i,j; scanf("%d",&T); while (T--) { scanf("%d",&n); for (i = 0; i < n; ++i) for (j = 0; j < n; ++j) scanf("%d",&a[i][j]); CL(head,-1); ct = 0; int s = (n - 1)*(n - 1); int e = s + 1; int x; for (i = 0; i < n; ++i) { for (j = 0; j < n; ++j) { if (i == 0 && j != n - 1) { x = j; add(s,x,a[i][j]); } if (j == n - 1 && i != n - 1) { x = i*(n - 1) + (n - 2); add(s,x,a[i][j]); } if (j == 0 && i != n - 1) { x = i*(n - 1); add(x,e,a[i][j]); } if (i == n - 1 && j != n - 1) { x = (n - 2)*(n - 1) + j; add(x,e,a[i][j]); } if (i != n - 1 && j != n - 1) { if (i) { x = (i - 1)*(n - 1) + j; add((i*(n - 1) + j),x,a[i][j]); } if (j) { x = i*(n - 1) + j - 1; add((i*(n - 1) + j),x,a[i][j]); } } } } //优先队列优化的Dijkstra比spfa快多了。而且spfa时如果s,e链接边的只能是单向,否则会超时// printf("<
    View Code

     

     

    转载于:https://www.cnblogs.com/E-star/archive/2013/06/05/3118451.html

    你可能感兴趣的文章
    小D课堂 - 零基础入门SpringBoot2.X到实战_第1节零基础快速入门SpringBoot2.0_5、SpringBoot2.x的依赖默认Maven版本...
    查看>>
    阶段3 3.SpringMVC·_07.SSM整合案例_08.ssm整合之Spring整合MyBatis框架
    查看>>
    小D课堂 - 零基础入门SpringBoot2.X到实战_第2节 SpringBoot接口Http协议开发实战_9、SpringBoot基础HTTP其他提交方法请求实战...
    查看>>
    小D课堂 - 零基础入门SpringBoot2.X到实战_第2节 SpringBoot接口Http协议开发实战_12、SpringBoot2.x文件上传实战...
    查看>>
    小D课堂 - 零基础入门SpringBoot2.X到实战_第4节 Springboot2.0单元测试进阶实战和自定义异常处理_19、SpringBoot个性化启动banner设置debug日志...
    查看>>
    小D课堂 - 零基础入门SpringBoot2.X到实战_第4节 Springboot2.0单元测试进阶实战和自定义异常处理_20、SpringBoot2.x配置全局异常实战...
    查看>>
    小D课堂 - 零基础入门SpringBoot2.X到实战_第5节 SpringBoot部署war项目到tomcat9和启动原理讲解_23、SpringBoot2.x启动原理概述...
    查看>>
    小D课堂 - 零基础入门SpringBoot2.X到实战_第4节 Springboot2.0单元测试进阶实战和自定义异常处理_21、SpringBoot2.x配置全局异常返回自定义页面...
    查看>>
    小D课堂 - 零基础入门SpringBoot2.X到实战_第8节 数据库操作之整合Mybaties和事务讲解_32..SpringBoot2.x持久化数据方式介绍...
    查看>>
    小D课堂 - 零基础入门SpringBoot2.X到实战_第8节 数据库操作之整合Mybaties和事务讲解_34、SpringBoot整合Mybatis实操和打印SQL语句...
    查看>>
    小D课堂 - 零基础入门SpringBoot2.X到实战_第8节 数据库操作之整合Mybaties和事务讲解_35、事务介绍和常见的隔离级别,传播行为...
    查看>>
    小D课堂 - 零基础入门SpringBoot2.X到实战_第9节 SpringBoot2.x整合Redis实战_40、Redis工具类封装讲解和实战...
    查看>>
    小D课堂 - 零基础入门SpringBoot2.X到实战_第9节 SpringBoot2.x整合Redis实战_37、分布式缓存Redis介绍...
    查看>>
    小D课堂 - 零基础入门SpringBoot2.X到实战_第10节 SpringBoot整合定时任务和异步任务处理_42、SpringBoot常用定时任务配置实战...
    查看>>
    小D课堂 - 零基础入门SpringBoot2.X到实战_第9节 SpringBoot2.x整合Redis实战_39、SpringBoot2.x整合redis实战讲解...
    查看>>
    小D课堂 - 零基础入门SpringBoot2.X到实战_第14节 高级篇幅之SpringBoot多环境配置_59、SpringBoot多环境配置介绍和项目实战...
    查看>>
    小D课堂 - 零基础入门SpringBoot2.X到实战_第10节 SpringBoot整合定时任务和异步任务处理_41、SpringBoot定时任务schedule讲解...
    查看>>
    小D课堂 - 零基础入门SpringBoot2.X到实战_第10节 SpringBoot整合定时任务和异步任务处理_43、SpringBoot2.x异步任务实战(核心知识)...
    查看>>
    小D课堂 - 新版本微服务springcloud+Docker教程_1_01课程简介
    查看>>
    小D课堂 - 零基础入门SpringBoot2.X到实战_第11节 Logback日志框架介绍和SpringBoot整合实战_45、SpringBoot2.x日志讲解和Logback配置实战...
    查看>>