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的点上面,

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

#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <queue>
#include <vector>
#include <algorithm>
#include <cstring>
#include <iostream>
#define inf 0x3f3f3f3f
using namespace std;
typedef long long ll;
const int maxn = 1e5 + ;
const int INF = 0x3f3f3f3f;
struct edge {
int u, v, c, f;
edge(int u, int v, int c, int f) :u(u), v(v), c(c), f(f) {}
};
vector<edge>e;
vector<int>G[maxn];
int level[maxn];//BFS分层,表示每个点的层数
int iter[maxn];//当前弧优化
int m;
void init(int n) {
for (int i = ; i <= n; i++)G[i].clear();
e.clear();
}
void addedge(int u, int v, int c) {
e.push_back(edge(u, v, c, ));
e.push_back(edge(v, u, , ));
m = e.size();
G[u].push_back(m - );
G[v].push_back(m - );
}
void BFS(int s)//预处理出level数组
//直接BFS到每个点
{
memset(level, -, sizeof(level));
queue<int>q;
level[s] = ;
q.push(s);
while (!q.empty()) {
int u = q.front();
q.pop();
for (int v = ; v < G[u].size(); v++) {
edge& now = e[G[u][v]];
if (now.c > now.f && level[now.v] < ) {
level[now.v] = level[u] + ;
q.push(now.v);
}
}
}
}
int dfs(int u, int t, int f)//DFS寻找增广路
{
if (u == t)return f;//已经到达源点,返回流量f
for (int &v = iter[u]; v < G[u].size(); v++)
//这里用iter数组表示每个点目前的弧,这是为了防止在一次寻找增广路的时候,对一些边多次遍历
//在每次找增广路的时候,数组要清空
{
edge &now = e[G[u][v]];
if (now.c - now.f > && level[u] < level[now.v])
//now.c - now.f > 0表示这条路还未满
//level[u] < level[now.v]表示这条路是最短路,一定到达下一层,这就是Dinic算法的思想
{
int d = dfs(now.v, t, min(f, now.c - now.f));
if (d > ) {
now.f += d;//正向边流量加d
e[G[u][v] ^ ].f -= d;
//反向边减d,此处在存储边的时候两条反向边可以通过^操作直接找到
return d;
}
}
}
return ;
}
int Maxflow(int s, int t) {
int flow = ;
for (;;) {
BFS(s);
if (level[t] < )return flow;//残余网络中到达不了t,增广路不存在
memset(iter, , sizeof(iter));//清空当前弧数组
int f;//记录增广路的可增加的流量
while ((f = dfs(s, t, INF)) > ) {
flow += f;
}
}
return flow;
}
int in[maxn], out[maxn]; int main()
{
int k;
scanf("%d", &k);
while(k--)
{
int n, m;
scanf("%d%d", &n, &m);
init(n + m);
memset(in, , sizeof(in));
memset(out, , sizeof(out));
int s = , t = n + ;
for(int i=;i<=m;i++)
{
int u, v, w;
scanf("%d%d%d", &u, &v, &w);
out[u]++; in[v]++;
if (w == ) addedge(u, v, );
}
bool flag = false;
for(int i=;i<=n;i++)
{
if ((out[i] - in[i]) & ) flag = true;
else if (out[i] > in[i]) addedge(s, i, (out[i] - in[i]) / );
else if (in[i] > out[i]) addedge(i, t, (in[i] - out[i]) / );
}
if (flag) {
printf("impossible\n");
continue;
}
int ans = Maxflow(s, t);
for(int i=;i<G[].size();i++)
{
edge now = e[G[][i]];
if (now.c != now.f) flag = true;
}
if (flag) printf("impossible\n");
else printf("possible\n");
}
return ;
}

欧拉回路

网络流 + 欧拉回路 = 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. echarts多个数据添加多个纵坐标

    在我们echarts开发中,肯定会遇到一个问题.那就是当有多个数据且数据大小差距太大时,就会出现有些数据小到看不到的情况.所以在遇到这种情况时,我通常的解决办法就是给他多加一个坐标轴. option  ...

  2. ATcoder E - Flatten 质因子分解求LCM

    题解:其实就是求n个数的lcm,由于数据特别大,求lcm时只能用质因子分解的方法来求. 质因子分解求lcm.对n个数每个数都进行质因子分解,然后用一个数组记录某个质因子出现的最大次数.然后累乘pow( ...

  3. CodeForces - 913C (贪心)

    点完菜,他们发现好像觉得少了点什么? 想想马上就要回老家了某不愿透露姓名的林姓学长再次却陷入了沉思......... 他默默的去前台打算点几瓶二锅头. 他发现菜单上有n 种不同毫升的酒. 第 i 种有 ...

  4. vue2.x学习笔记(十七)

    接着前面的内容:https://www.cnblogs.com/yanggb/p/12616847.html. 动态组件&异步组件 在前面学习组件基础的时候学习过动态组件,官方文档给出过一个例 ...

  5. 谈谈MySQL的索引

    目录 索引 前言 是什么 B树 B+树 B树和B+树结构上异同 有什么用 怎么用 索引 前言 总所周知,数据库查询是数据库的最主要功能之一.我们都希望查询数据的速度能尽可能的快.而支撑这一快速的背后就 ...

  6. 在dwr的调用类里获取请求信息

    在dwr的调用类里获取请求的相关信息HttpSession session = WebContextFactory.get().getSession();HttpServletResponse res ...

  7. Nginx安装、多域名访问

    nginx web服务 apache iis django web框架 lvs 负载均衡 章文嵩博士 vue 尤雨溪 Tengine F5 硬件负载 A10 安装 ``` wget http://ng ...

  8. jquery选择时分插件

    <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...

  9. Python 如何实现 单实例

    出处:https://stackoverflow.com/questions/380870/make-sure-only-a-single-instance-of-a-program-is-runni ...

  10. Windows 计划任务 如果选择未登录就运行 则看不到GUI

    You can specify that a task should run even if the account under which the task is scheduled to run ...