标签:des style blog http io color ar os sp
0 0 1 0 3 3 (0,1) (0,2) (1,2) 2 0 5 7 (0,1) (0,2) (1,3) (1,2) (1,4) (2,3) (3,4)
0 1 3 0 2
1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <cmath> 5 #include <algorithm> 6 #include <climits> 7 #include <vector> 8 #include <queue> 9 #include <cstdlib> 10 #include <string> 11 #include <set> 12 #include <stack> 13 #define LL long long 14 #define pii pair<int,int> 15 #define INF 0x3f3f3f3f 16 using namespace std; 17 const int maxn = 200; 18 struct arc{ 19 int to,flow,next; 20 arc(int x = 0,int y = 0,int z = -1){ 21 to = x; 22 flow = y; 23 next = z; 24 } 25 }; 26 arc e[maxn*maxn]; 27 int head[maxn],d[maxn],cur[maxn]; 28 int S,T,n,m,tot,cnt; 29 pii rec[maxn*maxn]; 30 void add(int u,int v,int flow){ 31 e[tot] = arc(v,flow,head[u]); 32 head[u] = tot++; 33 e[tot] = arc(u,0,head[v]); 34 head[v] = tot++; 35 } 36 bool bfs(){ 37 memset(d,-1,sizeof(d)); 38 d[S] = 1; 39 queue<int>q; 40 q.push(S); 41 while(!q.empty()){ 42 int u = q.front(); 43 q.pop(); 44 for(int i = head[u]; ~i; i = e[i].next){ 45 if(e[i].flow > 0 && d[e[i].to] == -1){ 46 d[e[i].to] = d[u] + 1; 47 q.push(e[i].to); 48 } 49 } 50 } 51 return d[T] > -1; 52 } 53 int dfs(int u,int low){ 54 if(u == T) return low; 55 int tmp = 0,a; 56 for(int &i = cur[u]; ~i; i = e[i].next){ 57 if(e[i].flow > 0&& d[e[i].to] == d[u] + 1&&(a=dfs(e[i].to,min(e[i].flow,low)))){ 58 e[i].flow -= a; 59 e[i^1].flow += a; 60 rec[cnt++] = make_pair(i,a); 61 rec[cnt++] = make_pair(i^1,-a); 62 low -= a; 63 tmp += a; 64 if(!low) break; 65 } 66 } 67 if(!tmp) d[u] = -1; 68 return tmp; 69 } 70 int dinic(){ 71 int ans = cnt = 0; 72 while(bfs()){ 73 memcpy(cur,head,sizeof(head)); 74 ans += dfs(S,INF); 75 } 76 return ans; 77 } 78 void release(){ 79 for(int i = 0; i < cnt; ++i) 80 e[rec[i].first].flow += rec[i].second; 81 } 82 int main(){ 83 int u,v; 84 while(~scanf("%d %d",&n,&m)){ 85 memset(head,-1,sizeof(head)); 86 if(!m){ 87 if(n == 1) puts("1"); 88 else puts("0"); 89 continue; 90 } 91 for(int i = tot = 0; i < m; ++i){ 92 scanf(" (%d,%d)",&u,&v); 93 add(u+n,v,INF); 94 add(v+n,u,INF); 95 } 96 for(int i = 0; i < n; ++i) 97 add(i,i+n,1); 98 S = n; 99 int ans = INF; 100 for(int i = 1; i < n; ++i){ 101 T = i; 102 ans = min(ans,dinic()); 103 release(); 104 } 105 printf("%d\n",ans == INF?n:ans); 106 } 107 return 0; 108 }
标签:des style blog http io color ar os sp
原文地址:http://www.cnblogs.com/crackpotisback/p/4100352.html