4102: [Usaco2015 Open]Bessie
Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 35 Solved: 22
[ Submit][ Status][ Discuss]
Description
For Bessie the cow’s birthday, Farmer John has given her free reign over one of his best fields to eat grass.
The field is covered in N patches of grass (1≤N≤1000), conveniently numbered 1…N, that each have a distinct
quality value. If Bessie eats grass of quality Q, she gains Q units of energy. Each patch is connected to up to 10
neighboring patches via bi-directional paths, and it takes Bessie E units of energy to move between adjacent
patches (1≤E≤1,000,000). Bessie can choose to start grazing in any patch she wishes, and she wants to stop
grazing once she has accumulated a maximum amount of energy.
Unfortunately, Bessie is a picky bovine, and once she eats grass of a certain quality, she’ll never eat grass at or
below that quality level again! She is still happy to walk through patches without eating their grass; in fact, she
might find it beneficial to walk through a patch of high-quality grass without eating it, only to return later for a tasty snack.
Please help determine the maximum amount of energy Bessie can accumulate.
为了庆祝贝茜的生日,FJ给她吃草的自由. N块草地,标号1到N(1<=N<=1000),草地有营养价值.当贝茜走到这个草地,可以获得等于这块草地的营养价值的能量. 每块草地最多有10条双向边,每走一条边,贝茜花费E的能量. 贝茜拿可以从任何地方出发,当她不能获得更多的能量的时候她就会停止. 然而因为贝茜挑食,她每次不会吃低于或等于上次的营养价值的草地,所以她获得的能量是严格上升的,当然她可以选择只是经过一块草地而不吃草.
算出贝茜能够获得的最大能量.
Input
The first line of input contains N and E. Each of the remaining N lines describe a patch of grass.
They contain two integers Q and D giving the quality of the patch (in the range 1…1,000,000) and
its number of neighbors. The remaining D numbers on the line specify the neighbors.
第一排两个数 N和E
剩下N排,每排包含Q,D两个数字,Q代表这个草地的营养价值,D代表这个点的边数,剩下D个数表示于这块草地相邻的草地.
Output
Please output the maximum amount of energy Bessie can accumulate.
一个整数
最大权值
Sample Input
5 2
4 1 2
1 3 1 3 4
6 2 2 5
5 2 2 5
2 2 3 4
4 1 2
1 3 1 3 4
6 2 2 5
5 2 2 5
2 2 3 4
Sample Output
7
Bessie starts at patch 4 gaining 5 units of energy from the grass there. She then takes the path to patch 5 losing 2 units of energy during her travel. She refuses to eat the lower quality grass at patch 5 and travels to patch 3 again losing 2 units of energy. Finally she eats the grass at patch 3 gaining 6 units of energy for a total of 7 energy.
Note tha the sample case above is different from test case 1 when you submit.
Bessie starts at patch 4 gaining 5 units of energy from the grass there. She then takes the path to patch 5 losing 2 units of energy during her travel. She refuses to eat the lower quality grass at patch 5 and travels to patch 3 again losing 2 units of energy. Finally she eats the grass at patch 3 gaining 6 units of energy for a total of 7 energy.
Note tha the sample case above is different from test case 1 when you submit.
HINT
求译文!请发至lydsy2012@163.com
Source
首先以每个点为起点跑一次BFS,求出任意两点间的最短距离dis[i][j]。再将所有点按权值从小到大排序,按这个顺序DP。状态转移方程为:f[j]=max{f[k]+a[j]-dis[k][j]},(1≤k≤j-1)。最后在f数组中求最大值,即为答案。
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cmath>
#include<cstring>
#include<queue>
#define F(i,j,n) for(int i=j;i<=n;i++)
#define D(i,j,n) for(int i=j;i>=n;i--)
#define LL long long
#define MAXN 1005
#define MAXM 10005
#define pa pair<int,int>
using namespace std;
struct edge_type
{
int next,to;
}e[MAXM];
int n,p,w,d,x,cnt=0,ans=0,head[MAXN],dis[MAXN][MAXN],f[MAXN];
pa a[MAXN];
bool vst[MAXN];
queue<int> q;
int read()
{
int ret=0;
char ch=getchar();
while (ch<'0'||ch>'9') ch=getchar();
while (ch>='0'&&ch<='9')
{
ret=ret*10+ch-'0';
ch=getchar();
}
return ret;
}
void add_edge(int u,int v)
{
e[++cnt].to=v;
e[cnt].next=head[u];
head[u]=cnt;
}
void bfs(int k)
{
memset(vst,false,sizeof(vst));
dis[k][k]=0;
vst[k]=true;
q.push(k);
while (!q.empty())
{
int tmp=q.front();
q.pop();
for(int i=head[tmp];i;i=e[i].next)
{
int to=e[i].to;
if (!vst[to])
{
dis[k][to]=dis[k][tmp]+1;
q.push(to);
vst[to]=true;
}
}
}
}
int main()
{
memset(head,0,sizeof(head));
memset(dis,-1,sizeof(dis));
n=read(); p=read();
F(i,1,n)
{
w=read(); d=read();
a[i]=make_pair(w,i);
F(j,1,d)
{
x=read();
add_edge(i,x);
}
}
F(i,1,n) bfs(i);
sort(a+1,a+n+1);
f[1]=a[1].first;
F(i,2,n)
{
f[i]=a[i].first;
F(j,1,n-1) if (a[j].first<a[i].first&&dis[a[j].second][a[i].second]>=0)
f[i]=max(f[i],f[j]+a[i].first-p*dis[a[j].second][a[i].second]);
}
F(i,1,n) ans=max(ans,f[i]);
printf("%d\n",ans);
}