• 图论刷题记录


    [NOIP2003 提高组] 神经网络 - 洛谷P1038 [NOIP2003 提高组] 神经网络

    容鄙人说一下,这是鄙人刷过最caodan的一个题,我就没见过这么刁钻的数据..

    传入层的出度可以是0,但是,他依然可以传导,并且还应该被当做答案输出

    第二,传入层一开始就是激发状态!并且不能减u  我就没见过这么caodan

    题目似乎也说了

     意思是传入层根本没有j属于i,那么按这个推ci就是负数,也就无法传导,所以...就很离谱的不要减u才正确,关键是题目根本没有挑明。。

    1. # include <iostream>
    2. # include<vector>
    3. # include<queue>
    4. using namespace std;
    5. int ru[110];
    6. int chu[110];
    7. int f[110][110];
    8. vector<int>v[110];
    9. int c[110],u[110];
    10. queue<int>q;
    11. int main()
    12. {
    13. int n,m;
    14. cin>>n>>m;
    15. for(int i=1; i<=n; i++)
    16. {
    17. cin>>c[i]>>u[i];
    18. }
    19. for(int i=1; i<=m; i++)
    20. {
    21. int x,y,z;
    22. cin>>x>>y>>z;
    23. f[x][y]=z;
    24. v[x].push_back(y);
    25. chu[x]++;
    26. ru[y]++;
    27. }
    28. for(int i=1; i<=n; i++)
    29. {
    30. if(ru[i]==0)
    31. {
    32. q.push(i);
    33. c[i]-=u[i];
    34. }
    35. else
    36. {
    37. c[i]-=u[i];
    38. }
    39. }
    40. while(!q.empty())
    41. {
    42. int now=q.front();
    43. q.pop();
    44. for(auto it:v[now])
    45. {
    46. ru[it]--;
    47. c[it]+=c[now]*f[now][it];
    48. if(ru[it]==0&&c[it]>0)
    49. {
    50. q.push(it);
    51. }
    52. }
    53. }
    54. int flag=0;
    55. for(int i=1; i<=n; i++)
    56. {
    57. if(chu[i]==0&&c[i]>0)
    58. {
    59. cout<<i<<" "<<c[i]<<endl;
    60. flag=1;
    61. }
    62. }
    63. if(!flag)
    64. {
    65. cout<<"NULL";
    66. }
    67. return 0;
    68. }

    把等级低的往等级高的连一条有向边即可,拓扑DP

    1. #include <iostream>
    2. # include<algorithm>
    3. # include<cstring>
    4. # include<vector>
    5. # include<queue>
    6. using namespace std;
    7. typedef long long int ll;
    8. int du[1010];
    9. int st[1010];
    10. bool book[1010];
    11. bool edge[1010][1010];
    12. vector<int>v[1010];
    13. int ans[1010];
    14. int dp[1010];
    15. queue<int>q;
    16. int main()
    17. {
    18. int n,m;
    19. cin>>n>>m;
    20. while(m--)
    21. {
    22. int t;
    23. cin>>t;
    24. for(int i=1; i<=t; i++)
    25. {
    26. cin>>st[i];
    27. book[st[i]]=1;
    28. }
    29. for(int i=st[1]; i<=st[t]; i++)
    30. {
    31. if(!book[i])
    32. {
    33. for(int j=1; j<=t; j++)
    34. {
    35. if(!edge[i][st[j]])
    36. {
    37. edge[i][st[j]]=1;
    38. v[i].push_back(st[j]);
    39. du[st[j]]++;
    40. }
    41. }
    42. }
    43. }
    44. memset(book,0,sizeof(book));
    45. }
    46. for(int i=1; i<=n; i++)
    47. {
    48. if(!du[i])
    49. {
    50. q.push(i);
    51. dp[i]=1;
    52. }
    53. }
    54. int ans=1;
    55. while(!q.empty())
    56. {
    57. int now=q.front();
    58. q.pop();
    59. for(auto it:v[now])
    60. {
    61. du[it]--;
    62. if(du[it]==0)
    63. q.push(it);
    64. if(!dp[it])
    65. dp[it]=dp[now]+1;
    66. else
    67. dp[it]=max(dp[it],dp[now]+1);
    68. ans=max(ans,dp[it]);
    69. }
    70. }
    71. cout<<ans;
    72. return 0;
    73. }

    两种贪心方法,第一种是放进小根堆,把最小的先输出。但会被hack,例如

    (5,2)  (4,3)  输出  1 4 3 5 2 实际上应该是   1 5 2 4 3。

    第二种就是放进大根堆,建立反图,倒着输出

    (5,2) (4,3)      ->  (2,5)  (3,4) ->  3 4 2 5 1   -> 1 5 2 4 3 

    这样就OK了

    1. # include <iostream>
    2. # include<algorithm>
    3. # include<cstring>
    4. # include<vector>
    5. # include<queue>
    6. using namespace std;
    7. typedef long long int ll;
    8. vector<int>v[100000+10];
    9. int du[100000+10];
    10. priority_queue<int>q;
    11. int ans[100000+10];
    12. int main()
    13. {
    14. int t;
    15. cin>>t;
    16. while(t--)
    17. {
    18. int n,m,len;
    19. cin>>n>>m;
    20. while(!q.empty())
    21. q.pop();
    22. for(int i=1;i<=n;i++)
    23. {
    24. v[i].clear();
    25. du[i]=0;
    26. }
    27. for(int i=1;i<=m;i++)
    28. {
    29. int x,y;
    30. cin>>x>>y;
    31. v[y].push_back(x);
    32. du[x]++;
    33. }
    34. for(int i=1;i<=n;i++)
    35. {
    36. if(du[i]==0)
    37. q.push(i);
    38. }
    39. len=0;
    40. while(!q.empty())
    41. {
    42. int now=q.top();
    43. len++;
    44. ans[len]=now;
    45. q.pop();
    46. for(auto it:v[now])
    47. {
    48. du[it]--;
    49. if(!du[it])
    50. q.push(it);
    51. }
    52. }
    53. if(len!=n)
    54. {
    55. cout<<"Impossible!"<<endl;
    56. continue;
    57. }
    58. for(int i=len;i>=1;i--)
    59. {
    60. cout<<ans[i]<<" ";
    61. }
    62. cout<<endl;
    63. }
    64. return 0;
    65. }

    基于迪杰斯特拉的BFS,为了能够拦截,必须上下左右连续建造墙,从第一整列,第n整行开始扩展,每次选取最小花费的点,只能上下左右移动,一旦到达第一行或者第m列,拦截成功。细节较多,要开ll

    1. #include <queue>
    2. #include <iostream>
    3. #include <algorithm>
    4. #include <cstdio>
    5. #include <cstring>
    6. using namespace std;
    7. typedef long long ll;
    8. int nx[4]= {0,0,-1,1};
    9. int ny[4]= {1,-1,0,0};
    10. int n,m;
    11. bool check(int x,int y)
    12. {
    13. return (x>=1&&x<=n&&y>=1&&y<=m);
    14. }
    15. ll mp[510][510], rem[510][510], inf=1e18,edge[510][510];
    16. struct node
    17. {
    18. int x,y;
    19. ll cost;
    20. friend bool operator<(node a, node b)
    21. {
    22. return a.cost>b.cost;
    23. }
    24. };
    25. priority_queue<node>q;
    26. bool book[510][510];
    27. int main ()
    28. {
    29. int t;
    30. cin>>t>>n>>m;
    31. while(t--)
    32. {
    33. for(int i=1; i<=n; i++)
    34. {
    35. for(int j=1; j<=m; j++)
    36. {
    37. cin>>mp[i][j];
    38. if(mp[i][j]==0)
    39. mp[i][j]=-1;
    40. else if(mp[i][j]==-1)
    41. mp[i][j]=0;
    42. rem[i][j]=inf;
    43. }
    44. }
    45. memset(book,0,sizeof(book));
    46. for(int i=1; i<=n; i++)
    47. {
    48. if(mp[i][1]==-1)
    49. continue;
    50. struct node now;
    51. now.x=i;
    52. now.y=1;
    53. if(mp[i][1]==0)
    54. {
    55. now.cost=0;
    56. rem[i][1]=0;
    57. book[i][1]=1;
    58. }
    59. else
    60. {
    61. now.cost=mp[i][1];
    62. rem[i][1]=now.cost;
    63. book[i][1]=1;
    64. }
    65. q.push(now);
    66. }
    67. for(int i=1; i<=m; i++)
    68. {
    69. if(mp[n][i]==-1)
    70. continue;
    71. struct node now;
    72. now.x=n;
    73. now.y=i;
    74. if(mp[n][i]==0)
    75. {
    76. now.cost=0;
    77. rem[n][i]=0;
    78. book[n][i]=1;
    79. }
    80. else
    81. {
    82. now.cost=mp[n][i];
    83. rem[n][i]=now.cost;
    84. book[n][i]=1;
    85. }
    86. q.push(now);
    87. }
    88. ll ans=-1;
    89. while(!q.empty())
    90. {
    91. struct node now=q.top();
    92. q.pop();
    93. if(now.x==1||now.y==m)
    94. {
    95. ans=now.cost;
    96. break;
    97. }
    98. for(int i=0; i<4; i++)
    99. {
    100. int xx=now.x+nx[i];
    101. int yy=now.y+ny[i];
    102. if(!check(xx,yy))
    103. continue;
    104. if(mp[xx][yy]==-1)
    105. continue;
    106. if(rem[xx][yy]>now.cost+mp[xx][yy])
    107. {
    108. rem[xx][yy]=now.cost+mp[xx][yy];
    109. struct node tail;
    110. book[xx][yy]=1;
    111. tail.x=xx;
    112. tail.y=yy;
    113. tail.cost=rem[xx][yy];
    114. q.push(tail);
    115. }
    116. }
    117. }
    118. while(!q.empty())
    119. q.pop();
    120. cout<<ans<<endl;
    121. }
    122. }

  • 相关阅读:
    轻量级RPC分布式网络通信框架设计——序列化协议Protobuf
    实现动态表单的一种思路 | 京东云技术团队
    互联网加竞赛 车道线检测(自动驾驶 机器视觉)
    【黑马程序员JVM学习笔记】02.内存结构
    应用系统设计:基于Spring security设计一套安全认证和授权的服务
    视频融合云平台EasyCVR增加多级分组,可灵活管理接入设备
    2023湖南省赛-B Square game
    抓包与发流软件与网络诊断
    three.js学习笔记(十九)——后期处理
    设计模式-面试题
  • 原文地址:https://blog.csdn.net/jisuanji2606414/article/details/126112038