裸的费用流。一开始因为这句话还觉得要拆点 样例行不通不知道这句话干啥用的。Further, the company cannot place the two chemicals in same depot (for any length of time) without special storage handling

一个点只能用一次??

忽略这句话就直接费用流 此题类似dijkstra,dijkstra那道

  1. #include <map>
  2. #include <set>
  3. #include <list>
  4. #include <cmath>
  5. #include <ctime>
  6. #include <deque>
  7. #include <stack>
  8. #include <queue>
  9. #include <cctype>
  10. #include <cstdio>
  11. #include <string>
  12. #include <vector>
  13. #include <climits>
  14. #include <cstdlib>
  15. #include <cstring>
  16. #include <iostream>
  17. #include <algorithm>
  18. #define LL long long
  19. #define PI 3.1415926535897932626
  20. using namespace std;
  21. int gcd(int a, int b) {return a % b == ? b : gcd(b, a % b);}
  22. #define MAXN 140
  23. const int INF = 0x3f3f3f3f ;
  24. int N,M;
  25. queue<int>q;
  26. struct node
  27. {
  28. int u,v,next;
  29. int flow,cap,cost;
  30. }edge[MAXN * MAXN * ];
  31. int cnt,src,tag;
  32. int C,F;
  33. bool inq[MAXN];int d[MAXN];
  34. int head[MAXN],p[MAXN];
  35. void add(int u,int v,int cap,int cost)
  36. {
  37. edge[cnt].u = u;
  38. edge[cnt].v = v;
  39. edge[cnt].cap = cap;
  40. edge[cnt].flow = ;
  41. edge[cnt].cost = cost;
  42. edge[cnt].next = head[u];
  43. head[u] = cnt++;
  44. //反向
  45. edge[cnt].v = u;
  46. edge[cnt].u = v;
  47. edge[cnt].flow = ;
  48. edge[cnt].cap = ;
  49. edge[cnt].cost = - cost;
  50. edge[cnt].next = head[v];
  51. head[v] = cnt++;
  52. }
  53. void read()
  54. {
  55. cnt = ;
  56. memset(head,-,sizeof(head));
  57. src = ; tag = N + ;
  58. add(src,,,);
  59. //for (int i = 1; i <= N; i++) add(i,i + N,1,0);
  60. for (int i = ; i <= M; i++)
  61. {
  62. int u ,v ,w;
  63. scanf("%d%d%d",&u,&v,&w);
  64. u++;v++;
  65. //u的后向点 链接 v 的前向点
  66. add(u,v,,w);
  67. }
  68. add(N,tag,,);
  69. }
  70. bool SPFA(int s, int t)
  71. {
  72. while (!q.empty()) q.pop();
  73. memset(inq,false,sizeof(inq));
  74. memset(d,0x3f,sizeof(d));
  75. memset(p,-,sizeof(p));
  76. d[s] = ;
  77. q.push(s);
  78. inq[s] = true;
  79. while (!q.empty())
  80. {
  81. int u = q.front(); q.pop();
  82. inq[u] = false;
  83. for (int i = head[u]; i != -; i = edge[i].next)
  84. {
  85. int v = edge[i].v;
  86. if (d[v] > d[u] + edge[i].cost && edge[i].cap > edge[i].flow)
  87. {
  88. d[v] = d[u] + edge[i].cost;
  89. p[v] = i;
  90. if (!inq[v])
  91. {
  92. q.push(v);
  93. inq[v] = true;
  94. }
  95. }
  96. }
  97. }
  98. return d[tag] != INF;
  99. }
  100. void slove()
  101. {
  102. C = F = ;
  103. while(SPFA(src,tag))
  104. {
  105. int a = INF;
  106. for (int i = p[tag]; i != -; i = p[edge[i].u])
  107. a = min(a,edge[i].cap - edge[i].flow);
  108. for (int i = p[tag]; i != -; i = p[edge[i].u])
  109. {
  110. edge[i].flow += a;
  111. edge[i ^ ].flow -= a;
  112. }
  113. C += d[tag] * a;
  114. F += a;
  115. }
  116. }
  117. int main()
  118. {
  119. //freopen("sample.txt","r",stdin);
  120. int kase = ;
  121. while (scanf("%d%d",&N,&M) != EOF)
  122. {
  123. if (N == && M == ) break;
  124. read();
  125. slove();
  126. if (F < ) printf("Instance #%d: Not possible\n",kase++);
  127. else printf("Instance #%d: %d\n",kase++,C);
  128. }
  129. return ;
  130. }

