题意:从1到n 再从n到1 不经过重复的边 ,(如果是点就是旅行商问题了),问最短路

建立一个超级源S S到1连一条费用为0,容量为2的边,求费用流即可

如果流<2 那么hehe

否则    输出结果

模板来自Kuangbing 如下:

  1. #include <cstdio>
  2. #include <cstdlib>
  3. #include <cmath>
  4. #include <cstring>
  5. #include <ctime>
  6. #include <algorithm>
  7. #include <iostream>
  8. #include <sstream>
  9. #include <string>
  10. #include <queue>
  11. #define oo 0x13131313
  12. using namespace std;
  13. const int MAXN=200;
  14. const int MAXM=200000;
  15. const int INF=0x3f3f3f3f;
  16. struct Edge
  17. {
  18. int to,next,cap,flow,cost;
  19. void get(int a,int b,int c,int d)
  20. {
  21. to=a,cap=b,cost=c;next=d;flow=0;
  22. }
  23. }edge[MAXM];
  24. int head[MAXN],tol;
  25. int pre[MAXN],dis[MAXN];
  26. bool vis[MAXN];
  27. int N;
  28. void init(int n)
  29. {
  30. N=n;
  31. tol=0;
  32. memset(head,-1,sizeof(head));
  33. }
  34. void addedge(int u,int v,int cap,int cost)
  35. {
  36. edge[tol].get(v,cap,cost,head[u]);head[u]=tol++;
  37. edge[tol].get(u,0,-cost,head[v]);head[v]=tol++;
  38. }
  39. bool spfa(int s,int t)
  40. {
  41. queue<int>q;
  42. for(int i=0;i<N;i++)
  43. {
  44. dis[i]=INF;
  45. vis[i]=false;
  46. pre[i]=-1;
  47. }
  48. dis[s]=0;
  49. vis[s]=true;
  50. q.push(s);
  51. while(!q.empty())
  52. {
  53. int u=q.front();
  54. q.pop();
  55. vis[u]=false;
  56. for(int i= head[u];i!=-1;i=edge[i].next)
  57. {
  58. int v=edge[i].to;
  59. if(edge[i].cap>edge[i].flow&&
  60. dis[v]>dis[u]+edge[i].cost )
  61. {
  62. dis[v]=dis[u]+edge[i].cost;
  63. pre[v]=i;
  64. if(!vis[v])
  65. {
  66. vis[v]=true;
  67. q.push(v);
  68. }
  69. }
  70. }
  71. }
  72. if(pre[t]==-1) return false;
  73. else return true;
  74. }
  75. int minCostMaxflow(int s,int t,int &cost)
  76. {
  77. int flow=0;
  78. cost = 0;
  79. while(spfa(s,t))
  80. {
  81. int Min=INF;
  82. for(int i=pre[t];i!=-1;i=pre[edge[i^1].to])
  83. {
  84. if(Min >edge[i].cap-edge[i].flow)
  85. Min=edge[i].cap-edge[i].flow;
  86. }
  87. for(int i=pre[t];i!=-1;i=pre[edge[i^1].to])
  88. {
  89. edge[i].flow+=Min;
  90. edge[i^1].flow-=Min;
  91. cost+=edge[i].cost*Min;
  92. }
  93. flow+=Min;
  94. }
  95. return flow;
  96. }
  97. int main()
  98. {
  99.  
  100. }

