• E. Nastya and Potions


     Problem - E - Codeforces

    思路:想到用图论前驱图了,但是因为考虑可能有环的存在,但是其实题干中说明了不能通过一种或几种混合得到自己,所以就保证了不存在环,那就能用拓扑结构的性质做,用记忆化搜索就可以了

    1. // Problem: E. Nastya and Potions
    2. // Contest: Codeforces - Codeforces Round 888 (Div. 3)
    3. // URL: https://codeforces.com/contest/1851/problem/E
    4. // Memory Limit: 256 MB
    5. // Time Limit: 3000 ms
    6. #include
    7. #include
    8. #include
    9. #define fi first
    10. #define se second
    11. #define i128 __int128
    12. using namespace std;
    13. typedef long long ll;
    14. typedef double db;
    15. typedef pair<int,int> PII;
    16. const double eps=1e-7;
    17. const int N=5e5+7 ,M=5e5+7, INF=0x3f3f3f3f,mod=1e9+7,mod1=998244353;
    18. const long long int llINF=0x3f3f3f3f3f3f3f3f;
    19. inline ll read() {ll x=0,f=1;char c=getchar();while(c<'0'||c>'9') {if(c=='-') f=-1;c=getchar();}
    20. while(c>='0'&&c<='9') {x=(ll)x*10+c-'0';c=getchar();} return x*f;}
    21. inline void write(ll x) {if(x < 0) {putchar('-'); x = -x;}if(x >= 10) write(x / 10);putchar(x % 10 + '0');}
    22. inline void write(ll x,char ch) {write(x);putchar(ch);}
    23. void stin() {freopen("in_put.txt","r",stdin);freopen("my_out_put.txt","w",stdout);}
    24. bool cmp0(int a,int b) {return a>b;}
    25. template<typename T> T gcd(T a,T b) {return b==0?a:gcd(b,a%b);}
    26. template<typename T> T lcm(T a,T b) {return a*b/gcd(a,b);}
    27. void hack() {printf("\n----------------------------------\n");}
    28. int T,hackT;
    29. int n,m,k;
    30. ll cost[N];
    31. bool st[N];
    32. int h[N],e[M],ne[M],w[M],idx;
    33. void add(int a,int b) {
    34. e[idx]=b,ne[idx]=h[a],h[a]=idx++;
    35. }
    36. void dfs(int u) {
    37. if(h[u]==-1) {
    38. st[u]=true;
    39. return ;
    40. }
    41. ll res=0;
    42. for(int i=h[u];i!=-1;i=ne[i]) {
    43. int j=e[i];
    44. if(!st[j]) dfs(j);
    45. res+=cost[j];
    46. }
    47. st[u]=true;
    48. cost[u]=min(res,cost[u]);
    49. }
    50. void solve() {
    51. n=read(),k=read();
    52. for(int i=1;i<=n;i++) cost[i]=read(),st[i]=false;
    53. for(int i=1;i<=k;i++) {
    54. int c=read();
    55. cost[c]=0;
    56. st[c]=true;
    57. }
    58. memset(h,-1,sizeof(int)*(n+4));
    59. idx=0;
    60. for(int i=1;i<=n;i++) {
    61. int m=read();
    62. for(int j=1;j<=m;j++) {
    63. int c=read();
    64. add(i,c);
    65. }
    66. }
    67. for(int i=1;i<=n;i++) {
    68. if(!st[i]) dfs(i);
    69. printf("%d ",cost[i]);
    70. }
    71. printf("\n");
    72. }
    73. int main() {
    74. // init();
    75. // stin();
    76. // ios::sync_with_stdio(false);
    77. scanf("%d",&T);
    78. // T=1;
    79. while(T--) hackT++,solve();
    80. return 0;
    81. }

  • 相关阅读:
    刷题记录 -- 面试题
    Appnium如何正确链接模拟器
    Redis持久化
    SQL注入与PreparedStatement对象
    关于项目管理的若干建议
    代码随想录算法训练营Day60|单调栈01
    厂家解读新标准GB21148-2020《足部防护 安全鞋》的变化有哪些(二)
    Python列出一个文件夹下的所有文件tif文件
    Mysql 45讲学习笔记(二十八)读写分离
    面向对象的照妖镜——UML类图绘制指南
  • 原文地址:https://blog.csdn.net/zzzyyzz_/article/details/132745655