POJ 1637 Sightseeing tour (SAP | Dinic 混合欧拉图的判断)
|
Sightseeing tour
Description The city executive board in Lund wants to construct a sightseeing tour by bus in Lund, so that tourists can see every corner of the beautiful city. They want to construct the tour so that every street in the city is visited exactly 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 On the first line of the input is a single positive integer n, telling the number of test scenarios to follow. Each scenario begins with a line containing two positive integers m and s, 1 <= m <= 200,1 <= s <= 1000 being the number 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 For each scenario, output one line containing the text "possible" or "impossible", whether or not it's possible to construct a sightseeing tour.
Sample Input 4 Sample Output possible Source |
给出一张混合图(有有向边,也有无向边),判断是否存在欧拉回路。
首先是对图中的无向边随意定一个方向,然后统计每个点的入度(indeg)和出度(outdeg),如果(indeg - outdeg)是奇数的话,一定不存在欧拉回路;
如果所有点的入度和出度之差都是偶数,那么就开始网络流构图:
1,对于有向边,舍弃;对于无向边,就按照最开始指定的方向建权值为 1 的边;
2,对于入度小于出度的点,从源点连一条到它的边,权值为(outdeg - indeg)/2;出度小于入度的点,连一条它到汇点的权值为(indeg - outdeg)/2 的边;
构图完成,如果满流(求出的最大流值 == 和汇点所有连边的权值之和),那么存在欧拉回路,否则不存在。
另附一个讲解欧拉图不错的博客:http://www.cnblogs.com/destinydesigner/archive/2009/09/28/1575674.html
SAP果然快,0ms:
#include<iostream>
#include<cstdio>
#include<cstring>
#include<queue> using namespace std; const int VM=;
const int EM=;
const int INF=0x3f3f3f3f; struct Edge{
int to,nxt;
int cap;
}edge[EM<<]; int n,m,cnt,head[VM];
int src,des,tot,sum,indeg[VM],outdeg[VM];
int dep[VM],gap[VM],cur[VM],aug[VM],pre[VM]; void addedge(int cu,int cv,int cw){
edge[cnt].to=cv; edge[cnt].cap=cw; edge[cnt].nxt=head[cu];
head[cu]=cnt++;
edge[cnt].to=cu; edge[cnt].cap=; edge[cnt].nxt=head[cv];
head[cv]=cnt++;
} int SAP(int n){
int max_flow=,u=src,v;
int id,mindep;
aug[src]=INF;
pre[src]=-;
memset(dep,,sizeof(dep));
memset(gap,,sizeof(gap));
gap[]=n;
for(int i=;i<=n;i++)
cur[i]=head[i]; // 初始化当前弧为第一条弧
while(dep[src]<n){
int flag=;
if(u==des){
max_flow+=aug[des];
for(v=pre[des];v!=-;v=pre[v]){ // 路径回溯更新残留网络
id=cur[v];
edge[id].cap-=aug[des];
edge[id^].cap+=aug[des];
aug[v]-=aug[des]; // 修改可增广量,以后会用到
if(edge[id].cap==) // 不回退到源点,仅回退到容量为0的弧的弧尾
u=v;
}
}
for(int i=cur[u];i!=-;i=edge[i].nxt){
v=edge[i].to; // 从当前弧开始查找允许弧
if(edge[i].cap> && dep[u]==dep[v]+){ // 找到允许弧
flag=;
pre[v]=u;
cur[u]=i;
aug[v]=min(aug[u],edge[i].cap);
u=v;
break;
}
}
if(!flag){
if(--gap[dep[u]]==) /* gap优化,层次树出现断层则结束算法 */
break;
mindep=n;
cur[u]=head[u];
for(int i=head[u];i!=-;i=edge[i].nxt){
v=edge[i].to;
if(edge[i].cap> && dep[v]<mindep){
mindep=dep[v];
cur[u]=i; // 修改标号的同时修改当前弧
}
}
dep[u]=mindep+;
gap[dep[u]]++;
if(u!=src) // 回溯继续寻找允许弧
u=pre[u];
}
}
return max_flow;
} void Init(){
cnt=;
memset(head,-,sizeof(head));
memset(indeg,,sizeof(indeg));
memset(outdeg,,sizeof(outdeg));
} int main(){ //freopen("input.txt","r",stdin); int t;
scanf("%d",&t);
while(t--){
Init();
scanf("%d%d",&n,&m);
int u,v,c;
for(int i=;i<m;i++){
scanf("%d%d%d",&u,&v,&c);
indeg[v]++;
outdeg[u]++;
if(c==)
addedge(u,v,);
} int flag=;
for(int i=;i<=n;i++)
if((indeg[i]-outdeg[i])%==){
flag=;
break;
}
if(!flag)
puts("impossible");
else{
sum=;
src=, des=n+;
for(int i=;i<=n;i++){ //无向边建图,有向边舍弃
if(indeg[i]<outdeg[i])
addedge(src,i,(outdeg[i]-indeg[i])/);
else if(indeg[i]>outdeg[i]){
addedge(i,des,(indeg[i]-outdeg[i])/);
sum+=(indeg[i]-outdeg[i])/;
}
}
int ans=SAP(des+);
if(sum==ans)
puts("possible");
else
puts("impossible");
}
}
return ;
}
#include<iostream>
#include<cstdio>
#include<cstring>
#include<queue> using namespace std; const int VM=;
const int EM=;
const int INF=0x3f3f3f3f; struct Edge{
int u,v,nxt;
int cap;
}edge[EM<<]; int n,m,cnt,head[VM];
int src,des,tot,sum,dep[VM],indeg[VM],outdeg[VM]; void addedge(int cu,int cv,int cw){
edge[cnt].u=cu; edge[cnt].v=cv; edge[cnt].cap=cw;
edge[cnt].nxt=head[cu]; head[cu]=cnt++;
edge[cnt].u=cv; edge[cnt].v=cu; edge[cnt].cap=;
edge[cnt].nxt=head[cv]; head[cv]=cnt++;
} void Init(){
cnt=;
memset(head,-,sizeof(head));
memset(indeg,,sizeof(indeg));
memset(outdeg,,sizeof(outdeg));
} int BFS(){
queue<int> q;
while(!q.empty())
q.pop();
memset(dep,-,sizeof(dep));
dep[src]=;
q.push(src);
while(!q.empty()){
int u=q.front();
q.pop();
for(int i=head[u];i!=-;i=edge[i].nxt){
int v=edge[i].v;
if(edge[i].cap> && dep[v]==-){ //没有标记,且可行流大于0
dep[v]=dep[u]+;
q.push(v);
}
}
}
return dep[des]!=-; //汇点是否成功标号,也就是说是否找到增广路
} int DFS(int u,int minx){
if(u==des)
return minx;
int tmp;
for(int i=head[u];i!=-;i=edge[i].nxt){
int v=edge[i].v;
if(edge[i].cap> && dep[v]==dep[u]+ && (tmp=DFS(v,min(minx,edge[i].cap)))){
edge[i].cap-=tmp;
edge[i^].cap+=tmp;
return tmp;
}
}
dep[u]=-;
return ;
} int Dinic(){
int ans=,tmp;
while(BFS()){
while(){
tmp=DFS(src,INF);
if(tmp==)
break;
ans+=tmp;
}
}
return ans;
} int main(){ //freopen("input.txt","r",stdin); int t;
scanf("%d",&t);
while(t--){
Init();
scanf("%d%d",&n,&m);
int u,v,c;
for(int i=;i<m;i++){
scanf("%d%d%d",&u,&v,&c);
indeg[v]++;
outdeg[u]++;
if(c==)
addedge(u,v,);
} int flag=;
for(int i=;i<=n;i++)
if((indeg[i]-outdeg[i])%==){
flag=;
break;
}
if(!flag)
puts("impossible");
else{
sum=;
src=, des=n+;
for(int i=;i<=n;i++){ //无向边建图,有向边舍弃
if(indeg[i]<outdeg[i])
addedge(src,i,(outdeg[i]-indeg[i])/);
else if(indeg[i]>outdeg[i]){
addedge(i,des,(indeg[i]-outdeg[i])/);
sum+=(indeg[i]-outdeg[i])/;
}
}
int ans=Dinic();
if(sum==ans)
puts("possible");
else
puts("impossible");
}
}
return ;
}
POJ 1637 Sightseeing tour (SAP | Dinic 混合欧拉图的判断)的更多相关文章
- POJ 1637 Sightseeing tour(最大流)
POJ 1637 Sightseeing tour 题目链接 题意:给一些有向边一些无向边,问能否把无向边定向之后确定一个欧拉回路 思路:这题的模型很的巧妙,转一个http://blog.csdn.n ...
- POJ 1637 Sightseeing tour (混合图欧拉路判定)
Sightseeing tour Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 6986 Accepted: 2901 ...
- POJ 1637 - Sightseeing tour - [最大流解决混合图欧拉回路]
嗯,这是我上一篇文章说的那本宝典的第二题,我只想说,真TM是本宝典……做的我又痛苦又激动……(我感觉ACM的日常尽在这张表情中了) 题目链接:http://poj.org/problem?id=163 ...
- POJ 1637 Sightseeing tour (混合图欧拉回路)
Sightseeing tour Description The city executive board in Lund wants to construct a sightseeing tou ...
- POJ 1637 Sightseeing tour
Sightseeing tour Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 9276 Accepted: 3924 ...
- 网络流(最大流) POJ 1637 Sightseeing tour
Sightseeing tour Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 8628 Accepted: 3636 ...
- POJ 1637 Sightseeing tour(混合图欧拉回路+最大流)
http://poj.org/problem?id=1637 题意:给出n个点和m条边,这些边有些是单向边,有些是双向边,判断是否能构成欧拉回路. 思路: 构成有向图欧拉回路的要求是入度=出度,无向图 ...
- POJ 1637 Sightseeing tour ★混合图欧拉回路
[题目大意]混合图欧拉回路(1 <= N <= 200, 1 <= M <= 1000) [建模方法] 把该图的无向边随便定向,计算每个点的入度和出度.如果有某个点出入度之差为 ...
- poj 1637 Sightseeing tour 混合图欧拉回路 最大流 建图
题目链接 题意 给定一个混合图,里面既有有向边也有无向边.问该图中是否存在一条路径,经过每条边恰好一次. 思路 从欧拉回路说起 首先回顾有向图欧拉回路的充要条件:\(\forall v\in G, d ...
随机推荐
- 转:centos7 安装与使用 postgreSQL
一. 安装与基本说明都非常的详细. https://www.digitalocean.com/community/tutorials/how-to-install-and-use-postgresql ...
- HTTP认证与https简介
HTTP请求报头: Authorization HTTP响应报头: WWW-Authenticate HTTP认证是基于质询/回应(challenge/response)的认证模式. HTTP认证 ...
- (转)C/C++ 程序设计员应聘常见 面试笔试 试题深入剖析
C/C++ 程序设计员应聘常见 面试笔试 试题深入剖析 http://www.nowcoder.com/discuss/1826?type=2&order=0&pos=23&p ...
- Horizon Is Easy, Horizon Is Complex
本文出自我的同事兼基友@monsterxx03 之手,本人稍作润色 Horizon Is Easy, Horizon Is Complex 如果要用一句话来概括Openstack Dashboard项 ...
- 牛客网-《剑指offer》-从尾到头打印链表
题目:http://www.nowcoder.com/practice/d0267f7f55b3412ba93bd35cfa8e8035 C++ /** * struct ListNode { * i ...
- ZH奶酪:PHP 执行时间Fatal error: Maximum execution time of...
来源:http://stackoverflow.com/questions/5164930/fatal-error-maximum-execution-time-of-30-seconds-excee ...
- Java中创建访问HTTPS的自签名证书的方法
一.问题: 常常在用java访问https的请求时,总是出现SSL禁止的异常.这里给大家教下怎么创建与添加证书. 二.工具 : 1.创建一个目录 2.去Github上搜索InstallCert,然后随 ...
- JavaScript正则式练习
使用正则式匹配第一个数字和最后一个数字,使用环视 str2 = 09051 : Fast Food Restaurants - Concession Stands/Snack Bars Delicat ...
- SQL Server查看被锁的表 - dead lock
select str(request_session_id ,4 ,0) as SPID ,convert(varchar(20) ,db_name(resource_database_id)) as ...
- 修改Ubuntu默认运行级别,启动字符界面
Ubuntu的默认开机的runlevel是2,可以用runlevel来查看当前的默认运行级别. debian系(ubuntu是基于debian)的Linux一直是用runlevel 2来默认启动,并且 ...