POJ 1637 Sightseeing tour 混合欧拉回路
来源:互联网 发布:252工程数据 编辑:程序博客网 时间:2024/05/21 17:34
题意:给出一张图,图中有无向边和有向边。是否存在一条回路,使每条边都只访问一次。
思路:先回忆一下有向图和无向图的欧拉回路和欧拉路径存在的充要条件。
无向图的欧拉路径:整个图为连通图。所有点的度全为偶数,或者只有两个奇数的度,其他都为偶数。
路径为: 如果全为偶数,从任意一点出发,都可以。如果有两个点的度为奇数,那终点和起点就是从这两个点选。
无向图的欧拉回路:整个图为连通图。所有点的度全为偶数。
路径为:从任意一点出发,回到该点。
有向图的欧拉路径: 整个图为连通图。所有点的入度等于出度,或着只有两个点入度不等于出度,且一个入度比出度大一,一个出度比入度大一。
路径为:如果所有点的入度等于出度,从任意点出发均可。否则,出度大的点为起点,入度大的点为终点。
有向图的欧拉回路:整个图为连通图。所有点的入度等于出度。
路径为:从任意点出发均可。
我们再回来讨论这道题。
因为图中既有有向边又有无向边,那该拿上面的哪个条件判断呢?不能是无向图,那只能是有向图了。这样,我们就需要对无向边进行定向,使其满足条件。
先对无向边随便定向,判断入度和出度之差,为奇数,是不能成为欧拉回路。
接下来,我们利用网络流来完成无向图的边的定向。对于每个点,需要改变的边的数目是出入度之差的一半。如果入度大于出度,需要补充出度,向汇点连边。如果入度小于出度,需要补充入度,从源点向该点连边。最后,判断源点和汇点是否满流即可。如果是满流,就是存在欧拉回路,否则没有。
那从这个残留网络中,该怎样构造出欧拉回路呢?
对于我们构造的流网络,该点有从源点来的流量,说明,我们需要该点需要补充流量,才能平衡。而,根据流网络的反对称性,我们只需让该边反向,就能让出入度平衡。
代码如下:
#include <cstdio>#include <algorithm>#include <cstring>#include <vector>using namespace std;struct edge{ int from,to; int cap,flow; edge(int u =0,int v =0,int c=0,int f=0):from(u),to(v),cap(c),flow(f){}};struct ISAP{ static const int INF = 0x3f3f3f3f; static const int MAX = 10000;//边的大小 int head[MAX];//每个节点对应链表的开始位置 int next[MAX];//链表的下一个节点在edges数组的位置 int tot;//edges数组的大小 edge edges[2 * MAX];//储存边的数组,两倍大小 int que[MAX],front,tail;//队列,保存节点 int d[MAX];//距离标号 bool vis[MAX];//访问标记 int num[MAX];//gap优化 int pre[MAX];//增广路中,节点X的前面一个弧的标号 int cur[MAX];//对于每个节点的,处理的当前弧。 int s,t,n;//s源点标号,t汇点标号,n节点总数 edge cut[2 * MAX]; //保存割的数组 int cut_cnt; void init(int n){ this->n = n;//注意此处的下标问题 memset(head,-1,sizeof(head)); tot = cut_cnt = 0;// } void addedge(int from,int to, int cap){ edges[tot] = edge(from,to,cap,0);//注意无向边和有向边 next[tot] = head[from], head[from] = tot++; edges[tot] = edge(to,from,0,0); next[tot] = head[to],head[to] = tot++; } void bfs(){ memset(vis,0,sizeof(vis)); front = tail = 0; d[t] = 0; vis[t] = true; que[tail++] = t; while(front < tail){ int u = que[front++]; for(int v = head[u]; v != -1; v = next[v]){ edge & e = edges[v^1]; if(e.cap > e.flow && !vis[e.from]){//对处于残余网络中的弧且没访问过的节点处理 d[e.from] = d[u] + 1; vis[e.from] = true; que[tail++] = e.from; } } } } int augment(){ int x = t,a = INF; while(x != s){ edge& e = edges[pre[x]]; a = min(a,e.cap - e.flow); x = e.from; } x = t; while(x != s){ edges[pre[x]].flow += a; edges[pre[x]^1].flow -= a; x = edges[pre[x]].from; } return a; } int maxflow(int s, int t){ this->s = s, this->t = t; memset(num,0,sizeof(num)); int flow = 0; bfs(); for(int i = 0; i < n; ++i){//注意此处的下标问题 num[d[i]]++,cur[i] = head[i]; } int x = s; while(d[s] < n){ if(x == t){//前进到t,进行增广 flow += augment(); x = s; } bool ok = false; for(int &v = cur[x]; v != -1; v = next[v]){ edge& e = edges[v]; if(e.cap > e.flow && d[x] == d[e.to] + 1){ ok = true; pre[x = e.to] = v;//能增广,前进,记录 break; } } if(!ok){//不能增广,重新计算距离 int m = n - 1; for(int v = head[x]; v != -1; v = next[v]){ edge & e = edges[v]; if(e.cap > e.flow) m = min(m,d[e.to]); } if(--num[d[x]] == 0) break;//gap优化 num[d[x]=m+1]++; cur[x] = head[x]; if(x != s) x = edges[pre[x]].from;//能回退,回退 } } return flow; } void mincut(){ bfs();//重新标记,被标记的是t可达的顶点 for(int i = 0 ;i < tot; ++i){ edge & e = edges[i]; if(!vis[e.from] && vis[e.to] && e.cap > 0) cut[cut_cnt++] = e; } } void print(){ printf("Graph:\n"); for(int i = 0 ; i < tot; ++i){ edge & e = edges[i]; printf("%d->%d c:%d f:%d\n",e.from,e.to,e.cap,e.flow); } }} solver;const int MAX = 300;int T,M,S;int ind[MAX],outd[MAX];int x,y,d;int s,t;bool judge(){ int sum = 0; for(int i = 1; i <= M; ++i){ int dif = abs(ind[i] - outd[i]); if(dif & 1) return false; dif >>= 1; if(ind[i] > outd[i]) solver.addedge(i,t,dif); else if(ind[i] < outd[i]){ solver.addedge(s,i,dif); sum += dif; } } return sum == solver.maxflow(s,t);}int main(void){ //freopen("input.txt","r",stdin); scanf("%d", &T); while(T--){ memset(ind,0,sizeof(ind)); memset(outd,0,sizeof(outd)); scanf("%d %d", &M, &S); solver.init(M + 2); s = 0, t = M + 1; for(int i = 0; i < S; ++i){ scanf("%d %d %d",&x,&y,&d); outd[x]++; ind[y]++; if(d == 0) solver.addedge(x,y,1); } if(judge()) puts("possible"); else puts("impossible"); //solver.print(); } return 0;}
- POJ 1637 Sightseeing tour (混合图的欧拉回路)
- poj 1637 Sightseeing tour(混合欧拉回路)
- poj 1637 Sightseeing tour(混合欧拉回路)
- POJ 1637 Sightseeing tour(混合欧拉回路,网络流)
- POJ 1637 Sightseeing tour 混合欧拉回路
- POJ 1637 Sightseeing tour(混合图的欧拉回路)
- POJ 1637 Sightseeing tour 混合图的欧拉回路
- poj 1637 Sightseeing tour 混合图的欧拉回路
- POJ 637 Sightseeing tour 混合欧拉回路 最大流
- JOJ 2414 && POJ 1637 Sightseeing tour(混合欧拉回路)
- Poj 1637 Sightseeing tour (混合图的欧拉回路判定)
- Sightseeing tour (poj 1637 混合图的欧拉回路)
- poj 1637 Sightseeing tour 【网络流 求解混合欧拉回路是否存在】
- POJ-1637 Sightseeing tour(通过网络流判定混合图的欧拉回路)
- POJ 1637 Sightseeing tour (混合图欧拉回路)
- poj 1637 Sightseeing tour(混合图欧拉回路)
- poj -- 1637 Sightseeing tour(混合图欧拉回路)
- [POJ 1637]Sightseeing tour[混合图欧拉回路]
- C++学习总结2——函数
- QT学习笔记1:QT入门
- python <tab>自动补全
- SqlServer中存储过程中将Exec的执行结果赋值给变量输出
- MATLAB层次聚类分析法
- POJ 1637 Sightseeing tour 混合欧拉回路
- OpenLayers学习--加载指定等级指定范围的高德地图数据
- android系列:数据存储的几种方式
- 有道校招面试建议
- 基于数据场和云模型的FCM聚类算法MATLAB源代码
- 使用highcharts时,报表一直出不来
- 关于 float 和double
- UVA - 10010 Where's Waldorf?
- CString与LPCWSTR、LPSTR、char*、LPWSTR等类型的转换【转】