ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

【cf】J. Grammy and Jewelry(Dijkstra+完全背包)

2021-05-02 11:33:16  阅读:190  来源: 互联网

标签:11 Jewelry ll vertex cf Dijkstra she include jewelry


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;
}

 

标签:11,Jewelry,ll,vertex,cf,Dijkstra,she,include,jewelry
来源: https://blog.csdn.net/weixin_45720782/article/details/116351711

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有