poj--1637--Sightseeing tour(网络流,最大流判断混合图是否存在欧拉图)
| Time Limit: 1000MS | Memory Limit: 10000KB | 64bit IO Format: %I64d & %I64u |
Description
once. The bus should also start and end at the same junction. As in any city, the streets are either one-way or two-way, traffic rules that must be obeyed by the tour bus. Help the executive board and determine if it's possible to construct a sightseeing tour
under these constraints.
Input
of junctions and streets, respectively. The following s lines contain the streets. Each street is described with three integers, xi, yi, and di, 1 <= xi,yi <= m, 0 <= di <= 1, where xi and yi are the junctions connected by a street. If di=1, then the street
is a one-way street (going from xi to yi), otherwise it's a two-way street. You may assume that there exists a junction from where all other junctions can be reached.
Output
Sample Input
4
5 8
2 1 0
1 3 0
4 1 1
1 5 0
5 4 1
3 4 0
4 2 1
2 2 0
4 4
1 2 1
2 3 0
3 4 0
1 4 1
3 3
1 2 0
2 3 0
3 2 0
3 4
1 2 0
2 3 1
1 2 0
3 2 0
Sample Output
possible
impossible
impossible
possible 根据欧拉图的性质,如果一个图是欧拉图,那么每一个点的入度出度一定相等,如果之和就是偶数,入度出度只差也应该是偶数,如果差不是偶数的话,那么一定不是欧拉图
差k是偶数的话就可以通过最大流对无向边确定方向,只需要将多出来的度数k/2变换方向,就可使将图变成欧拉图,通过最大流实现多余的度数重新分配,最后判断是否满流,
如果满流欧拉图就可以建成 【建模方法】
把该图的无向边随便定向,计算每个点的入度和出度。如果有某个点出入度
之差为奇数,那么肯定不存在欧拉回路。因为欧拉回路要求每点入度 = 出度,
也就是总度数为偶数,存在奇数度点必不能有欧拉回路。
好了,现在每个点入度和出度之差均为偶数。那么将这个偶数除以 2,得 x。
也就是说,对于每一个点,只要将 x 条边改变方向(入>出就是变入,出>入就是
9
变出),就能保证出=入。如果每个点都是出=入,那么很明显,该图就存在欧拉
回路。
现在的问题就变成了:我该改变哪些边,可以让每个点出=入?构造网络流
模型。首先,有向边是不能改变方向的,要之无用,删。一开始不是把无向边定
向了吗?定的是什么向,就把网络构建成什么样,边长容量上限 1。另新建 s 和
t。对于入>出的点 u,连接边(u, t)、容量为 x,对于出>入的点 v,连接边(s, v),
容量为 x(注意对不同的点 x 不同)。之后,察看是否有满流的分配。有就是能
有欧拉回路,没有就是没有。欧拉回路是哪个?察看流值分配,将所有流量非 0
(上限是 1,流值不是 0 就是 1)的边反向,就能得到每点入度=出度的欧拉图。
由于是满流,所以每个入>出的点,都有 x 条边进来,将这些进来的边反向,
OK,入=出了。对于出>入的点亦然。那么,没和 s、t 连接的点怎么办?和 s 连
接的条件是出>入,和 t 连接的条件是入>出,那么这个既没和 s 也没和 t 连接的
点,自然早在开始就已经满足入=出了。那么在网络流过程中,这些点属于“中
间点”。我们知道中间点流量不允许有累积的,这样,进去多少就出来多少,反
向之后,自然仍保持平衡。
所以,就这样,混合图欧拉回路问题,解了。#include<stdio.h>
#include<string.h>
#include<queue>
#include<stack>
#include<vector>
#include<algorithm>
using namespace std;
#define MAXN 400
#define MAXM 50000
#define INF 10000000+10
int dis[MAXN],vis[MAXN],head[MAXN],in[MAXN],out[MAXN];
int cur[MAXN],m,n,top,sum;
bool flog;
struct node
{
int u,v,cap,flow,next;
}edge[MAXM];
void init()
{
top=0;
memset(head,-1,sizeof(head));
}
void add(int a,int b,int c)
{
node E1={a,b,c,0,head[a]};
edge[top]=E1;
head[a]=top++;
node E2={b,a,0,0,head[b]};
edge[top]=E2;
head[b]=top++;
}
void getmap()
{
init();
scanf("%d%d",&n,&m);
memset(in,0,sizeof(in));
memset(out,0,sizeof(out));
for(int i=1;i<=m;i++)
{
int a,b,c;
scanf("%d%d%d",&a,&b,&c);
if(a==b) continue;
if(c==0) add(a,b,1);
in[b]++,out[a]++;
}
flog=true;
sum=0;
for(int i=1;i<=n;i++)
{
int k=abs(in[i]-out[i]);
if(k%2)
{
flog=false;
break;
}
k/=2;
if(in[i]<out[i])
add(0,i,k),sum+=k;
else
add(i,n+1,k);
}
}
bool bfs(int s,int e)
{
queue<int>q;
memset(vis,0,sizeof(vis));
memset(dis,-1,sizeof(dis));
q.push(s);
dis[s]=0;
vis[s]=1;
while(!q.empty())
{
int u=q.front();
q.pop();
for(int i=head[u];i!=-1;i=edge[i].next)
{
node E=edge[i];
if(E.cap>E.flow&&!vis[E.v])
{
vis[E.v]=1;
dis[E.v]=dis[E.u]+1;
if(E.v==e)
return true;
q.push(E.v);
}
}
}
return false;
}
int dfs(int x,int a,int e)
{
if(x==e||a==0)
return a;
int flow=0,f;
for(int& i=cur[x];i!=-1;i=edge[i].next)
{
node& E=edge[i];
if(dis[E.v]==dis[x]+1&&(f=dfs(E.v,min(a,E.cap-E.flow),e))>0)
{
E.flow+=f;
edge[i^1].flow-=f;
a-=f;
flow+=f;
if(a==0) break;
}
}
return flow;
}
int MAXflow(int s,int e)
{
int flow=0;
while(bfs(s,e))
{
memcpy(cur,head,sizeof(head));
flow+=dfs(s,INF,e);
}
return flow;
}
void slove()
{
if(!flog)
{
printf("impossible\n");
return ;
}
if(MAXflow(0,n+1)==sum)
printf("possible\n");
else
printf("impossible\n");
}
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
init();
getmap();
slove();
}
return 0;
}
poj--1637--Sightseeing tour(网络流,最大流判断混合图是否存在欧拉图)的更多相关文章
- POJ 1637 Sightseeing tour(最大流)
POJ 1637 Sightseeing tour 题目链接 题意:给一些有向边一些无向边,问能否把无向边定向之后确定一个欧拉回路 思路:这题的模型很的巧妙,转一个http://blog.csdn.n ...
- poj 1637 Sightseeing tour【最大流+欧拉路】
参考:https://www.cnblogs.com/kuangbin/p/3537525.html 这篇讲的挺好的 首先分清欧拉路和欧拉环: 欧拉路:图中经过每条边一次且仅一次的路径,要求只有两个点 ...
- POJ 1637 - Sightseeing tour - [最大流解决混合图欧拉回路]
嗯,这是我上一篇文章说的那本宝典的第二题,我只想说,真TM是本宝典……做的我又痛苦又激动……(我感觉ACM的日常尽在这张表情中了) 题目链接:http://poj.org/problem?id=163 ...
- POJ 1637 Sightseeing tour (混合图欧拉路判定)
Sightseeing tour Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 6986 Accepted: 2901 ...
- 网络流(最大流) POJ 1637 Sightseeing tour
Sightseeing tour Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 8628 Accepted: 3636 ...
- POJ 1637 Sightseeing tour (SAP | Dinic 混合欧拉图的判断)
Sightseeing tour Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 6448 Accepted: 2654 ...
- POJ 1637 Sightseeing tour(混合图欧拉回路+最大流)
http://poj.org/problem?id=1637 题意:给出n个点和m条边,这些边有些是单向边,有些是双向边,判断是否能构成欧拉回路. 思路: 构成有向图欧拉回路的要求是入度=出度,无向图 ...
- [POJ 1637] Sightseeing tour(网络流)
题意 (混合图的欧拉回路判定) 给你一个既存在有向边, 又存在无向边的图. 问是否存在欧拉回路. \(N ≤ 200, M ≤ 1000\) 题解 难点在于无向边. 考虑每个点的度数限制. 我们先对无 ...
- poj 1637 Sightseeing tour 混合图欧拉回路 最大流 建图
题目链接 题意 给定一个混合图,里面既有有向边也有无向边.问该图中是否存在一条路径,经过每条边恰好一次. 思路 从欧拉回路说起 首先回顾有向图欧拉回路的充要条件:\(\forall v\in G, d ...
随机推荐
- WebRTC | Failed to execute 'setLocalDescription' on 'RTCPeerConnection': Failed to parse SessionDescription. a=msid: Missing track ID in msid attribute.
1.问题回放 使用如下代码获取局域网IP报错 (代码来源:https://github.com/diafygi/webrtc-ips 日期:2019-02-16) Uncaught (in promi ...
- 【PRML学习笔记】第四章:分类的线性模型
一.基础概念 线性分类模型:决策面(decision boundary)是输入向量的线性函数 目标类别的表示"1 of K" :$ t = (0,1,0,0,0)^T$ 二.分类问 ...
- c进程学习日志
#include<unistd.h> #include<sys/types.h> #include<pwd.h> #include<stdio.h> i ...
- 制作PC端的安装程序
一个多月不写博客了,不造大家有没有想我,(别自恋了,寥寥无几的粉丝,谁会想你),呜呜~~~ 好了,废话少叙,借用郭德纲老板的话,天儿不早了,干点正事儿吧! 一.序 Unity开发者都知道,打包出来的e ...
- 【转】C#添加修改删除文件文件夹大全
[转]C#添加修改删除文件文件夹大全 C#添加修改删除文件文件夹大全 StreamWriter sw = File.AppendText(Server.MapPath(".")+& ...
- Android自己定义处理崩溃异常
用过安卓手机的用户以及安卓开发人员们会时长碰到程序异常退出的情况.普通用户遇到这样的情况,肯定非常恼火,甚至会骂一生垃圾软件,然后卸载掉.那么开发人员们在开发过程中遇到这样的情况给怎么办呢,当然,你不 ...
- 继续过Hard题目.周五
# Title Editorial Acceptance Difficulty Frequency . 65 Valid Number 12.6% Hard . 126 Word ...
- Beat 'Em Up Game Starter Kit (横版格斗游戏) cocos2d-x游戏源代码
浓缩精华.专注战斗! 游戏的本质是什么?界面?养成?NoNo! 游戏来源于对实战和比赛的模拟,所以它的本源就是对抗.就是战斗! 是挥洒热血的一种方式! 一个游戏最复杂最难做的是什么?UI?商城? ...
- Irrlicht 3D Engine 笔记系列 之 教程6- 2D Graphics
作者:i_dovelemon 日期:2015 / 7 / 1 来源: CSDN 主题:2D Graphics, Irrlicht 教程翻译 本篇教程将要向大家展示怎样使用Irrlicht引擎绘制2D图 ...
- PHP join() 函数
PHP join() 函数 实例 把数组元素组合为一个字符串: <?php $arr = array('Hello','World!','I','love','Shanghai!'); echo ...