完整代码如下:

  1. #include <cstdio>
  2. #include <cstdlib>
  3. #include <cmath>
  4. #include <cstring>
  5. #include <ctime>
  6. #include <algorithm>
  7. #include <iostream>
  8. #include <sstream>
  9. #include <string>
  10. #include <queue>
  11. #define oo 0x13131313
  12. using namespace std;
  13. const int MAXN=200;
  14. const int MAXM=200000;
  15. const int INF=0x3f3f3f3f;
  16. struct Edge
  17. {
  18. int to,next,cap,flow,cost;
  19. void get(int a,int b,int c,int d)
  20. {
  21. to=a,cap=b,cost=c;next=d;flow=0;
  22. }
  23. }edge[MAXM];
  24. int head[MAXN],tol;
  25. int pre[MAXN],dis[MAXN];
  26. bool vis[MAXN];
  27. int N;
  28. void init(int n)
  29. {
  30. N=n;
  31. tol=0;
  32. memset(head,-1,sizeof(head));
  33. }
  34. void addedge(int u,int v,int cap,int cost)
  35. {
  36. edge[tol].get(v,cap,cost,head[u]);head[u]=tol++;
  37. edge[tol].get(u,0,-cost,head[v]);head[v]=tol++;
  38. }
  39. bool spfa(int s,int t)
  40. {
  41. queue<int>q;
  42. for(int i=0;i<=N;i++)
  43. {
  44. dis[i]=INF;
  45. vis[i]=false;
  46. pre[i]=-1;
  47. }
  48. dis[s]=0;
  49. vis[s]=true;
  50. q.push(s);
  51. while(!q.empty())
  52. {
  53. int u=q.front();
  54. q.pop();
  55. vis[u]=false;
  56. for(int i= head[u];i!=-1;i=edge[i].next)
  57. {
  58. int v=edge[i].to;
  59. if(edge[i].cap>edge[i].flow&&
  60. dis[v]>dis[u]+edge[i].cost )
  61. {
  62. dis[v]=dis[u]+edge[i].cost;
  63. pre[v]=i;
  64. if(!vis[v])
  65. {
  66. vis[v]=true;
  67. q.push(v);
  68. }
  69. }
  70. }
  71. }
  72. if(pre[t]==-1) return false;
  73. else return true;
  74. }
  75. int minCostMaxflow(int s,int t,int &cost)
  76. {
  77. int flow=0;
  78. cost = 0;
  79. while(spfa(s,t))
  80. {
  81. int Min=INF;
  82. for(int i=pre[t];i!=-1;i=pre[edge[i^1].to])
  83. {
  84. if(Min >edge[i].cap-edge[i].flow)
  85. Min=edge[i].cap-edge[i].flow;
  86. }
  87. for(int i=pre[t];i!=-1;i=pre[edge[i^1].to])
  88. {
  89. edge[i].flow+=Min;
  90. edge[i^1].flow-=Min;
  91. cost+=edge[i].cost*Min;
  92. }
  93. flow+=Min;
  94. }
  95. return flow;
  96. }
  97. int NN,MM;
  98. void input()
  99. {
  100. int a,b,c;
  101. for(int i=1;i<=MM;i++)
  102. {
  103. scanf("%d%d%d",&a,&b,&c);
  104. addedge(a,b,1,c);
  105. addedge(b,a,1,c);
  106. }
  107. }
  108. void solve()
  109. {
  110. int ANS=0,t;
  111. addedge(NN+1,1,2,0); //建立源S=NN+1;
  112. t=minCostMaxflow(NN+1,NN,ANS);
  113. if(t==2) printf("%d\n",ANS);
  114. else printf("hehe\n");
  115. }
  116. void File()
  117. {
  118. freopen("a.in","r",stdin);
  119. freopen("a.out","w",stdout);
  120. }
  121. int main()
  122. {
  123. // File();
  124. while(cin>>NN>>MM)
  125. {
  126. init(NN+1);
  127. input();
  128. solve();
  129. }
  130. }