UVALIVE 2927 "Shortest" pair of paths的更多相关文章

  1. UVALive - 2927 "Shortest" pair of paths(最小费用最大流)题解

    题意:有n个机器,机器之间有m条连线,我们需要判断机器0到n-1是否存在两条线路,存在输出最小费用. 思路:我们把0连接超级源点,n-1连接超级汇点,两者流量都设为2,其他流量设为1,那么只要最后我们 ...

  2. 2018.06.27"Shortest" pair of paths(费用流)

    "Shortest" pair of paths Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 1589 A ...

  3. POJ3068 "Shortest" pair of paths 【费用流】

    POJ3068 "Shortest" pair of paths Description A chemical company has an unusual shortest pa ...

  4. poj 3068 "Shortest" pair of paths

    "Shortest" pair of paths Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 1407 ...

  5. "Shortest" pair of paths[题解]

    "Shortest" pair of paths 题目大意 给出 \(n\) 个点,\(m\) 条边,除第一个点和最后一个点外,其他所有的点都只能被经过一次,要求找到两条从第一个点 ...

  6. POJ3068 "Shortest" pair of paths

    嘟嘟嘟 题目大意:一个有向图,每一条边有一个边权,求从节点\(0\)到\(n - 1\)的两条不经过同一条边的路径,并且边权和最小. 费用流板子题. 发个博客证明一下我写了这题. #include&l ...

  7. POJ3068:"Shortest" pair of paths——题解

    http://poj.org/problem?id=3068 题目大意: 从0-n-1找到两条边和点都不相同(除了0和n-1外)的最小费用路径. ——————————————————————————— ...

  8. POJ 3068 "Shortest" pair of paths(费用流)

    [题目链接] http://poj.org/problem?id=3068 [题目大意] 给出一张图,要把两个物品从起点运到终点,他们不能运同一条路过 每条路都有一定的费用,求最小费用 [题解] 题目 ...

  9. [poj] 3068 "Shortest" pair of paths || 最小费用最大流

    [原题](http://poj.org/problem?id=3068) 给一个有向带权图,求两条从0-N-1的路径,使它们没有公共点且边权和最小 . //是不是像传纸条啊- 是否可行只要判断最后最大 ...

随机推荐

  1. 笔记-DB-mongodb-常用操作-1

    笔记-DB-mongodb-常用操作-1 1.  启动及连接 1.1.  启动 启动mongod windows下: 1.   如已添加服务 net start <service name> ...

  2. 笔记-python-lib-requests常用类/方法/属性

    笔记-python-lib-requests常用类/方法/属性 1.      requests模块常用类/方法/属性 在使用中发现对requests模块不够熟悉,写了几个案例后重新整理了一下文档,罗 ...

  3. JVM——九大工具助你玩转Java性能优化

    本文转载自 http://www.importnew.com/12324.html 本文由 ImportNew - 陈 晓舜 翻译自 idrsolutions.欢迎加入翻译小组.转载请参见文章末尾的要 ...

  4. PHP代码审计6-实战漏洞挖掘-xdcms用户注册页面漏洞

    xdcms 源码:xdcms v2.0.8 1.配置 [一直下一步(仅为测试)] #数据库账号root,密码为空:管理员账号/密码:xdcms/xdcms #登录后台 2.查看后台登录页面的配置项[x ...

  5. KEIL里如何实现仿真 查看输出波形

    1首先打开keil软件 ,点击options 我们选择在debug 2 点击debug 红色的按钮 3 进入调试界面后 ,打开logic analysis窗口 4 打开窗口后 进入setup 4 会弹 ...

  6. HDU 1384 Intervals(差分约束)

    Intervals Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total ...

  7. mybatis和redis整合 log4j打印sql语句

    首先,需要在项目中引进jedis-2.8.1.jar包,在pom.xml里加上 <dependency> <groupId>redis.clients</groupId& ...

  8. 剑指Offer - 九度1351 - 数组中只出现一次的数字

    剑指Offer - 九度1351 - 数组中只出现一次的数字2013-11-23 01:23 题目描述: 一个整型数组里除了两个数字之外,其他的数字都出现了两次.请写程序找出这两个只出现一次的数字. ...

  9. 1102 Invert a Binary Tree (25 分)(二叉树遍历)

    二叉树有N个结点,给出每个结点的左右孩子结点的编号,把二叉树反转(左右孩子交换  所以是后序遍历交换) 输出反转后二叉树的层序遍历和中序遍历 #include<bits/stdc++.h> ...

  10. leetcode 201. 数字范围按位与 解题报告

    给定范围 [m, n],其中 0 <= m <= n <= 2147483647,返回此范围内所有数字的按位与(包含 m, n 两端点). 示例 1: 输入: [5,7] 输出: 4 ...