标签:closed algo inf 一个 小结 isp detail family lan
1 #include<iostream> 2 #include<cstdio> 3 #include<cstring> 4 #include<algorithm> 5 #include<cmath> 6 #include<cstdlib> 7 using namespace std; 8 const int minf=2100010000; 9 int n,m; 10 struct nod{ 11 int rev; 12 int y; 13 int next; 14 int v; 15 }e[201010]; 16 int head[2100]={}; 17 int tot=0; 18 bool vis[2100]={}; 19 inline void init(int x,int y,int v,int rev){ 20 e[++tot].y=y; 21 e[tot].next=head[x]; 22 e[tot].v=v; 23 e[tot].rev=rev; 24 head[x]=tot; 25 } 26 inline int dis(int x){ 27 return x+n; 28 } 29 int dfs(int s,int t,int f){ 30 if(s==t){ 31 return f; 32 } 33 vis[s]=1; 34 for(int i=head[s];i;i=e[i].next){ 35 int v=e[i].v,y=e[i].y; 36 if(vis[y]==0&&v>0){ 37 int d=dfs(y,t,min(f,v)); 38 if(d>0){ 39 e[i].v-=d; 40 e[e[i].rev].v+=d; 41 return d; 42 } 43 } 44 } 45 return 0; 46 } 47 int main(){ 48 //freopen("wtf.in","r",stdin); 49 scanf("%d%d",&n,&m); 50 int x,y; 51 for(int i=2;i<n;i++){ 52 init(i,i+n,1,tot+2); 53 init(i+n,i,1,tot); 54 } 55 for(int i=1;i<=m;i++){ 56 scanf("%d%d",&x,&y); 57 if(x>y){ 58 swap(x,y); 59 } 60 if(y==n){ 61 init(x+n,y,1,tot+2); 62 init(y,x+n,0,tot); 63 init(y,x+n,1,tot+2); 64 init(x+n,y,0,tot); 65 } 66 else if(x==1){ 67 init(1,y,1,tot+2); 68 init(y,1,0,tot); 69 init(y,1,1,tot+2); 70 init(1,y,0,tot); 71 } 72 else{ 73 init(x+n,y,1,tot+2); 74 init(y,x+n,0,tot); 75 init(y+n,x,1,tot+2); 76 init(x,y+n,0,tot); 77 } 78 } 79 int ans=0; 80 for(;;){ 81 memset(vis,0,sizeof(vis)); 82 int f=dfs(1,n,minf); 83 if(f==0){ 84 break; 85 } 86 ans+=f; 87 } 88 if(ans==0){ 89 printf("%d\n",ans); 90 } 91 else 92 printf("%d\n",ans-1); 93 return 0; 94 }
标签:closed algo inf 一个 小结 isp detail family lan
原文地址:http://www.cnblogs.com/137shoebills/p/7783833.html