【Codeforces 283 C. Coin Troubles】+ 拓扑序 + 完全背包
来源:互联网 发布:欧陆风云4秘籍源码军事 编辑:程序博客网 时间:2024/05/17 23:29
C. Coin Troubles
time limit per test2 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard output
In the Isle of Guernsey there are n different types of coins. For each i (1 ≤ i ≤ n), coin of type i is worth ai cents. It is possible that ai = aj for some i and j (i ≠ j).
Bessie has some set of these coins totaling t cents. She tells Jessie q pairs of integers. For each i (1 ≤ i ≤ q), the pair bi, ci tells Jessie that Bessie has a strictly greater number of coins of type bi than coins of type ci. It is known that all bi are distinct and all ci are distinct.
Help Jessie find the number of possible combinations of coins Bessie could have. Two combinations are considered different if there is some i (1 ≤ i ≤ n), such that the number of coins Bessie has of type i is different in the two combinations. Since the answer can be very large, output it modulo 1000000007 (109 + 7).
If there are no possible combinations of coins totaling t cents that satisfy Bessie’s conditions, output 0.
Input
The first line contains three space-separated integers, n, q and t (1 ≤ n ≤ 300; 0 ≤ q ≤ n; 1 ≤ t ≤ 105). The second line contains n space separated integers, a1, a2, …, an (1 ≤ ai ≤ 105). The next q lines each contain two distinct space-separated integers, bi and ci (1 ≤ bi, ci ≤ n; bi ≠ ci).
It’s guaranteed that all bi are distinct and all ci are distinct.
Output
A single integer, the number of valid coin combinations that Bessie could have, modulo 1000000007 (109 + 7).
Examples
Input
4 2 17
3 1 2 5
4 2
3 4
Output
3
Input
3 2 6
3 1 1
1 2
2 3
Output
0
Input
3 2 10
1 2 3
1 2
2 1
Output
0
Note
For the first sample, the following 3 combinations give a total of 17 cents and satisfy the given conditions: {0 of type 1, 1 of type 2, 3 of type 3, 2 of type 4}, {0, 0, 6, 1}, {2, 0, 3, 1}.
No other combinations exist. Note that even though 4 occurs in both bi and ci, the problem conditions are still satisfied because all bi are distinct and all ci are distinct.
对于 u,v ,选 v 必选 u ,且 num[u] > num[v],所以 u 至少有一个,而 vl[v] += vl[u],选 v 必须选 u,有可能会有 u v,v u 的环,此时满足不了,而后完全背包
AC代码:
#include<cstdio>#include<cstring>using namespace std;const int mod = 1e9 + 7;const int K = 1e5 + 10;const int k = 310;int dp[K],in[k],s[k],a[k];int main(){ int n,q,t,u,v; scanf("%d %d %d",&n,&q,&t); for(int i = 1; i <= n; i++) scanf("%d",&a[i]); memset(in,0,sizeof(in)); memset(s,0,sizeof(s)); memset(dp,0,sizeof(dp)); for(int i = 1; i <= q; i++){ scanf("%d %d",&u,&v); in[v]++,s[u] = v; } while(q--){ int p = 0; for(int i = 1; i <= n; i++) if(!in[i] && s[i]){ p = i; break; } if(!p) { puts("0"); return 0; } // 说明有环,不能满足 t -= a[p]; // 这枚硬币至少有一个 if(t < 0) { puts("0"); return 0; } int ms = s[p]; s[p] = 0; in[ms]--; a[ms] += a[p]; // 不能比 p 少,选 ms 必选 p } dp[0] = 1; for(int i = 1; i <= n; i++) // 完全背包 for(int j = a[i]; j <= t; j++) dp[j] = (dp[j] + dp[j - a[i]]) % mod; printf("%d\n",dp[t]); return 0;}
- 【Codeforces 283 C. Coin Troubles】+ 拓扑序 + 完全背包
- CodeForces 283C Coin Troubles 分析+背包思想
- CodeForces 283C Coin Troubles 背包问题 分析问题
- Codeforces 284E Coin Troubles【思维+拓扑排序+完全背包】好题!
- codeforces 283C Coin Troubles (神级思维+判环+背包)
- Uva 674 Coin Change 完全背包
- uva 674Coin Change(完全背包)
- uva 674 Coin Change(完全背包)
- UVaOJ-674 Coin Change (完全背包)
- uva674 - Coin Change(完全背包)
- UVA 674 - Coin Change(完全背包)
- UVA 674 Coin Change(完全背包)
- UVA 674 Coin Change(完全背包)
- LightOJ 1232 - Coin Change (II) 【完全背包】
- UVA - 674 Coin Change(完全背包)
- LightOJ 1232 Coin Change (II)(完全背包)
- lightoj 1232 - Coin Change (II) 完全背包
- HDU 1398 Square Coin(简单完全背包)
- Android中Handler的理解
- xdoj 1195: V8与女友
- mybatis输出高级映射(一对多查询)
- Swift3 创建字典
- 慢慢python之路----心情笔记
- 【Codeforces 283 C. Coin Troubles】+ 拓扑序 + 完全背包
- Linux常用命令
- LinkedList-链表集合
- 第6
- 使用OpenCV来进行人脸识别
- (贪心,区间选点问题) Radar Installation POJ 1328
- 算法导论 练习题 11.2-1
- SSH框架搭建 详细图文教程
- xdoj 1188: 大大数星星