题目描述
There is a connected undirected graph with
n
n
n vertices and m edges. Vertices are indexed from
1
1
1 to
n
n
n. There is infinite jewelry in vertex
i
i
i
(
2
≤
i
≤
n
)
(2≤i≤n)
(2≤i≤n), each valued
a
i
a_i
ai. Grammy starts at point
1
1
1. Going through each edge consumes
1
1
1 unit of time. She can pick up a piece of jewelry at vertex
i
i
i and put it down at vertex
1
1
1. Picking up and putting down a piece of jewelry can be done instantly. Additionally, she can carry at most
1
1
1 piece of jewelry at any time. When she put down a piece of jewelry valued
x
x
x at vertex
1
1
1, she obtains the value of it. Now, for each
k
k
k between
1
1
1 and
T
T
T (inclusive) she wonders what is the maximum value she can get in
k
k
k units of time.
输入
The input contains only a single case.
The first line contains three integers n , m n, m n,m, and T T T ( 1 ≤ n , m , T ≤ 3000 ) (1≤n,m,T≤3000) (1≤n,m,T≤3000).
The second line contains n − 1 n−1 n−1 integers a 2 , a 3 , … , a n ( 1 ≤ a i ≤ 3000 ) a_2,a_3,…,a_n (1≤a_i≤3000) a2,a3,…,an(1≤ai≤3000).
The following m lines describe m m m edges. Each line contains 2 2 2 integers x i x_i xi and y i ( 1 ≤ x i , y i ≤ n ) y_i (1≤x_i,y_i≤n) yi(1≤xi,yi≤n), representing a bidirectional edge between vertex x i x_i xi and vertex y i y_i yi.
Note that the graph may contain self-loops or duplicated edges.
输出
Print T integers in one line, the
k
k
k-th
(
1
≤
k
≤
T
)
(1≤k≤T)
(1≤k≤T) of which denoting the maximum value she can get in
k
k
k units of time.
样例输入
5 6 5
2 3 4 5
1 2
4 5
1 4
2 3
1 3
3 3
样例输出
0 4 4 8 8
大意:
一个无向图,从1出发,经过一个点消耗1单位时间,将一节点带回起点,所得价值为该点点权。每个点可以带回无限次。
求在1~T所有时间点中,能够得到的最大价值。
思考:
到达一个点所用的最短时间为最短路径。得到一个点的点权所用最短时间为2*最短路径
。
每个点的点权可以获得无限次。
所以先预处理出:在每一个时间段(2*最短路径),能够获得的最大价值。
后面如果需要这个时间段,可以一直加这个时间段的最大价值。
要求时间T里,所能得到的最大价值,其实就是总时间不超过T,所能得到的最大价值。
前面预处理出了每个时间段能得到的最大价值,每个时间段就看作一个物品,体积为时间。可以有无限个。
于是就转化成了完全背包求最大价值问题。
Code:
#define mem(a, b) memset(a, b, sizeof a)
#include <algorithm>
#include <iostream>
#include <cstring>
#include <queue>
using namespace std;
int h[N],w[N],e[N],ne[N],idx;
bool ff[N];
int dist[N], va[N];
int f[N];
void add(int x,int y){
e[idx]=y,ne[idx]=h[x],h[x]=idx++;
}
void dij()
{
priority_queue<PII,vector<PII>,greater<PII> > que;
que.push({0,1});
dist[1]=0;
ff[1]=1;
while(que.size())
{
int x=que.top().second;
que.pop();
ff[x]=1;
for(int i=h[x];i!=-1;i=ne[i])
{
int tx=e[i];
if(ff[tx]) continue;
if(dist[tx]>dist[x]+1){
dist[tx]=dist[x]+1;
que.push({dist[tx],tx});
}
}
}
}
int main(){
cin>>n>>m>>T;
for(int i=2;i<=n;i++) cin>>va[i];
mem(h,-1);
mem(dist,0x3f);
while(m--)
{
int x,y;cin>>x>>y;
add(x,y);
add(y,x);
}
dij();
for(int i=1;i<=n;i++){ //预处理出每个时间段能够得到的最大价值
w[2*dist[i]]=max(w[2*dist[i]],va[i]);
}
for(int i=1;i<=n;i++) //完全背包
{
for(int j=i;j<=T;j++)
{
f[j]=max(f[j],f[j-i]+w[i]);
}
}
for(int i=1;i<=T;i++) cout<<f[i]<<" ";
return 0;
}
很好的结合。