【最小费用最大流模板】【Uva10806+Spring Team PK】Dijkstra, Dijkstra,的更多相关文章

  1. 图论算法-最小费用最大流模板【EK;Dinic】

    图论算法-最小费用最大流模板[EK;Dinic] EK模板 const int inf=1000000000; int n,m,s,t; struct node{int v,w,c;}; vector ...

  2. HDU3376 最小费用最大流 模板2

    Matrix Again Time Limit: 5000/2000 MS (Java/Others)    Memory Limit: 102400/102400 K (Java/Others)To ...

  3. 洛谷P3381 最小费用最大流模板

    https://www.luogu.org/problem/P3381 题目描述 如题,给出一个网络图,以及其源点和汇点,每条边已知其最大流量和单位流量费用,求出其网络最大流和在最大流情况下的最小费用 ...

  4. 最大流 && 最小费用最大流模板

    模板从  这里   搬运,链接博客还有很多网络流题集题解参考. 最大流模板 ( 可处理重边 ) ; const int INF = 0x3f3f3f3f; struct Edge { int from ...

  5. Doctor NiGONiGO’s multi-core CPU(最小费用最大流模板)

    题目链接:http://acm.nyist.net/JudgeOnline/problem.php?pid=693 题意:有一个 k 核的处理器和 n 个工作,全部的工作都须要在一个核上处理一个单位的 ...

  6. 【网络流#2】hdu 1533 - 最小费用最大流模板题

    最小费用最大流,即MCMF(Minimum Cost Maximum Flow)问题 嗯~第一次写费用流题... 这道就是费用流的模板题,找不到更裸的题了 建图:每个m(Man)作为源点,每个H(Ho ...

  7. poj 2195 最小费用最大流模板

    /*Source Code Problem: 2195 User: HEU_daoguang Memory: 1172K Time: 94MS Language: G++ Result: Accept ...

  8. POJ2135 最小费用最大流模板题

    练练最小费用最大流 此外此题也是一经典图论题 题意:找出两条从s到t的不同的路径,距离最短. 要注意:这里是无向边,要变成两条有向边 #include <cstdio> #include ...

  9. POJ 2135 Farm Tour (最小费用最大流模板)

    题目大意: 给你一个n个农场,有m条道路,起点是1号农场,终点是n号农场,现在要求从1走到n,再从n走到1,要求不走重复路径,求最短路径长度. 算法讨论: 最小费用最大流.我们可以这样建模:既然要求不 ...

随机推荐

  1. rsync常用参数详解

    rsync常用参数详解 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 在linux中,一切皆是文件,包括你的终端,硬件设备信息,目录,内核文件等等.所以工作中我们难免会遇到拷贝文件 ...

  2. SQL注入原理深度解析

    本文转自:http://www.iii-soft.com/forum.php?mod=viewthread&tid=1613&extra=page%3D1 对于Web应用来说,注射式攻 ...

  3. docker 实战---使用oracle xe作为开发数据库(六)

    oracle作为oltp的大佬,非常多行业应用都会用到它.那么在开发的过程中就不可避免的要使用oracle数据库,oracle数据库的版本号有好多,当中express版本号是免费的开发版.它的主要限制 ...

  4. freemarker声明变量

    freemarker声明变量 1.使用assign创建和替换变量 (1)新建声明变量的ftl variable.ftl: <html> <head> <meta http ...

  5. 加载本地html遇到的问题

    之前要做一个Demo,需要用UIWebView来加载网页,前端的同事把资源包给我,里面包含html,css,JavaScript,图片等文件.我想当然的把文件夹拷到工程中,然后用以下方法加载: NSU ...

  6. 【二分图最大匹配】【HDU2063】过山车

    [科普]什么是BestCoder?如何参加? 过山车 Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Ja ...

  7. ECShop2.7.2详细文件结构及模板结构目录名称

    ┣plugins目录┣templates目录┃   ┣backup目录┃   ┃   ┣index.htm┃   ┃   ┗ibrary目录┃   ┃       ┗index.htm┃   ┣cac ...

  8. JavaScript 【正则表达式验证数字代码】

    可以看到 Ajax 请求多了个 x-requested-with ,可以利用它,request.getHeader("x-requested-with"); 为 null,则为传统 ...

  9. canvas入门

    <html> <head> <script> window.onload=function(){ var canvas=document.getElementByI ...

  10. (转)C/C++中static关键字

    下面的转自http://www.cnblogs.com/yc_sunniwell/archive/2010/07/14/1777441.html 静态变量作用范围在一个文件内,程序开始时分配空间,结束 ...