bzoj刷题合集

正式开始从2021年12.04年南京站结束
由于本人知识点学习较乱使得在比赛中不能熟练运用为了提高这种能力进行一个bzoj经典题的刷与练习
1.1001 最小割转换为最短路最小
使用对偶图将图变成如下 转换为最短路 使得每个路为一个割的子集
在这里插入图片描述

#include<iostream>
#include<cstring>
#include<queue>
#include<utility>
#define x first
#define y second

using namespace std;

const int N = 1100 * 1100 * 2,M = N * 4;
typedef pair<int,int> PII;

int head[N],to[M],last[M],w[M],cnt;
void add(int a,int b,int c){
    to[++cnt] = b;
    last[cnt] = head[a];
    w[cnt] = c;
    head[a] = cnt;
    to[++cnt] = a;
    last[cnt] = head[b];
    w[cnt] = c;
    head[b] = cnt;
}
int S,T;
int a[1100][1100],b[1100][1100],c[1100][1100];
int dist[N],flag[N];
int dij(){
    memset(dist,0x3f,sizeof dist);
    memset(flag,0,sizeof flag);
    priority_queue<PII,vector<PII>,greater<PII> > q;
    q.push({0,S});
    dist[S] = 0;
    while(q.size()){
        PII p = q.top();
        q.pop();
        if(flag[p.y]) continue;
        flag[p.y] = 1;
        for(int i = head[p.y]; i != -1; i = last[i]){
            int j = to[i];
            if(dist[j] > dist[p.y] + w[i]){
                dist[j] = dist[p.y] + w[i];
                q.push({dist[j],j});
            }
        }
    }
    return dist[T];
}

int main(){
    int n,m;
    cin >> n >> m;
    int tot = 0,cnt = 1;
    memset(head,-1,sizeof head);
    for(int i = 1; i <= n; i++){
        for(int j = 1; j < m; j++){
            scanf("%d",&a[i][j]);
        }
    }

    for(int i = 1; i <= n - 1; i++){
        for(int j = 1; j <= m; j++){
            scanf("%d",&b[i][j]);
        }
    }

    for(int i = 1; i <= n - 1; i++){
        for(int j = 1; j <= m - 1; j++){
            scanf("%d",&c[i][j]);
        }
    }

    S = 0,T = n * m * 2 + 1;

    for(int i = 1; i <= (n - 1); i++){
        for(int j = 1; j <= (m - 1); j++){
            ++tot;
            if(i == 1) add(S,tot,a[i][j]);
            if(j == m - 1) add(S,tot,b[i][j + 1]);
            if(j == 1) add(tot + n * m,T,b[i][j]);
            if(i == n - 1) add(tot + n * m,T,a[i + 1][j]);
            add(tot,tot + n * m,c[i][j]);
            if(j != m - 1){
                add(tot,tot + 1 + n * m,b[i][j + 1]);
            }
            if(i != n - 1){
                add(tot + n * m,tot + m - 1,a[i + 1][j]);
            }
        }
    }

    int d = dij();
    if(d == 0x3f3f3f3f){
        cout << 1 << endl;
    }else  cout << d << endl;





    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值