网络流24题12

状压 + spfa

https://loj.ac/problem/6009

#include <iostream>
#include <cstring>
#include <stdio.h>
#include <queue>
using namespace std;
const int maxn = 109;
struct CC{
    int b1,b2,f1,f2,w;
}cc[maxn];
int n,m;
int dis[1<<20];
int vis[1<<20];
int spfa(int S,int T){
    memset(dis,0x3f,sizeof(dis));
    memset(vis,0,sizeof(vis));
    dis[S] = 0;
    vis[S] = 1;
    queue<int> q;
    q.push(S);
    while(!q.empty()){
        int now = q.front();
        q.pop();
        vis[now] = 0;
        for(int i=1;i<=m;i++){
            if(((~now)&cc[i].b1)==0 && (now&cc[i].b2)==0){
                int v = (now&(~cc[i].f1))|cc[i].f2;
                if(v>=(1<<n) || v<0) continue;
                int w = cc[i].w;
                if(dis[v]>dis[now]+w){
                    dis[v] = dis[now]+w;
                    if(vis[v]==0) {
                        vis[v] = 1;
                        q.push(v);
                    }
                }
            }

        }
    }
    if(dis[T]==0x3f3f3f3f){
        return 0;
    }else{
        return dis[T];
    }
}
int main()
{

    scanf("%d%d",&n,&m);
    for(int i=1;i<=m;i++){
        cc[i] = (CC){0,0,0,0,0};
        int w;
        char str1[30],str2[30];
        scanf("%d%s%s",&w,str1,str2);
        cc[i].w = w;
        for(int j=0;j<n;j++){
            if(str1[j]=='-'){
                cc[i].b2|=(1<<j);
            }else if(str1[j]=='+'){
                cc[i].b1|=(1<<j);
            }
            if(str2[j]=='-'){
                cc[i].f1|=(1<<j);
            }else if(str2[j]=='+'){
                cc[i].f2|=(1<<j);
            }
        }
    }
    int ans = spfa((1<<n)-1,0);
    printf("%d\n",ans);
    return 0;
}

  

转载于:https://www.cnblogs.com/tjucxz/p/8640999.html

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值