Los Valley 1455 Buy with
Address: http://www.luogu.org/problem/show?pid=1455
Title Description
Tomorrow is the mother's Day, the computer group of children in the busy schoolwork, thinking about what gifts to express their hearts? I heard that on a website has sold clouds, the children decided to go to see this magical goods, the store has n clouds, the cloud has been the boss number 1,2,3,......,n, and every cloud has a value, but the shop owner is a very strange person, he will tell you some clouds to match up to buy before selling, That is to buy a cloud with this cloud with the cloud have to buy, computer group you think this gift is too novel, but your money is limited, so you must want to use the existing money to buy as much as possible the value of the cloud.
Input/output format
Input format:
Line 1th N,m,w, representing the n cloud, M collocation and the number of your existing money
Line 2nd to n+1, each line Ci,di represents the price and value of I cloud
N+2 to N+1+m, each line ui,vi to buy the UI must buy VI, similarly, if buy VI must buy UI
Output format:
A row that represents the maximum value that can be obtained
Input/Output sample
Input Sample # #:
5 3 10
3 10
3 10
3 10
5 100
10 1
1 3
3 2
4 2
Sample # # of output:
1
Description
30% of the data meet: n<=100
50% of data Meet:n<=1000;m<=100;w<=1000;
100% of the data meet: n<=10000;0<=m<=5000;w<=10000.
Ideas
And check out +01 backpacks.
And the collection constructs each interdependent item package weight and value, 01 backpack can be.
As a result, the check-set can be used to deal with connectivity problems on the graph.
Code
1#include <cstdio>2#include <iostream>3 using namespacestd;4 5 Const intMAXN =10000+Ten;6 7 intP[MAXN],SUMW[MAXN],SUMC[MAXN];8 intW[MAXN],C[MAXN],F[MAXN];9 intn,m,c;Ten One intFindintx) { A returnx==p[x]?x:p[x]=find (P[x]); - } - the intMain () { -scanf"%d%d%d",&n,&m,&C); - for(intI=1; i<=n;i++) -scanf"%d%d", &sumw[i],&sumc[i]), p[i]=i; + intu,v; - for(intI=0; i<m;i++) { +scanf"%d%d",&u,&v); A intX=find (u), y=Find (v); at if(x!=y) { -p[x]=y; -sumw[y]+=Sumw[x]; -sumc[y]+=Sumc[x]; - } - } in intCnt=0; - for(intI=1; i<=n;i++)if(p[i]==i) { to++CNT; +w[cnt]=Sumw[i]; -c[cnt]=Sumc[i]; the } * for(intI=1; i<=cnt;i++) $ for(intj=c;j>=w[i];j--)Panax NotoginsengF[j]=max (f[j],f[j-w[i]]+c[i]); -printf"%d\n", F[c]); the return 0; +}
Valley 1455 with purchase (and check set)