B - Sightseeing tour POJ - 1637

https://blog.csdn.net/qq_36551189/article/details/80905345

首先要了解一下欧拉回路的基本思路。

欧拉回路:如果是无向图,那么每一个点连的边的数量为偶数,如果是有向图,那么每一个点的入度要等于出度。

欧拉路径:这个欧拉路径是没有成环的,如果是无向图,那么除了两个点连的边是奇数,其他都是偶数,

如果是有向图,那么除了有一个点入度比出度大1,有一个点的出度比入度大1 ,其他都是入度等于出度。

这个题目的基本思路就涉及到了欧拉回路。

这个地方难处理的就是有无向和有向边的混合,这个无向很难处理,但是这个无向最后都要转化成有向。

根据欧拉回路的一些基本性质我们可以知道,有向图每一个点的入度要等于出度。

所以我们可以先给无向图随意定一个方向然后我们用 d=出度-入度 因为我们随意改变一条边的方向这个d的变化量为2

所以就说明之后改变边的方向并不会改变改变这个d的奇偶性。

根据欧拉回路我们就可以知道我们需要的是这个d==0

这个时候就需要用到最大流,怎么用最大流解决这个问题呢,

就是把d大于0的部分和源点相连,因为d大于0如果是欧拉回路那么就肯定是由其他边d小于0,

其他边d<0说明出度小于入度,也就是说有点的入度会小于出度,就是说在任意给定边的时候有点把边连到了这个d<0的点上面,

说到这里其实这个图就建的差不多了。

  1. #include <cstdio>
  2. #include <cstring>
  3. #include <cstdlib>
  4. #include <queue>
  5. #include <vector>
  6. #include <algorithm>
  7. #include <cstring>
  8. #include <iostream>
  9. #define inf 0x3f3f3f3f
  10. using namespace std;
  11. typedef long long ll;
  12. const int maxn = 1e5 + ;
  13. const int INF = 0x3f3f3f3f;
  14. struct edge {
  15. int u, v, c, f;
  16. edge(int u, int v, int c, int f) :u(u), v(v), c(c), f(f) {}
  17. };
  18. vector<edge>e;
  19. vector<int>G[maxn];
  20. int level[maxn];//BFS分层,表示每个点的层数
  21. int iter[maxn];//当前弧优化
  22. int m;
  23. void init(int n) {
  24. for (int i = ; i <= n; i++)G[i].clear();
  25. e.clear();
  26. }
  27. void addedge(int u, int v, int c) {
  28. e.push_back(edge(u, v, c, ));
  29. e.push_back(edge(v, u, , ));
  30. m = e.size();
  31. G[u].push_back(m - );
  32. G[v].push_back(m - );
  33. }
  34. void BFS(int s)//预处理出level数组
  35. //直接BFS到每个点
  36. {
  37. memset(level, -, sizeof(level));
  38. queue<int>q;
  39. level[s] = ;
  40. q.push(s);
  41. while (!q.empty()) {
  42. int u = q.front();
  43. q.pop();
  44. for (int v = ; v < G[u].size(); v++) {
  45. edge& now = e[G[u][v]];
  46. if (now.c > now.f && level[now.v] < ) {
  47. level[now.v] = level[u] + ;
  48. q.push(now.v);
  49. }
  50. }
  51. }
  52. }
  53. int dfs(int u, int t, int f)//DFS寻找增广路
  54. {
  55. if (u == t)return f;//已经到达源点,返回流量f
  56. for (int &v = iter[u]; v < G[u].size(); v++)
  57. //这里用iter数组表示每个点目前的弧,这是为了防止在一次寻找增广路的时候,对一些边多次遍历
  58. //在每次找增广路的时候,数组要清空
  59. {
  60. edge &now = e[G[u][v]];
  61. if (now.c - now.f > && level[u] < level[now.v])
  62. //now.c - now.f > 0表示这条路还未满
  63. //level[u] < level[now.v]表示这条路是最短路,一定到达下一层,这就是Dinic算法的思想
  64. {
  65. int d = dfs(now.v, t, min(f, now.c - now.f));
  66. if (d > ) {
  67. now.f += d;//正向边流量加d
  68. e[G[u][v] ^ ].f -= d;
  69. //反向边减d,此处在存储边的时候两条反向边可以通过^操作直接找到
  70. return d;
  71. }
  72. }
  73. }
  74. return ;
  75. }
  76. int Maxflow(int s, int t) {
  77. int flow = ;
  78. for (;;) {
  79. BFS(s);
  80. if (level[t] < )return flow;//残余网络中到达不了t,增广路不存在
  81. memset(iter, , sizeof(iter));//清空当前弧数组
  82. int f;//记录增广路的可增加的流量
  83. while ((f = dfs(s, t, INF)) > ) {
  84. flow += f;
  85. }
  86. }
  87. return flow;
  88. }
  89. int in[maxn], out[maxn];
  90.  
  91. int main()
  92. {
  93. int k;
  94. scanf("%d", &k);
  95. while(k--)
  96. {
  97. int n, m;
  98. scanf("%d%d", &n, &m);
  99. init(n + m);
  100. memset(in, , sizeof(in));
  101. memset(out, , sizeof(out));
  102. int s = , t = n + ;
  103. for(int i=;i<=m;i++)
  104. {
  105. int u, v, w;
  106. scanf("%d%d%d", &u, &v, &w);
  107. out[u]++; in[v]++;
  108. if (w == ) addedge(u, v, );
  109. }
  110. bool flag = false;
  111. for(int i=;i<=n;i++)
  112. {
  113. if ((out[i] - in[i]) & ) flag = true;
  114. else if (out[i] > in[i]) addedge(s, i, (out[i] - in[i]) / );
  115. else if (in[i] > out[i]) addedge(i, t, (in[i] - out[i]) / );
  116. }
  117. if (flag) {
  118. printf("impossible\n");
  119. continue;
  120. }
  121. int ans = Maxflow(s, t);
  122. for(int i=;i<G[].size();i++)
  123. {
  124. edge now = e[G[][i]];
  125. if (now.c != now.f) flag = true;
  126. }
  127. if (flag) printf("impossible\n");
  128. else printf("possible\n");
  129. }
  130. return ;
  131. }

