POJ 1637 网络流判混合欧拉回路 解题报告
来源:互联网 发布:qq企业邮箱域名 编辑:程序博客网 时间:2024/06/05 09:09
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
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
【解题报告】
来自http://blog.csdn.net/l04205613/article/details/6681300
给出一张混合图(有有向边,也有无向边),判断是否存在欧拉回路。
首先是对图中的无向边随意定一个方向,然后统计每个点的入度(indeg)和出度(outdeg),如果(indeg - outdeg)是奇数的话,一定不存在欧拉回路;
如果所有点的入度和出度之差都是偶数,那么就开始网络流构图:
1,对于有向边,舍弃;对于无向边,就按照最开始指定的方向建权值为 1 的边;
2,对于入度小于出度的点,从源点连一条到它的边,权值为(outdeg - indeg)/2;出度小于入度的点,连一条它到汇点的权值为(indeg - outdeg)/2 的边;
构图完成,如果满流(求出的最大流值 == 和汇点所有连边的权值之和),那么存在欧拉回路,否则不存在。
代码如下:
#include<cstdio>#include<cstring>#include<algorithm>#include<queue>using namespace std;#define N 210 #define inf 0x3f3f3f3fstruct Edge{int s,e,v,next;}edge[10*N]; int n,m,e_num,cnt,head[N],d[N],sp,tp; int sum,indeg[N],outdeg[N]; void AddEdge(int a,int b,int c){ edge[e_num].s=a;edge[e_num].e=b;edge[e_num].v=c; edge[e_num].next=head[a];head[a]=e_num++; edge[e_num].s=b;edge[e_num].e=a;edge[e_num].v=0; edge[e_num].next=head[b];head[b]=e_num++; } void getmap(){ sum=0; for(int i=1;i<=n;i++) { if(indeg[i]<outdeg[i]) { AddEdge(0,i,(outdeg[i]-indeg[i])/2); } else if(indeg[i]>outdeg[i]) { AddEdge(i,n+1,(indeg[i]-outdeg[i])/2); sum+=(indeg[i]-outdeg[i])/2; } } } int bfs(){ queue<int> q; memset(d,-1,sizeof(d)); d[sp]=0; q.push(sp); while(!q.empty()) { int cur=q.front(); q.pop(); for(int i=head[cur];i!=-1;i=edge[i].next) { int u=edge[i].e; if(d[u]==-1 && edge[i].v>0) { d[u]=d[cur]+1; q.push(u); } } } return d[tp]!=-1; } int dfs(int a,int b){ int r=0; if(a==tp) return b; for(int i=head[a];i!=-1 && r<b;i=edge[i].next) { int u=edge[i].e; if(edge[i].v>0&&d[u]==d[a]+1) { int x=min(edge[i].v,b-r); x=dfs(u,x); r+=x; edge[i].v-=x; edge[i^1].v+=x; } } if(!r) d[a]=-2; return r; } int dinic(int sp,int tp){ int total=0,t; while(bfs()) { while(t=dfs(sp,inf)) total+=t; } return total; } void solve(){ int i,flag=1; for(i=1;i<=n;i++) { if((indeg[i]-outdeg[i])%2==1) { flag=0;break; } } if(!flag) puts("impossible"); else { getmap(); int ans=dinic(sp,tp); if(sum==ans) puts("possible"); else puts("impossible"); } } int main() { int t; for(scanf("%d",&t);t;--t) { e_num=0; memset(head,-1,sizeof(head)); memset(indeg,0,sizeof(indeg)); memset(outdeg,0,sizeof(outdeg)); scanf("%d%d",&n,&m); for(int i=0;i<m;i++) { int a,b,c; scanf("%d%d%d",&a,&b,&c); indeg[b]++; outdeg[a]++; if(c==0) AddEdge(a,b,1); } sp=0;tp=n+1; solve(); } return 0; }
- POJ 1637 网络流判混合欧拉回路 解题报告
- POJ 1637 Sightseeing tour(混合欧拉回路,网络流)
- POJ 1637 混合图的欧拉回路 网络流
- poj 1637(求混合图的欧拉回路是否存在:构造网络流求解)
- poj 1637 混合欧拉回路 网络流 dinic 以及sap 算法
- poj 1637 Sightseeing tour 【网络流 求解混合欧拉回路是否存在】
- POJ-1637 Sightseeing tour(通过网络流判定混合图的欧拉回路)
- POJ 1637 混合欧拉回路
- poj 2337 Catenyms 欧拉回路+dfs 解题报告
- hdu 1878 欧拉回路 解题报告
- HDU1116 欧拉回路 解题报告
- POJ1300 欧拉回路 解题报告
- poj 1637判定混合图欧拉回路 uva10735 混合欧拉回路+路径输出
- 混合图的欧拉回路 POJ 1637
- poj 1637 & zoj 1992 混合图的欧拉回路
- POJ 1637 混合图的欧拉回路
- POJ 1637 Sightseeing tour (混合图的欧拉回路)
- poj 1637 Sightseeing tour(混合欧拉回路)
- [LeetCode]86. Partition List
- python (小)模块简介
- 利用Sympy做数学
- ps的一些使用技巧
- Linux(十四):磁盘分区与格式化
- POJ 1637 网络流判混合欧拉回路 解题报告
- Python3学习(11)--高阶函数
- Eclipse-properties文件
- 小辨常量指针与指向常量的指针
- 基于局部均方差的图像去噪声算法
- ioWriterAndReader
- 高级网络控制
- 将tensorflow训练好的模型移植到android
- OpenSessionInViewFilter