假期训练——CodeForces - 742D Arpa's weak amphitheater and Mehrdad's valuable Hoses DP+dfs
2017-01-24 23:24
375 查看
Just to remind, girls in Arpa's land are really nice.
Mehrdad wants to invite some Hoses to the palace for a dancing party. Each Hos has some weight wi and some beauty bi.
Also each Hos may have some friends. Hoses are divided in some friendship groups. Two Hoses x and y are in the same friendship group if and only if there is a sequence of Hoses a1, a2, ..., ak such
that ai and ai + 1 are friends for each 1 ≤ i < k,
and a1 = x and ak = y.
Arpa allowed to use the amphitheater of palace to Mehrdad for this party. Arpa's amphitheater can hold at most w weight on it.
Mehrdad is so greedy that he wants to invite some Hoses such that sum of their weights is not greater than w and sum of their beauties is as large as possible. Along with that, from each friendship group he can either
invite all Hoses, or no more than one. Otherwise, some Hoses will be hurt. Find for Mehrdad the maximum possible total beauty of Hoses he can invite so that no one gets hurt and the total weight doesn't exceed w.
Input
The first line contains integers n, m and w (1 ≤ n ≤ 1000,
, 1 ≤ w ≤ 1000) —
the number of Hoses, the number of pair of friends and the maximum total weight of those who are invited.
The second line contains n integers w1, w2, ..., wn (1 ≤ wi ≤ 1000) —
the weights of the Hoses.
The third line contains n integers b1, b2, ..., bn (1 ≤ bi ≤ 106) —
the beauties of the Hoses.
The next m lines contain pairs of friends, the i-th of them contains two integers xiand yi (1 ≤ xi, yi ≤ n, xi ≠ yi),
meaning that Hoses xi and yi are friends. Note that friendship is bidirectional. All pairs (xi, yi) are
distinct.
Output
Print the maximum possible total beauty of Hoses Mehrdad can invite so that no one gets hurt and the total weight doesn't exceed w.
Example
Input
Output
Input
Output
Note
In the first sample there are two friendship groups: Hoses {1, 2} and Hos {3}. The best way is to choose all of Hoses in the first group, sum of their weights is equal to 5 and
sum of their beauty is 6.
In the second sample there are two friendship groups: Hoses {1, 2, 3} and Hos {4}. Mehrdad can't invite all the Hoses from the first group because their total weight is 12 > 11,
thus the best way is to choose the first Hos from the first group and the only one from the second group. The total weight will be 8, and the total beauty will be 7.
这道题是0-1背包和dfs的结合,首先用DFS找出在一个group的所有成员,,然后把每个组看为一个人
人的个数就是组的个数,在动态规划时,,第n行,为第n组所有情况的最优解,,(以组为单位或单个成员)
然后其他与0-1背包类似
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <algorithm>
#include <climits>
#include <cstring>
#include <string>
#include <set>
#include <map>
#include <queue>
#include <stack>
#include <vector>
#include <list>
#define rep(i,m,n) for(int i=m;i<=n;i++)
#define rsp(it,s) for(set<int>::iterator it=s.begin();it!=s.end();it++)
const int inf_int = 2e9;
const long long inf_ll = 2e18;
#define inf_add 0x3f3f3f3f
#define mod 1e10+7#define pb push_back
//#define mp make_pair
#define fi first
#define se second
#define pi acos(-1.0)
#define pii pair<int,int>
#define Lson L, mid, rt<<1
#define Rson mid+1, R, rt<<1|1
const int maxn=5e2+10;
using namespace std;
typedef vector<int> vi ;
typedef long long ll;
typedef unsigned long long ull;
inline int read(){int ra,fh;char rx;rx=getchar(),ra=0,fh=1;
while((rx<'0'||rx>'9')&&rx!='-')rx=getchar();if(rx=='-')
fh=-1,rx=getchar();while(rx>='0'&&rx<='9')ra*=10,ra+=rx-48,
rx=getchar();return ra*fh;}
//#pragma comment(linker, "/STACK:102400000,102400000")
ll gcd(ll p,ll q){return q==0?p:gcd(q,p%q);}
ll qpow(ll p,ll q){ll f=1;while(q){if(q&1)f=f*p;p=p*p;q>>=1;}return f;}
int n,m,k;
ll w[2005];
ll b[2005];
ll dp[2][100005];
vector<int> aa[1005];
set<int> xx;
set<int>::iterator it;
vector<int> mp[1005];
int f[2005];
void dfs(int sx);
int ct=1;
int x,y;
int main()
{
ios::sync_with_stdio(false);
cin >> n>>m>>k;
rep(i,1,n)
{
cin >> w[i];
}
rep(i,1,n)
cin >> b[i];
rep(i,1,m)
{
cin>>x>>y;
mp[x].push_back(y);
mp[y].push_back(x);
}
for(int i=1;i<=n;i++)
{
if(f[i]==0)
{
dfs(i);
ct ++;
}
}
/*for(int i=1;i<=n;i++)
cout << f[i]<<" ";
cout <<endl;*/
for(int kk = 1;kk<=ct;kk++)
{
ll sumw = 0;
ll sumb = 0;
for(int i=1;i<=n;i++)
{
if(f[i]==kk)
{
aa[kk].push_back(i);
sumw += w[i];
sumb += b[i];
}
}
w[++n]=sumw;
b
=sumb;
aa[kk].push_back(n);
}
/*for(int i=1;i<=ct;i++)
{
for(int j=0;j<aa[i].size();j++)
{
cout << aa[i][j]<<" ";
}
cout << endl;
}*/
for(int i=1;i<=ct;i++)
{
for(int j=1;j<=k;j++)
{
dp[i%2][j] =dp[(i-1)%2][j];
for(int ii=0;ii<aa[i].size();ii++)
{
if(j-w[aa[i][ii]]>=0)
{
if(dp[i%2][j]<dp[(i-1)%2][j-w[aa[i][ii]]]+b[aa[i][ii]])
{
dp[i%2][j] =dp[(i-1)%2][j-w[aa[i][ii]]]+b[aa[i][ii]];
}
}
}
}
}
cout<<dp[ct%2][k]<<endl;
return 0;
}
void dfs(int sx)
{
f[sx] = ct;
for(int i=0;i<mp[sx].size();i++)
{
int nex = mp[sx][i];
if(f[nex]==0)
{
dfs(nex);
}
}
}
Mehrdad wants to invite some Hoses to the palace for a dancing party. Each Hos has some weight wi and some beauty bi.
Also each Hos may have some friends. Hoses are divided in some friendship groups. Two Hoses x and y are in the same friendship group if and only if there is a sequence of Hoses a1, a2, ..., ak such
that ai and ai + 1 are friends for each 1 ≤ i < k,
and a1 = x and ak = y.
Arpa allowed to use the amphitheater of palace to Mehrdad for this party. Arpa's amphitheater can hold at most w weight on it.
Mehrdad is so greedy that he wants to invite some Hoses such that sum of their weights is not greater than w and sum of their beauties is as large as possible. Along with that, from each friendship group he can either
invite all Hoses, or no more than one. Otherwise, some Hoses will be hurt. Find for Mehrdad the maximum possible total beauty of Hoses he can invite so that no one gets hurt and the total weight doesn't exceed w.
Input
The first line contains integers n, m and w (1 ≤ n ≤ 1000,
, 1 ≤ w ≤ 1000) —
the number of Hoses, the number of pair of friends and the maximum total weight of those who are invited.
The second line contains n integers w1, w2, ..., wn (1 ≤ wi ≤ 1000) —
the weights of the Hoses.
The third line contains n integers b1, b2, ..., bn (1 ≤ bi ≤ 106) —
the beauties of the Hoses.
The next m lines contain pairs of friends, the i-th of them contains two integers xiand yi (1 ≤ xi, yi ≤ n, xi ≠ yi),
meaning that Hoses xi and yi are friends. Note that friendship is bidirectional. All pairs (xi, yi) are
distinct.
Output
Print the maximum possible total beauty of Hoses Mehrdad can invite so that no one gets hurt and the total weight doesn't exceed w.
Example
Input
3 1 5 3 2 5 2 4 2 1 2
Output
6
Input
4 2 11
2 4 6 66 4 2 1
1 2
2 3
Output
7
Note
In the first sample there are two friendship groups: Hoses {1, 2} and Hos {3}. The best way is to choose all of Hoses in the first group, sum of their weights is equal to 5 and
sum of their beauty is 6.
In the second sample there are two friendship groups: Hoses {1, 2, 3} and Hos {4}. Mehrdad can't invite all the Hoses from the first group because their total weight is 12 > 11,
thus the best way is to choose the first Hos from the first group and the only one from the second group. The total weight will be 8, and the total beauty will be 7.
这道题是0-1背包和dfs的结合,首先用DFS找出在一个group的所有成员,,然后把每个组看为一个人
人的个数就是组的个数,在动态规划时,,第n行,为第n组所有情况的最优解,,(以组为单位或单个成员)
然后其他与0-1背包类似
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <algorithm>
#include <climits>
#include <cstring>
#include <string>
#include <set>
#include <map>
#include <queue>
#include <stack>
#include <vector>
#include <list>
#define rep(i,m,n) for(int i=m;i<=n;i++)
#define rsp(it,s) for(set<int>::iterator it=s.begin();it!=s.end();it++)
const int inf_int = 2e9;
const long long inf_ll = 2e18;
#define inf_add 0x3f3f3f3f
#define mod 1e10+7#define pb push_back
//#define mp make_pair
#define fi first
#define se second
#define pi acos(-1.0)
#define pii pair<int,int>
#define Lson L, mid, rt<<1
#define Rson mid+1, R, rt<<1|1
const int maxn=5e2+10;
using namespace std;
typedef vector<int> vi ;
typedef long long ll;
typedef unsigned long long ull;
inline int read(){int ra,fh;char rx;rx=getchar(),ra=0,fh=1;
while((rx<'0'||rx>'9')&&rx!='-')rx=getchar();if(rx=='-')
fh=-1,rx=getchar();while(rx>='0'&&rx<='9')ra*=10,ra+=rx-48,
rx=getchar();return ra*fh;}
//#pragma comment(linker, "/STACK:102400000,102400000")
ll gcd(ll p,ll q){return q==0?p:gcd(q,p%q);}
ll qpow(ll p,ll q){ll f=1;while(q){if(q&1)f=f*p;p=p*p;q>>=1;}return f;}
int n,m,k;
ll w[2005];
ll b[2005];
ll dp[2][100005];
vector<int> aa[1005];
set<int> xx;
set<int>::iterator it;
vector<int> mp[1005];
int f[2005];
void dfs(int sx);
int ct=1;
int x,y;
int main()
{
ios::sync_with_stdio(false);
cin >> n>>m>>k;
rep(i,1,n)
{
cin >> w[i];
}
rep(i,1,n)
cin >> b[i];
rep(i,1,m)
{
cin>>x>>y;
mp[x].push_back(y);
mp[y].push_back(x);
}
for(int i=1;i<=n;i++)
{
if(f[i]==0)
{
dfs(i);
ct ++;
}
}
/*for(int i=1;i<=n;i++)
cout << f[i]<<" ";
cout <<endl;*/
for(int kk = 1;kk<=ct;kk++)
{
ll sumw = 0;
ll sumb = 0;
for(int i=1;i<=n;i++)
{
if(f[i]==kk)
{
aa[kk].push_back(i);
sumw += w[i];
sumb += b[i];
}
}
w[++n]=sumw;
b
=sumb;
aa[kk].push_back(n);
}
/*for(int i=1;i<=ct;i++)
{
for(int j=0;j<aa[i].size();j++)
{
cout << aa[i][j]<<" ";
}
cout << endl;
}*/
for(int i=1;i<=ct;i++)
{
for(int j=1;j<=k;j++)
{
dp[i%2][j] =dp[(i-1)%2][j];
for(int ii=0;ii<aa[i].size();ii++)
{
if(j-w[aa[i][ii]]>=0)
{
if(dp[i%2][j]<dp[(i-1)%2][j-w[aa[i][ii]]]+b[aa[i][ii]])
{
dp[i%2][j] =dp[(i-1)%2][j-w[aa[i][ii]]]+b[aa[i][ii]];
}
}
}
}
}
cout<<dp[ct%2][k]<<endl;
return 0;
}
void dfs(int sx)
{
f[sx] = ct;
for(int i=0;i<mp[sx].size();i++)
{
int nex = mp[sx][i];
if(f[nex]==0)
{
dfs(nex);
}
}
}
相关文章推荐
- Codeforces 474A (五一训练 J)+
- Codeforces-297A 思维训练
- Codeforces 475A (五一训练 L)
- YD 督促训练 判断这年五一几天假期
- CodeForces 705B (训练水题)
- Codeforces 251A+(五一训练 C)+二分法
- Codeforces 204 A (五一训练 A)+想法题
- 2017暑假训练国庆小假期总结
- DP训练 Codeforces 673E Levels and Regions [斜率优化dp][期望]
- SEU寒假训练题解二 D Codeforces 439A
- CodeForces 670A Holidays 火星人的假期
- CSU-ACM2017暑期训练12-KMP G - Next[]树 CodeForces - 432D
- 假期训练——Mahjong HDU - 4431 模拟
- 假期训练——OpenJ_Bailian - 1833 排列 全排序
- 假期训练——Puzzle UVA - 227 模拟+DFS and similar
- SEU寒假训练题解二 G Codeforces 432C
- 假期训练——OpenJ_Bailian - 2950 摘花生 模拟
- 假期训练—— Repeating Decimals UVA - 202 模拟
- 2016 北邮暑期训练3-A题(CodeForces 697B Barnicle)白痴题
- 假期训练——HDU - 5965 扫雷 多情况考虑