• 22/6/26


    1,cf Omkar and Heavenly Tree;2,acwing 1127 香甜的黄油;3,acwing 1126 最小花费;


    1,Omkar and Heavenly Tree

    题意:给你n个节点和m个限制,建一棵符合条件的树;m<n,m个限制是给出a,b,c三个节点,b不能出现在a到c的简单路径上; 

    思路:m<n,所以给出的限制一定不会包含所有的节点,那么我们就可以用未给出的b节点来做根,它一定满足所有要求;然后把其他的节点都连向它即可;

    1. #pragma GCC optimize(2)
    2. #include<bits/stdc++.h>
    3. #define rep1(i,a,n) for( int i=a;i<n;++i)
    4. #define rep2(i,a,n) for( int i=a;i<=n;++i)
    5. #define per1(i,n,a) for( int i=n;i>a;i--)
    6. #define per2(i,n,a) for( int i=n;i>=a;i--)
    7. #define quick_cin() cin.tie(0),cout.tie(0),ios::sync_with_stdio(false)
    8. #define memset(a,i,b) memset((a),(i),sizeof (b))
    9. #define memcpy(a,i,b) memcpy((a),(i),sizeof (b))
    10. #define pro_q priority_queue
    11. #define pb push_back
    12. #define pf push_front
    13. #define endl "\n"
    14. #define lowbit(m) ((-m)&(m))
    15. #define YES cout<<"YES\n"
    16. #define NO cout<<"NO\n"
    17. #define Yes cout<<"Yes\n"
    18. #define No cout<<"No\n"
    19. #define yes cout<<"yes\n"
    20. #define no cout<<"no\n"
    21. #define yi first
    22. #define er second
    23. using namespace std;
    24. typedef pair<long long,long long>PLL;
    25. typedef long long LL;
    26. typedef pair<int,int> PII;
    27. typedef pair<int,PII> PIII;
    28. typedef double dob;
    29. const int N=1e5+10;
    30. int p[N];
    31. void solve()
    32. {
    33. int n,m;
    34. cin>>n>>m;
    35. memset(p,0,p);
    36. while(m--)
    37. {
    38. int a,b,c;
    39. cin>>a>>b>>c;
    40. p[b]=1;
    41. }
    42. int root;
    43. rep2(i,1,n)
    44. {
    45. if(p[i]==0)
    46. {
    47. root=i;
    48. break;
    49. }
    50. }
    51. rep2(i,1,n)
    52. {
    53. if(i!=root)
    54. {
    55. cout<<i<<" "<<root<<endl;
    56. }
    57. }
    58. }
    59. signed main()
    60. {
    61. quick_cin();
    62. int T;
    63. cin>>T;
    64. while(T--)solve();
    65. return 0;
    66. }

    2,香甜的黄油;

     很明显,这是一个多源最短路问题;

    但是floyed算法n^3会tle,考虑其他做法;

    朴素版dijkstra:n^3;tle;

    堆优化版dijkstra:n*(mlongn);10^7的复杂度,不会tle;

    spfa:n*m;10^6;更不会tle了;(spfa一般情况是O(m),但是可能会被卡成O(n*m));

    所以用spfa来做此题;

    简单复习下spfa算法:

     

     

    1. #pragma GCC optimize(2)
    2. #include<bits/stdc++.h>
    3. #define rep1(i,a,n) for( int i=a;i<n;++i)
    4. #define rep2(i,a,n) for( int i=a;i<=n;++i)
    5. #define per1(i,n,a) for( int i=n;i>a;i--)
    6. #define per2(i,n,a) for( int i=n;i>=a;i--)
    7. #define quick_cin() cin.tie(0),cout.tie(0),ios::sync_with_stdio(false)
    8. #define memset(a,i,b) memset((a),(i),sizeof (b))
    9. #define memcpy(a,i,b) memcpy((a),(i),sizeof (b))
    10. #define pro_q priority_queue
    11. #define pb push_back
    12. #define pf push_front
    13. #define endl "\n"
    14. #define lowbit(m) ((-m)&(m))
    15. #define YES cout<<"YES\n"
    16. #define NO cout<<"NO\n"
    17. #define Yes cout<<"Yes\n"
    18. #define No cout<<"No\n"
    19. #define yes cout<<"yes\n"
    20. #define no cout<<"no\n"
    21. #define yi first
    22. #define er second
    23. using namespace std;
    24. typedef pair<long long,long long>PLL;
    25. typedef long long LL;
    26. typedef pair<int,int> PII;
    27. typedef pair<int,PII> PIII;
    28. typedef double dob;
    29. const int N=1e4+10;
    30. int e[N],ne[N],w[N],idx,h[N];
    31. int n,p,c;
    32. int dist[N];
    33. int st[N];
    34. int id[N];
    35. void add(int a,int b,int c)
    36. {
    37. w[idx]=c,e[idx]=b,ne[idx]=h[a],h[a]=idx++;
    38. }
    39. int spfa(int op)
    40. {
    41. memset(dist,0x3f,dist);
    42. queue<int>q;
    43. dist[op]=0;
    44. q.push(op);
    45. st[op]=1;
    46. while(q.size())
    47. {
    48. int t=q.front();q.pop();
    49. st[t]=0;
    50. for(int i=h[t];~i;i=ne[i])
    51. {
    52. int j=e[i];
    53. if(dist[j]>dist[t]+w[i])
    54. {
    55. dist[j]=dist[t]+w[i];
    56. if(!st[j])
    57. {
    58. q.push(j);
    59. st[j]=1;
    60. }
    61. }
    62. }
    63. }
    64. int res=0;
    65. rep2(i,1,n)
    66. {
    67. int j=id[i];
    68. if(dist[j]==0x3f3f3f3f)return INT_MAX;
    69. res+=dist[j];
    70. }
    71. return res;
    72. }
    73. signed main()
    74. {
    75. quick_cin();
    76. memset(h,-1,h);
    77. cin>>n>>p>>c;
    78. rep2(i,1,n)cin>>id[i];
    79. while(c--)
    80. {
    81. int a,b,w;
    82. cin>>a>>b>>w;
    83. add(a,b,w),add(b,a,w);
    84. }
    85. int ans=INT_MAX;
    86. rep2(i,1,p)ans=min(ans,spfa(i));
    87. cout<<ans;
    88. return 0;
    89. }

    3,最小花费;

     思路:,将转账看成是在图上走过一条边,这条边的权是1 − z % ,并且每走过一条边,就乘上这个权值。那么相当于要问,从A 走到B 的最大权值乘积是多少。

    1. #pragma GCC optimize(2)
    2. #include<bits/stdc++.h>
    3. #define rep1(i,a,n) for( int i=a;i<n;++i)
    4. #define rep2(i,a,n) for( int i=a;i<=n;++i)
    5. #define per1(i,n,a) for( int i=n;i>a;i--)
    6. #define per2(i,n,a) for( int i=n;i>=a;i--)
    7. #define quick_cin() cin.tie(0),cout.tie(0),ios::sync_with_stdio(false)
    8. #define memset(a,i,b) memset((a),(i),sizeof (b))
    9. #define memcpy(a,i,b) memcpy((a),(i),sizeof (b))
    10. #define pro_q priority_queue
    11. #define pb push_back
    12. #define pf push_front
    13. #define endl "\n"
    14. #define lowbit(m) ((-m)&(m))
    15. #define YES cout<<"YES\n"
    16. #define NO cout<<"NO\n"
    17. #define Yes cout<<"Yes\n"
    18. #define No cout<<"No\n"
    19. #define yes cout<<"yes\n"
    20. #define no cout<<"no\n"
    21. #define yi first
    22. #define er second
    23. using namespace std;
    24. typedef pair<long long,long long>PLL;
    25. typedef long long LL;
    26. typedef pair<int,int> PII;
    27. typedef pair<int,PII> PIII;
    28. typedef double dob;
    29. const int N=2010,M=2e5+10,INF=0x3f3f3f3f;
    30. int n,m;
    31. dob d[N][N];
    32. int s,e;
    33. dob dist[N];
    34. int st[N];
    35. void dijkstra()
    36. {
    37. dist[s]=1;
    38. rep2(i,1,n)
    39. {
    40. int ver=-1;
    41. rep2(j,1,n)
    42. {
    43. if(!st[j]&&(ver==-1||dist[j]>dist[ver]))ver=j;
    44. }
    45. st[ver]=1;
    46. rep2(j,1,n)dist[j]=max(dist[j],dist[ver]*d[ver][j]);
    47. }
    48. }
    49. signed main()
    50. {
    51. quick_cin();
    52. cin>>n>>m;
    53. while(m--)
    54. {
    55. int a,b,w;
    56. cin>>a>>b>>w;
    57. dob t=(100.0-w)/100;
    58. d[a][b]=d[b][a]=max(d[a][b],t);
    59. }
    60. cin>>s>>e;
    61. dijkstra();
    62. cout<<fixed<<setprecision(8)<<100/dist[e];
    63. return 0;
    64. }

    然后关于单源最值的问题;

    spfa可以求乘积最大和最小,也可以求加法最大和最小,原理是其是松弛操作不是贪心;

    而dijkstra可以求加法最小(正权边,)乘积最大;

     

  • 相关阅读:
    【通信】两层无线 Femtocell 网络上行链路中的最优功率分配附matlab代码
    宏转录组分析揭示不同土壤生境中氮循环基因的表达
    基于企鹅优化算法的机器人轨迹规划(Matlab代码实现)
    选择低代码开发,我轻松了不少!
    Pinctrl子系统_02_使用Pinctrl要掌握的重要概念
    QT学习day4
    重要消息丨.NET Core 3.1 将于今年12月13日结束支持
    GitHub详解:代码托管与协作开发平台
    第75步 时间序列建模实战:多步滚动预测 vol-3(以决策树回归为例)
    [设计模式Java实现附plantuml源码~行为型]定义算法的框架——模板方法模式
  • 原文地址:https://blog.csdn.net/aidaqiudeaichao/article/details/125469736