[BZOJ3380] [USACO2004 Open]Cave Cows 1 洞穴里的牛之一

博客围绕奶牛洞窟探险吃干草问题展开。洞窟有N个洞室、M条双向通道,K个洞室有干草。贝茜从洞窟1出发,体重指数为0,每条通道有宽度阈值,超阈值会被卡住。问题是求贝茜最多能吃多少捆干草,还给出了输入输出格式及求解思路。

Description

​ 很少人知道其实奶牛非常喜欢到洞穴里面去探险。

​ 洞窟里有N(1≤N≤100)个洞室,由M(1≤M≤1000)条双向通道连接着它们.每对洞室间

至多只有一条双向通道.有K(1≤K≤14)个洞室,里面放有1捆干草.牛吃1捆干草,体重指数就会增加1.

​ 贪吃的贝茜要到洞窟里面探险.她希望能吃尽量多的干草,但每条通道有一个宽度阈值,如果体重指数超过相应的阈值,贝茜就会被卡祝她从洞窟1出发,体重指数为0.在洞里溜达一圈后,她要返回洞窟1. 那她最多能吃多少捆干草呢?注意,贝茜经过一个洞室,不一定非要吃掉里面的干草.

Input

​ 第1行输入N,M,K,之后K行每行一个整数,表示在这个洞室放有一捆干草;接下来M行每行三个整数,表示一条双向通道的起点终点和宽度阈值.

Output

​ 最多能吃掉的干草数.

Sample Input

6 7 5
1
2
3
4
5
1 2 3
3 6 2
6 2 10
2 4 1
5 1 1
4 5 1
1 6 1

Sample Output

4

Solution

\(f[x][s]\)表示在点\(x\)吃了状态\(s\)的所有干草合不合法。

那么直接暴力转移就好了。

#include<bits/stdc++.h>
using namespace std;
 
void read(int &x) {
    x=0;int f=1;char ch=getchar();
    for(;!isdigit(ch);ch=getchar()) if(ch=='-') f=-f;
    for(;isdigit(ch);ch=getchar()) x=x*10+ch-'0';x*=f;
}
 
void print(int x) {
    if(x<0) putchar('-'),x=-x;
    if(!x) return ;print(x/10),putchar(x%10+48);
}
void write(int x) {if(!x) putchar('0');else print(x);putchar('\n');}

#define lf double
#define ll long long 

const int maxn = 101;
const int inf = 1e9;
const lf eps = 1e-8;

int f[101][(1<<14)+10],n,m,k,id[101];
int head[maxn],tot,cnt[(1<<14)+10];

struct edge{int to,nxt,w;}e[maxn<<5];

void add(int u,int v,int w) {e[++tot]=(edge){v,head[u],w},head[u]=tot;}
void ins(int u,int v,int w) {add(u,v,w),add(v,u,w);}

int main() {
    read(n),read(m),read(k);
    for(int i=1,x;i<=k;i++) read(x),id[x]=i;
    for(int i=1,x,y,z;i<=m;i++) read(x),read(y),read(z),ins(x,y,z);
    for(int i=1;i<1<<k;i++) cnt[i]=__builtin_popcount(i);
    queue<pair<int,int >  > q;
    q.push(make_pair(1,0));f[1][0]=1;
    while(!q.empty()) {
        int now=q.front().first,s=q.front().second;q.pop();
        for(int i=head[now];i;i=e[i].nxt) {
            if(!f[now][s]) continue;
            if(e[i].w<cnt[s]) continue;
            if(!f[e[i].to][s]) f[e[i].to][s]=1,q.push(make_pair(e[i].to,s));
            if(id[now])
                if(!(s>>(id[now]-1)&1))
                    if(e[i].w>cnt[s]) 
                        if(!f[e[i].to][s|(1<<(id[now]-1))])
                            f[e[i].to][s|(1<<(id[now]-1))]=1,q.push(make_pair(e[i].to,s|(1<<(id[now]-1))));
        }
    }
    int ans=0,t=id[1]?1<<(id[1]-1):0;
    for(int i=0;i<1<<k;i++)
        if(f[1][i]) ans=max(ans,cnt[i|t]);
    write(ans);
    return 0;
}

转载于:https://www.cnblogs.com/hbyer/p/10547912.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值