网络流(最大流) POJ 1637 Sightseeing tour

Sightseeing tour

Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 8628   Accepted: 3636

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

  题意:给你一个图,其中既有有向边又有无向边,要你判断图中是否存在欧拉回路。  这题难点就在于讨论无向边的方向。首先,欧拉回路图有个性质:所有点的入度等于出度。然后又发现,对于某点连出去的一条无向边,改变它的方向,这个点的(出度-入度)奇偶性不变。所以先给无向边随意定向,然后判断是否有点的(出度-入度)为奇数,有就绝逼不可能有欧拉回路。  然而到这里还没有完,每个点的(出度-入度)都为偶数并不代表改变那些无向边的方向就可以形成一个欧拉回路图。  现在的问题类似于网络流的分配问题,设一个点的(出度-入度)为d,那么将d大于零的点和d小于零的点分成两个集合,保留原来的无向边,容量为1……具体还是看程序吧。
  1 #include <iostream>
  2 #include <cstring>
  3 #include <cstdio>
  4 #include <queue>
  5
  6 using namespace std;
  7 const int INF=2147483647;
  8 const int maxn=210,maxm=20010;
  9 int cnt,fir[maxn],nxt[maxm],cap[maxm],to[maxm],dis[maxn],gap[maxn],path[maxn];
 10 int In[maxn],Out[maxn];
 11 void addedge(int a,int b,int c)
 12 {
 13     nxt[++cnt]=fir[a];
 14     to[cnt]=b;
 15     cap[cnt]=c;
 16     fir[a]=cnt;
 17 }
 18
 19 bool BFS(int S,int T)
 20 {
 21     memset(dis,0,sizeof(dis));
 22     dis[T]=1;
 23     queue<int>q;q.push(T);
 24     while(!q.empty())
 25     {
 26         int node=q.front();q.pop();
 27         for(int i=fir[node];i;i=nxt[i])
 28         {
 29             if(dis[to[i]])continue;
 30             dis[to[i]]=dis[node]+1;
 31             q.push(to[i]);
 32         }
 33     }
 34     return dis[S];
 35 }
 36 int fron[maxn];
 37 int ISAP(int S,int T)
 38 {
 39     if(!BFS(S,T))
 40         return 0;
 41     for(int i=1;i<=T;i++)++gap[dis[i]];
 42     int p=S,ret=0;
 43     memcpy(fron,fir,sizeof(fir));
 44     while(dis[S]<=T+1)
 45     {
 46         if(p==T){
 47             int f=INF;
 48             while(p!=S){
 49                 f=min(f,cap[path[p]]);
 50                 p=to[path[p]^1];
 51             }
 52             p=T;ret+=f;
 53             while(p!=S){
 54                 cap[path[p]]-=f;
 55                 cap[path[p]^1]+=f;
 56                 p=to[path[p]^1];
 57             }
 58         }
 59         int &ii=fron[p];
 60         for(;ii;ii=nxt[ii]){
 61             if(!cap[ii]||dis[to[ii]]+1!=dis[p])
 62                 continue;
 63             else
 64                 break;
 65         }
 66
 67         if(ii){
 68             p=to[ii];
 69             path[p]=ii;
 70         }
 71
 72
 73         else{
 74             if(--gap[dis[p]]==0)break;
 75             int minn=T+1;
 76             for(int i=fir[p];i;i=nxt[i])
 77                 if(cap[i])
 78                     minn=min(minn,dis[to[i]]);
 79             gap[dis[p]=minn+1]++;
 80             fron[p]=fir[p];
 81             if(p!=S)
 82                 p=to[path[p]^1];
 83         }
 84     }
 85     return ret;
 86 }
 87
 88 void Init()
 89 {
 90     memset(fir,0,sizeof(fir));
 91     memset(gap,0,sizeof(gap));
 92     memset(In,0,sizeof(In));
 93     memset(Out,0,sizeof(Out));
 94     cnt=1;
 95 }
 96 int main()
 97 {
 98     int T,n,m;
 99     scanf("%d",&T);
100     while(T--)
101     {
102         Init();
103         scanf("%d%d",&n,&m);
104         for(int i=1;i<=m;i++)
105         {
106             int u,v,k;
107             scanf("%d%d%d",&u,&v,&k);
108             In[v]++;Out[u]++;
109             if(!k)
110                 addedge(u,v,1),addedge(v,u,0);
111         }
112         int flag=1;
113         for(int i=1;i<=n;i++){
114             int d=Out[i]-In[i];
115             if(d&1){
116                 flag=0;
117                 break;
118             }
119             if(d>0)addedge(0,i,d/2),addedge(i,0,0);
120             if(d<0)addedge(i,n+1,d/(-2)),addedge(n+1,i,0);
121         }
122         if(flag)
123             ISAP(0,n+1);
124         for(int i=fir[0];i;i=nxt[i])
125             if(cap[i])
126                 flag=0;
127
128         if(flag)
129             puts("possible");
130         else
131             puts("impossible");
132     }
133     return 0;
134 }
最后感谢邝斌的题解,%%%
时间: 2024-10-14 13:29:09

