Codeforces 284E Coin Troubles【思维+拓扑排序+完全背包】好题!
来源:互联网 发布:哈工大电力大数据 编辑:程序博客网 时间:2024/05/17 08:44
题目大意:
有n种硬币,现在我们需要凑出t价值的硬币,我们每种硬币都可以任取数量,不过要满足q个约束。
对于每个约束,(a,b)表示需要a种类的硬币使用的个数多余b种类的硬币。
思路:
①对于依赖(a,b),如果我们拿了一个b硬币,那么肯定同时也要拿至少一个a硬币,那么我们可以预处理一下,令一开始的时候,我们是满足所有约束条件的。
那么过程其实就相当于建个反图,然后将t-=a【i】*d,d表示从度为0的点拓扑到当前点链的长度。这样的话我们就能够使得初始的时候,我们是满足所有约束条件的。
②那么接下来,我们考虑拿一个b硬币的时候,同时也要再拿一个a硬币才能继续保证满足约束条件。所以我们再拓扑排序一下过程使得a【v】+=a【u】,然后构建出新的物品集合,然后再跑完全背包即可。
具体细节参考代码;
Ac代码:
#include<stdio.h>#include<string.h>#include<iostream>#include<algorithm>#include<queue>#include<vector>using namespace std;#define mod 1000000007#define ll __int64ll wupin[350];vector<ll>mp[350];ll a[350];ll b[350];ll dp[150000];ll degree[150000];ll vis[150000];ll n,q,t;void Top(){ queue<ll>s; memset(dp,0,sizeof(dp)); for(ll i=1;i<=n;i++) { if(degree[i]==0) { s.push(i); } } ll cnt=0; int cont=0; while(!s.empty()) { ll u=s.front();s.pop(); cont++; if(vis[u]>0)t-=a[u]-b[u]; if(a[u]<=t)wupin[cnt++]=a[u]; for(ll i=0;i<mp[u].size();i++) { ll v=mp[u][i]; a[v]+=a[u]; degree[v]--; if(degree[v]==0)s.push(v); } } dp[0]=1; for(ll i=0;i<cnt;i++) { for(ll j=wupin[i];j<=t;j++) { dp[j]+=dp[j-wupin[i]]; dp[j]%=mod; } } if(t>=0&&cont==n) printf("%I64d\n",dp[t]); else printf("0\n");}int main(){ while(~scanf("%I64d%I64d%I64d",&n,&q,&t)) { memset(vis,0,sizeof(vis)); memset(degree,0,sizeof(degree)); for(ll i=1;i<=n;i++)mp[i].clear(); for(ll i=1;i<=n;i++)scanf("%I64d",&a[i]),b[i]=a[i]; for(ll i=0;i<q;i++) { ll x,y;scanf("%I64d%I64d",&x,&y); vis[x]++,vis[y]++; mp[x].push_back(y); degree[y]++; } Top(); }}
阅读全文
0 0
- Codeforces 284E Coin Troubles【思维+拓扑排序+完全背包】好题!
- 【Codeforces 283 C. Coin Troubles】+ 拓扑序 + 完全背包
- codeforces 283C Coin Troubles (神级思维+判环+背包)
- CodeForces 283C Coin Troubles 分析+背包思想
- CodeForces 283C Coin Troubles 背包问题 分析问题
- Educational Codeforces Round 25E. Minimal Labels(拓扑排序+思维)
- Codeforces 270E Flawed Flow【思维+类拓扑排序】
- codeforces 825E Minimum Label 拓扑排序+逆向思维贪心
- Gym 101503I Just Matrix【思维+拓扑排序】好题~
- Codeforces 584E Anton and Ira【思维+贪心】好题~
- Codeforces 190E Counter Attack【思维+Bfs】好题!
- ZOJ3524:Crazy Shopping(拓扑排序+完全背包)
- ZOJ3524Crazy Shopping(完全背包+拓扑排序)经典
- Crazy Shopping(拓扑排序+完全背包)
- Codeforces 214C Game【思维+拓扑排序】
- CodeForces 825E Educational #25 E:拓扑排序+优先队列
- Codeforces 864E Fire【排序+背包dp】
- Uva-674 Coin Change (完全背包 入门题)
- 1044. Lucky Tickets. Easy!
- 【Android】沉浸式状态栏设置
- Java IO的RandomAccessFile的使用
- 仿微博的评论功能以及评论区置顶效果
- 一个字符串包含的所有回文串
- Codeforces 284E Coin Troubles【思维+拓扑排序+完全背包】好题!
- ubuntu16.04 下dbus-cxx Library编译安装指南
- android 自定义权限
- VS高级使用方法1:autoexp.dat使用Visual studio在debug时显示变量内容
- Java学习笔记之IO(十二):SequenceInputStream序列流
- webstorm创建cordova项目运行失败
- Java获取当前时间日期
- 逆矩阵的计算方法
- Gym 101164.C