码农知识堂 - 1000bd
  •   Python
  •   PHP
  •   JS/TS
  •   JAVA
  •   C/C++
  •   C#
  •   GO
  •   Kotlin
  •   Swift
  • 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. }

  • 相关阅读:
    759页14万字智慧大楼弱电智能化规划设计方案
    C语言-----qsort函数的功能以及模拟实现
    电脑硬盘里的文件能保存多久?电脑硬盘文件突然没了怎么办
    【计算机网络】75 张图详解:网络设备、网络地址规划、静态路由(万字长文)
    Linux系统下四种nginx安装方法
    关键字查询方法
    mysql 客户端SSL错误2026 (HY000)
    【DevOps】Rundeck以及Jenkins
    新库上线 | CnOpenData租赁和商务服务业工商注册企业基本信息数据
    虾皮印尼买家号如何注册
  • 原文地址:https://blog.csdn.net/zzzyyzz_/article/details/132745655
  • 最新文章
  • 攻防演习之三天拿下官网站群
    数据安全治理学习——前期安全规划和安全管理体系建设
    企业安全 | 企业内一次钓鱼演练准备过程
    内网渗透测试 | Kerberos协议及其部分攻击手法
    0day的产生 | 不懂代码的"代码审计"
    安装scrcpy-client模块av模块异常,环境问题解决方案
    leetcode hot100【LeetCode 279. 完全平方数】java实现
    OpenWrt下安装Mosquitto
    AnatoMask论文汇总
    【AI日记】24.11.01 LangChain、openai api和github copilot
  • 热门文章
  • 十款代码表白小特效 一个比一个浪漫 赶紧收藏起来吧!!!
    奉劝各位学弟学妹们,该打造你的技术影响力了!
    五年了,我在 CSDN 的两个一百万。
    Java俄罗斯方块,老程序员花了一个周末,连接中学年代!
    面试官都震惊,你这网络基础可以啊!
    你真的会用百度吗?我不信 — 那些不为人知的搜索引擎语法
    心情不好的时候,用 Python 画棵樱花树送给自己吧
    通宵一晚做出来的一款类似CS的第一人称射击游戏Demo!原来做游戏也不是很难,连憨憨学妹都学会了!
    13 万字 C 语言从入门到精通保姆级教程2021 年版
    10行代码集2000张美女图,Python爬虫120例,再上征途
Copyright © 2022 侵权请联系2656653265@qq.com    京ICP备2022015340号-1
正则表达式工具 cron表达式工具 密码生成工具

京公网安备 11010502049817号