zoukankan      html  css  js  c++  java
  • [BZOJ3380] [USACO2004 Open]Cave Cows 1 洞穴里的牛之一

    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('
    ');}
    
    #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;
    }
    
  • 相关阅读:
    python之路_爬虫之selenium模块
    python之路_爬虫之requests模块补充
    扩展中国剩余定理讲解
    扩展中国剩余定理讲解
    bzoj3225 [Sdoi2008]立方体覆盖——扫描线
    差分约束讲解
    CF917C Pollywog —— 状压DP + 矩乘优化
    斜率优化讲解
    AC自动机讲解
    BZOJ2870—最长道路tree
  • 原文地址:https://www.cnblogs.com/hbyer/p/10547912.html
Copyright © 2011-2022 走看看