网络流(最大流) POJ 1637 Sightseeing tour
Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 8628 | Accepted: 3636 |
Description
Input
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 题意:给你一个图,其中既有有向边又有无向边,要你判断图中是否存在欧拉回路。
这题难点就在于讨论无向边的方向。首先,欧拉回路图有个性质:所有点的入度等于出度。然后又发现,对于某点连出去的一条无向边,改变它的方向,这个点的(出度-入度)奇偶性不变。所以先给无向边随意定向,然后判断是否有点的(出度-入度)为奇数,有就绝逼不可能有欧拉回路。
然而到这里还没有完,每个点的(出度-入度)都为偶数并不代表改变那些无向边的方向就可以形成一个欧拉回路图。
现在的问题类似于网络流的分配问题,设一个点的(出度-入度)为d,那么将d大于零的点和d小于零的点分成两个集合,保留原来的无向边,容量为1……具体还是看程序吧。
#include <iostream>
#include <cstring>
#include <cstdio>
#include <queue> using namespace std;
const int INF=;
const int maxn=,maxm=;
int cnt,fir[maxn],nxt[maxm],cap[maxm],to[maxm],dis[maxn],gap[maxn],path[maxn];
int In[maxn],Out[maxn];
void addedge(int a,int b,int c)
{
nxt[++cnt]=fir[a];
to[cnt]=b;
cap[cnt]=c;
fir[a]=cnt;
} bool BFS(int S,int T)
{
memset(dis,,sizeof(dis));
dis[T]=;
queue<int>q;q.push(T);
while(!q.empty())
{
int node=q.front();q.pop();
for(int i=fir[node];i;i=nxt[i])
{
if(dis[to[i]])continue;
dis[to[i]]=dis[node]+;
q.push(to[i]);
}
}
return dis[S];
}
int fron[maxn];
int ISAP(int S,int T)
{
if(!BFS(S,T))
return ;
for(int i=;i<=T;i++)++gap[dis[i]];
int p=S,ret=;
memcpy(fron,fir,sizeof(fir));
while(dis[S]<=T+)
{
if(p==T){
int f=INF;
while(p!=S){
f=min(f,cap[path[p]]);
p=to[path[p]^];
}
p=T;ret+=f;
while(p!=S){
cap[path[p]]-=f;
cap[path[p]^]+=f;
p=to[path[p]^];
}
}
int &ii=fron[p];
for(;ii;ii=nxt[ii]){
if(!cap[ii]||dis[to[ii]]+!=dis[p])
continue;
else
break;
} if(ii){
p=to[ii];
path[p]=ii;
} else{
if(--gap[dis[p]]==)break;
int minn=T+;
for(int i=fir[p];i;i=nxt[i])
if(cap[i])
minn=min(minn,dis[to[i]]);
gap[dis[p]=minn+]++;
fron[p]=fir[p];
if(p!=S)
p=to[path[p]^];
}
}
return ret;
} void Init()
{
memset(fir,,sizeof(fir));
memset(gap,,sizeof(gap));
memset(In,,sizeof(In));
memset(Out,,sizeof(Out));
cnt=;
}
int main()
{
int T,n,m;
scanf("%d",&T);
while(T--)
{
Init();
scanf("%d%d",&n,&m);
for(int i=;i<=m;i++)
{
int u,v,k;
scanf("%d%d%d",&u,&v,&k);
In[v]++;Out[u]++;
if(!k)
addedge(u,v,),addedge(v,u,);
}
int flag=;
for(int i=;i<=n;i++){
int d=Out[i]-In[i];
if(d&){
flag=;
break;
}
if(d>)addedge(,i,d/),addedge(i,,);
if(d<)addedge(i,n+,d/(-)),addedge(n+,i,);
}
if(flag)
ISAP(,n+);
for(int i=fir[];i;i=nxt[i])
if(cap[i])
flag=; if(flag)
puts("possible");
else
puts("impossible");
}
return ;
}
最后感谢邝斌的题解,%%%
网络流(最大流) POJ 1637 Sightseeing tour的更多相关文章
- POJ 1637 Sightseeing tour(最大流)
POJ 1637 Sightseeing tour 题目链接 题意:给一些有向边一些无向边,问能否把无向边定向之后确定一个欧拉回路 思路:这题的模型很的巧妙,转一个http://blog.csdn.n ...
- 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 (SAP | Dinic 混合欧拉图的判断)
Sightseeing tour Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 6448 Accepted: 2654 ...
- POJ 1637 Sightseeing tour
Sightseeing tour Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 9276 Accepted: 3924 ...
- POJ 1637 Sightseeing tour (混合图欧拉回路)
Sightseeing tour Description The city executive board in Lund wants to construct a sightseeing tou ...
- [POJ 1637] Sightseeing tour(网络流)
题意 (混合图的欧拉回路判定) 给你一个既存在有向边, 又存在无向边的图. 问是否存在欧拉回路. \(N ≤ 200, M ≤ 1000\) 题解 难点在于无向边. 考虑每个点的度数限制. 我们先对无 ...
- POJ 1637 Sightseeing tour(混合图欧拉回路+最大流)
http://poj.org/problem?id=1637 题意:给出n个点和m条边,这些边有些是单向边,有些是双向边,判断是否能构成欧拉回路. 思路: 构成有向图欧拉回路的要求是入度=出度,无向图 ...
- poj 1637 Sightseeing tour——最大流+欧拉回路
题目:http://poj.org/problem?id=1637 先给无向边随便定向,如果一个点的入度大于出度,就从源点向它连 ( 入度 - 出度 / 2 ) 容量的边,意为需要流出去这么多:流出去 ...
随机推荐
- css 图片平铺
背景图尺寸(数值表示方式): #background-size{ background-size:200px 100px; } 背景图尺寸(百分比表示方式): #background-size2{ b ...
- Java_Activiti5_菜鸟也来学Activiti5工作流_之初识BPMN2.0的简单结构(五)
<?xml version="1.0" encoding="UTF-8"?> <definitions xmlns="http:// ...
- 测测你适合从事Web前端开发吗
一般初创的互联网公司最烧钱的时候往往都是刚刚获得风投或融资的时候,因为他们要把钱砸向前端,因为那时候没有客户访问,对于企业来说只有先做好前端技 术.做好客户体验一切才有可能.用户体验做好,才有人访问, ...
- WCF,WebAPI,WCFREST和WebService的区别
Web ServiceIt is based on SOAP and return data in XML form.It support only HTTP protocol.It is not o ...
- 数据库连接报错之IO异常(The Network Adapter could not establish the connection)
Io 异常: The Network Adapter could not establish the connection 有以下四个原因: 1.oracle配置 listener.ora 和tnsn ...
- JSP中用include标签动态引入其它文件报错
<jsp:include page="<%=path %>/include.jsp"></jsp:include> 报错:attribute f ...
- html良好结构-之豆瓣风格
良好HTML结构 1 结构层次 unit> hd+ unit-wrap>section>item2 语议化的结构 html5 html 语议 ck-box-unit ck-hd-wr ...
- ubantu下重启apache
启动apache服务 sudo /etc/init.d/apache2 start重启apache服务sudo /etc/init.d/apache2 restart停止apache服务 sudo / ...
- javascript 设为首页 | 加入收藏夹 JS代码
我们介绍一个可兼容所有浏览器的加入收藏代码代码,大概原理是这样的我们根据获取用户navigator.userAgent.toLowerCase()信息来判断浏览器,根据浏览器是否支持加入收藏js命令, ...
- redis-消息订阅
使用办法: 订阅端: Subscribe 频道名称 发布端: publish 频道名称发布内容 客户端例子: redis > subscribe news Reading messages... ...