• Hdu 3549 Flow Problem(最大流)



    Flow Problem
    Time Limit: 5000/5000 MS (Java/Others) Memory Limit: 65535/32768 K (Java/Others)
    Total Submission(s): 14412 Accepted Submission(s): 6859
    Problem Description
    Network flow is a well-known difficult problem for ACMers. Given a graph, your task is to find out the maximum flow for the weighted directed graph.
    Input
    The first line of input contains an integer T, denoting the number of test cases.
    For each test case, the first line contains two integers N and M, denoting the number of vertexes and edges in the graph. (2 <= N <= 15, 0 <= M <= 1000)
    Next M lines, each line contains three integers X, Y and C, there is an edge from X to Y and the capacity of it is C. (1 <= X, Y <= N, 1 <= C <= 1000)
    Output
    For each test cases, you should output the maximum flow from source 1 to sink N.
    Sample Input
    2
    3 2
    1 2 1
    2 3 1
    3 3
    1 2 1
    2 3 1
    1 3 1
    Sample Output
    Case 1: 1
    Case 2: 2
    Author
    HyperHexagon

    1. /* 裸题. 求1~n的最大流. 多组数据哦. */
    2. #include<iostream>
    3. #include<cstdio>
    4. #include<queue>
    5. #include<cstring>
    6. #define MAXN 10001
    7. using namespace std;
    8. struct data{int v,next,c;}e[MAXN*2];
    9. int n,m,tot,cut,s=1,max1=1e9,head[MAXN],dis[MAXN];
    10. int read()
    11. {
    12. int x=0,f=1;char ch=getchar();
    13. while(ch<'0'||ch>'9') {if(ch=='-') f=-1;ch=getchar();}
    14. while(ch>='0'&&ch<='9') x=x*10+ch-48,ch=getchar();
    15. return x*f;
    16. }
    17. void add(int u,int v,int x)
    18. {
    19. e[++cut].v=v;
    20. e[cut].c=x;
    21. e[cut].next=head[u];
    22. head[u]=cut;
    23. }
    24. int bfs()
    25. {
    26. queue<int >q;
    27. memset(dis,-1,sizeof dis);
    28. dis[s]=0;
    29. q.push(s);
    30. while(!q.empty())
    31. {
    32. int u=q.front();q.pop();
    33. for(int i=head[u];i;i=e[i].next)
    34. {
    35. int v=e[i].v;
    36. if(dis[v]==-1&&e[i].c>0)
    37. {
    38. dis[v]=dis[u]+1;
    39. q.push(v);
    40. }
    41. }
    42. }
    43. return dis[n]!=-1;
    44. }
    45. int dfs(int u,int y)
    46. {
    47. int rest=0;
    48. if(u==n) return y;
    49. for(int i=head[u];i&&rest<y;i=e[i].next)
    50. {
    51. int v=e[i].v;
    52. if(e[i].c>0&&dis[v]==dis[u]+1)
    53. {
    54. int x=min(e[i].c,y-rest);
    55. x=dfs(v,x);
    56. rest+=x;
    57. e[i].c-=x;
    58. e[i^1].c+=x;
    59. }
    60. }
    61. if(!rest) dis[u]=-1;
    62. return rest;
    63. }
    64. int dinic(int s,int t)
    65. {
    66. int tot=0;
    67. while(bfs())
    68. {
    69. int ans=dfs(s,max1);
    70. while(ans){tot+=ans;ans=dfs(s,max1);}
    71. }
    72. return tot;
    73. }
    74. void Clear()
    75. {
    76. cut=1;//2WA
    77. memset(dis,-1,sizeof dis);
    78. memset(head,0,sizeof head);
    79. }
    80. int main()
    81. {
    82. int t,u,v,x;t=read();
    83. for(int i=1;i<=t;i++)
    84. {
    85. n=read();m=read();
    86. Clear();
    87. while(m--)
    88. {
    89. u=read(),v=read(),x=read();
    90. add(u,v,x),add(v,u,0);//1WA
    91. }
    92. printf("Case %d: %d\n",i,dinic(1,n));
    93. }
    94. return 0;
    95. }
  • 相关阅读:
    Apache DolphinScheduler v1.3.9 补数执行连续月任务的参数配置方法
    自动化测试——selenium(环境部署和元素定位)
    基于springboot+vue的戒毒所人员管理系统毕业设计源码251514
    写着简单跑着又快的数据库语言SPL
    Java基础学习笔记(七)—— 面向对象编程(3)
    (Note)硬件领域SCI期刊
    java基于springboot+vue动物诊所综合管理系统
    Linux - 进程
    端口扫描-安全体系-网络安全技术和协议
    Java 数据结构、集合框架、ArrayList
  • 原文地址:https://blog.csdn.net/m0_62089210/article/details/127853096