poj 1637 (网络流——混合图的欧拉图)
一开始想传统的构图方法想了好久都不解,有两个问题觉得用网络流很难解决。 1. 如何控制每条街只走一次 2. 每条街都必须走一次 这两点我怎么都想不出怎样可以一起解决。
后面才知道,要利用欧拉图的特殊性质来做, 如果把无向边可以看成有向边,那么我们只要能够找到一种方法变换无向边的方向,使得每个顶点的出度等于入度就可以了,然后这个就可以用网络流来解决了。
计算每个点的出度,入度差,其实就是出度--,入度++, 然后如果最后为正,则从源点连一条这个值的权值, 如果为负,则从这个点连一条边道汇点,权为该点的大小.如果为奇数那么必不存在. 然后如果一开始是无向边(x,y)的话就默认为x->y的有向边,然后在求网络流建图时,采用y->x 权值为2的边。 这个认真想一下应该就可以明白.
最后,我感觉网络流绝不是死的,他的用途很大,要多利用一些性质,需求建图的方案.才能解决问题.
|
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 Source |
1 #include <stdio.h> 2 #include <string.h> 3 #include <string> 4 #include <iostream> 5 using namespace std; 6 7 #define N 220 8 #define M 2200 9 #define INF 0x3ffffff 10 11 /*struct node 12 { 13 int u,v,w; 14 }save[M]; // 表示等下要构图的边*/ 15 16 struct node 17 { 18 int to,next,w; 19 }edge[10*M]; 20 21 int g[N]; 22 int s,t; 23 int n,m; 24 int nn; 25 int cnt,pre[N]; 26 int tcnt; 27 int ans; 28 int lv[N],gap[N]; 29 30 void init() 31 { 32 ans=0; 33 cnt=0; tcnt=0; 34 memset(pre,-1,sizeof(pre)); 35 memset(g,0,sizeof(g)); 36 s=0; t=n+1; 37 } 38 39 void add_edge(int u,int v,int w) 40 { 41 edge[cnt].to=v; 42 edge[cnt].w=w; 43 edge[cnt].next=pre[u]; 44 pre[u]=cnt++; 45 } 46 47 int sdfs(int k,int w) 48 { 49 if(k==t) 50 return w; 51 int f=0; 52 int mi=nn-1; 53 for(int p=pre[k];p!=-1;p=edge[p].next) 54 { 55 int v=edge[p].to; 56 if(edge[p].w!=0) 57 { 58 if(lv[k]==lv[v]+1) 59 { 60 int tmp=sdfs(v,min(w-f,edge[p].w)); 61 f+=tmp; 62 edge[p].w -= tmp; 63 edge[p^1].w += tmp; 64 if(w==f||lv[s]==nn) return f; 65 } 66 if(mi>lv[v]) mi=lv[v]; 67 } 68 } 69 if(f==0) 70 { 71 gap[lv[k]]--; 72 if(gap[lv[k]]==0) 73 { 74 lv[s]=nn; 75 //return f; 76 } 77 lv[k]=mi+1; 78 gap[lv[k]]++; 79 } 80 return f; 81 } 82 83 int sap() 84 { 85 int sum=0; 86 nn=t+1; 87 memset(lv,0,sizeof(lv)); 88 memset(gap,0,sizeof(gap)); 89 gap[0]=nn; 90 while(lv[s]<nn) 91 { 92 sum+=sdfs(s,INF); 93 } 94 return sum; 95 } 96 int main() 97 { 98 int t1; 99 scanf("%d",&t1); 100 while(t1--) 101 { 102 scanf("%d%d",&n,&m); 103 init(); 104 for(int i=0;i<m;i++) 105 { 106 int flag,x,y; 107 scanf("%d%d%d",&x,&y,&flag); 108 if(flag==0) 109 { 110 g[x]--; 111 g[y]++; 112 add_edge(y,x,2); 113 add_edge(x,y,0); 114 } 115 else 116 { 117 g[x]--; 118 g[y]++; 119 } 120 } 121 int flag=0; 122 for(int i=1;i<=n;i++) 123 { 124 if(g[i]%2!=0) 125 { 126 flag=1; 127 break; 128 } 129 } 130 if(flag==1) 131 { 132 printf("impossible\n"); 133 continue; 134 } 135 for(int i=1;i<=n;i++) 136 { 137 if(g[i]>0) 138 { 139 ans+=g[i]; 140 add_edge(s,i,g[i]); 141 add_edge(i,s,0); 142 } 143 if(g[i]<0) 144 { 145 add_edge(i,t,-g[i]); 146 add_edge(t,i,0); 147 } 148 } 149 if( sap() == ans ) 150 printf("possible\n"); 151 else 152 printf("impossible\n"); 153 } 154 return 0; 155 }

浙公网安备 33010602011771号