3380: [Usaco2004 Open]Cave Cows 1 洞穴里的牛之一
Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 94 Solved: 54
[ Submit][ Status][ Discuss]
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
广搜,只有最多14堆稻草,所以可以用01串表示状态,例如11010就相当于吃了第1,2,4堆稻草
vis[x][y]表示是否可能在x点时稻草食用状态为y
(不知道为什么我这么慢,可能不是正解。。。)
#include<stdio.h>
#include<vector>
#include<queue>
#include<algorithm>
using namespace std;
typedef struct
{
int y;
int val;
}Road;
Road now, temp;
vector<Road> G[105];
queue<Road> q;
int k, vis[105][16888], eat[105];
int Jud(int x)
{
int i, sum = 0;;
for(i=0;i<=k-1;i++)
{
if(x&(1<<i))
sum++;
}
return sum;
}
int main(void)
{
int n, m, i, x, y, ans;
scanf("%d%d%d", &n, &m, &k);
for(i=1;i<=k;i++)
{
scanf("%d", &x);
eat[x] = 1<<(i-1);
}
for(i=1;i<=m;i++)
{
scanf("%d%d%d", &x, &y, &now.val);
now.y = y, G[x].push_back(now);
now.y = x, G[y].push_back(now);
}
now.y = 1, now.val = 0;
vis[now.y][now.val] = 1;
q.push(now);
while(q.empty()==0)
{
now = q.front();
q.pop();
for(i=0;i<G[now.y].size();i++)
{
temp.y = G[now.y][i].y;
temp.val = now.val;
if(G[now.y][i].val>=Jud(now.val) && vis[temp.y][temp.val]==0)
{
vis[temp.y][temp.val] = 1;
q.push(temp);
}
}
now.val ^= eat[now.y];
if(vis[now.y][now.val]==0)
{
vis[now.y][now.val] = 1;
q.push(now);
}
}
ans = 0;
for(i=0;i<(1<<k);i++)
{
if(vis[1][i])
ans = max(ans, Jud(i));
}
printf("%d\n", ans);
return 0;
}
/*
2 1 1
2
1 2 0
*/