J. Grammy and Jewelry
time limit per test
0.5 seconds
memory limit per test
512 megabytes
input
standard input
output
standard output
There is a connected undirected graph with nn vertices and mm edges. Vertices are indexed from 11 to nn. There is infinite jewelry in vertex ii (2≤i≤n2≤i≤n), each valued aiai. Grammy starts at point 11. Going through each edge consumes 11 unit of time. She can pick up a piece of jewelry at vertex ii and put it down at vertex 11. Picking up and putting down a piece of jewelry can be done instantly. Additionally, she can carry at most 1 piece of jewelry at any time. When she put down a piece of jewelry valued xx at vertex 11, she obtains the value of it. Now, for each kk between 11 and TT (inclusive) she wonders what is the maximum value she can get in kk units of time.
Input
The input contains only a single case.
The first line contains three integers nn, mm, and TT (1≤n,m,T≤30001≤n,m,T≤3000).
The second line contains n−1n−1 integers a2,a3,…,ana2,a3,…,an (1≤ai≤30001≤ai≤3000).
The following mm lines describe mm edges. Each line contains 2 integers xixi and yiyi (1≤xi,yi≤n1≤xi,yi≤n), representing a bidirectional edge between vertex xixi and vertex yiyi.
Note that the graph may contain self-loops or duplicated edges.
Output
Print TT integers in one line, the kk-th (1≤k≤T1≤k≤T) of which denoting the maximum value she can get in kk units of time.
Example
input
Copy
5 6 5 2 3 4 5 1 2 4 5 1 4 2 3 1 3 3 3
output
Copy
0 4 4 8 8
解题思路:用Dijkstra求出从1到各个点的最短路径预处理数据,然后题目就变成了,时间T内能获得的最大价值,每个物品可以采很多次,也就是一道完全背包的裸题,直接套上完全背包的模板就可以了
下面附上ac代码
#include <iostream>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <string>
#include <queue>
#include <set>
#include <map>
#include <vector>
#define dbg(a) cout<<#a<<" : "<<a<<endl;
using namespace std;
typedef long long ll;
ll a[1000010];
ll vis[1000010];
ll co[10000010];
ll dp[1000010];
map<ll,ll> ss;
typedef pair<ll,ll> pr;
const ll MAX_N=1e5+5;
const ll INF=1e9+5;
ll n,m;
vector<pr> e[MAX_N];
ll dist[MAX_N];
bool tag[MAX_N];
priority_queue<pr,vector<pr>,greater<pr>> Q;
void Dijkstra(ll S){
for(ll i=1;i<=n;++i)
dist[i]=INF;
dist[S]=0;
Q.push({0,S});
ll u,v,w;
while(!Q.empty()){
u=Q.top().second; Q.pop();
if(tag[u]) continue;
tag[u]=true;
for(ll j=0;j<e[u].size();++j)
{
v=e[u][j].first; w=e[u][j].second;
if(!tag[v]&&dist[v]>dist[u]+w){
dist[v]=dist[u]+w;
Q.push({dist[v],v});
}
}
}
}
int main()
{
std::ios::sync_with_stdio(false);
cin.tie(0),cout.tie(0);
ll T;
cin>>n>>m>>T;
a[1]=0;
for(ll i=2;i<=n;i++)
{
cin>>a[i];
}
ll u,v;
for(ll i=1;i<=m;i++)
{
cin>>u>>v;
e[u].push_back({v,2});
e[v].push_back({u,2});
}
Dijkstra(1);
for(int i = 1; i <= n; ++i) co[i] = dist[i];
for(ll i=1;i<=n;i++)
{
for(ll j=co[i];j<=T;j++)
{
dp[j]=max(dp[j],dp[j-co[i]]+a[i]);
//dbg(dp[j]);
}
}
for(ll i=1;i<=T;i++)
{
cout<<dp[i]<<" ";
}
//cout<<endl;
return 0;
}