网络流(最大流) POJ 1637 Sightseeing tour的相关文章

POJ 1637 Sightseeing tour(最大流)

POJ 1637 Sightseeing tour 题目链接 题意:给一些有向边一些无向边,问能否把无向边定向之后确定一个欧拉回路 思路:这题的模型很的巧妙,转一个http://blog.csdn.net/pi9nc/article/details/12223693 先把有向边随意定向了,然后依据每一个点的入度出度之差,能够确定每一个点须要调整的次数,然后中间就是须要调整的边,容量为1,这样去建图最后推断从源点出发的边是否都满流就可以 代码: #include <cstdio> #includ

POJ 1637 Sightseeing tour (混合图欧拉回路,网络最大流)

http://poj.org/problem?id=1637 Sightseeing tour Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 7498   Accepted: 3123 Description The city executive board in Lund wants to construct a sightseeing tour by bus in Lund, so that tourists can

POJ 1637 Sightseeing tour 混合图欧拉回路存在性判断

没有想到网络流还能解决这一类问题,完全想不到@[email protected] 一开始把所有的无向边制定任意方向有当做有向边看,然后统计每个点的入度和出度.以前有向图的欧拉回路判定是每个点的入读都等于出度,这样可以保证可以回到起点,现在在一些边可以调换方向的情况下,所有定点的入度和出度之差必定为偶数,因为调换任意一条边的方向都会使两个定点的入度和出度变化2,所以要构成一个欧拉回路所有点的入度和出度之差都为偶数,并设差为deg. 现在问题转化成了能否通过改变一些边的方向来是的所有点的入度出度都为

POJ 1637 Sightseeing tour (混合图欧拉回路)

POJ 1637 Sightseeing tour 链接:http://poj.org/problem?id=1637 题意:给定一个混合图,既有有向边,又有无向边,问是否存在欧拉回路. 思路: 1 定义 欧拉通路 (Euler tour)--通过图中每条边一次且仅一次,并且过每一顶点的通路. 欧拉回路 (Euler circuit)--通过图中每条边一次且仅一次,并且过每一顶点的回路. 欧拉图--存在欧拉回路的图. 2 无向图是否具有欧拉通路或回路的判定 G有欧拉通路的充分必要条件为:G 连通

POJ 1637 Sightseeing tour(混合图的欧拉回路)

题目链接 建个图,套个模板. #include <cstdio> #include <cstring> #include <iostream> #include <map> #include <algorithm> #include <vector> #include <string> #include <queue> using namespace std; #define INF 0x3ffffff str

POJ 1637 Sightseeing tour

Sightseeing tour Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 9276   Accepted: 3924 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 beauti

POJ 1637 Sightseeing tour (混合图欧拉回路)

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 visit

POJ 1637 Sightseeing tour(混合图欧拉回路+最大流)

http://poj.org/problem?id=1637 题意:给出n个点和m条边,这些边有些是单向边,有些是双向边,判断是否能构成欧拉回路. 思路: 构成有向图欧拉回路的要求是入度=出度,无向图的要求是所有顶点的度数为偶数. 但不管是那个,顶点的度数若是奇数,那都是不能构成的. 这道题目是非常典型的混合图欧拉回路问题,对于双向边,我们先随便定个向,然后就这样先记录好每个顶点的入度和出度. 如果有顶点的度数为奇数,可以直接得出结论,是不能构成欧拉回路的. 那么,如果都是偶数呢? 因为还会存在

poj 1637 Sightseeing tour —— 最大流+欧拉回路

题目:http://poj.org/problem?id=1637 建图很妙: 先给无向边随便定向,这样会有一些点的入度不等于出度: 如果入度和出度的差值不是偶数,也就是说这个点的总度数是奇数,那么一定无解: 随便定向后,如果定向 x -> y,那么从 y 向 x 连一条容量为1的边,将来选了这条边,表示重新定向成 y -> x 了: 考虑如果选了这条边,那么 x 的出度-1,入度+1,变化量是2: 所以对于每个点,如果入度>出度,从源点向它连容量为 (入度-出度)/2 的边,因为刚才改