欧拉回路

网络流 + 欧拉回路 = B - Sightseeing tour POJ - 1637的更多相关文章

  1. POJ 1637 - Sightseeing tour - [最大流解决混合图欧拉回路]

    嗯,这是我上一篇文章说的那本宝典的第二题,我只想说,真TM是本宝典……做的我又痛苦又激动……(我感觉ACM的日常尽在这张表情中了) 题目链接:http://poj.org/problem?id=163 ...

  2. 网络流(最大流) POJ 1637 Sightseeing tour

    Sightseeing tour Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 8628   Accepted: 3636 ...

  3. POJ 1637 Sightseeing tour (混合图欧拉回路)

    Sightseeing tour   Description The city executive board in Lund wants to construct a sightseeing tou ...

  4. POJ 1637 Sightseeing tour (混合图欧拉路判定)

    Sightseeing tour Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 6986   Accepted: 2901 ...

  5. POJ 1637 Sightseeing tour(最大流)

    POJ 1637 Sightseeing tour 题目链接 题意:给一些有向边一些无向边,问能否把无向边定向之后确定一个欧拉回路 思路:这题的模型很的巧妙,转一个http://blog.csdn.n ...

  6. POJ1637 Sightseeing tour (混合图欧拉回路)(网络流)

                                                                Sightseeing tour Time Limit: 1000MS   Me ...

  7. TZOJ 2099 Sightseeing tour(网络流判混合图欧拉回路)

    描述 The city executive board in Lund wants to construct a sightseeing tour by bus in Lund, so that to ...

  8. POJ 1637 Sightseeing tour (SAP | Dinic 混合欧拉图的判断)

    Sightseeing tour Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 6448   Accepted: 2654 ...

  9. POJ 1637 Sightseeing tour

    Sightseeing tour Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 9276   Accepted: 3924 ...

随机推荐

  1. AJ学IOS 之微博项目实战(13)发送微博调用相机里面的图片以及调用相机

    AJ分享,必须精品 一:效果 二:代码 相机部分就简单多了,几行代码调用而已,但是如果你要是想实现更多丰富的功能,需要自己写.利用AssetsLibrary.framework,利用这个框架可以获得手 ...

  2. AJ学IOS(43)之网易彩票底部自定义TabBar实现切换

    AJ分享,必须精品 效果: 代码: NYTabBarController // // NYTabBarController.m // 彩票lottery // // Created by apple ...

  3. python初学(二)

    1.输入一个整数列表L,判断L中是否存在相同的数字: (1)若存在,输出YES,否则输出NO: (2)若存在,输出YES,同时输出相同的数字:否则输出NO. l=list(input()) print ...

  4. Thinking in Java,Fourth Edition(Java 编程思想,第四版)学习笔记(一)之Introduction

    Learn Java I found out that I and other speakers tended to give the typical audience too many topics ...

  5. threejs创建地球

    上个月底,在朋友圈看到一个号称“这可能是地球上最美的h5”的分享,点进入后发现这个h5还很别致,思考了一会,决定要不高仿一个? 到今天为止,高仿基本完成, 线上地址 github地址 除了手机端的me ...

  6. D - Fox And Two Dots DFS

    Fox Ciel is playing a mobile puzzle game called "Two Dots". The basic levels are played on ...

  7. RabbitMQ 消息队列入门

    文档 入门 主要的内容:one two three four five six seven 前言 中间件 消息队列 异步处理,注册完发短信 应用解耦,订单接口调用扣库存接口,失败了怎么办? 流量削峰, ...

  8. selenium 获取页面<input>标签的个数和各个属性的值

    获取页面某个标签的数量.id.name.class的值,来辅助定位         List<WebElement> lw =driver.findElements(By.tagName( ...

  9. 【5min+】为你的.NET应用进行一次全方位体检

    系列介绍 [五分钟的dotnet]是一个利用您的碎片化时间来学习和丰富.net知识的博文系列.它所包含了.net体系中可能会涉及到的方方面面,比如C#的小细节,AspnetCore,微服务中的.net ...

  10. sql查询慢 查找

    SELECT creation_time N'语句编译时间' ,last_execution_time N'上次执行时间' ,total_physical_reads N'物理读取总次数' ,tota ...