标签:
Description
Input
Output
Sample Input
3 6 1 2 3 4 2 1 1 2 1 1 3 2 1 2 3 1 2 3
Sample Output
5 3 1 + 2 - 2 +
1 #include <iostream> 2 #include <cstring> 3 #include <cstdio> 4 #include <queue> 5 using namespace std; 6 const int maxn=1010; 7 const int maxm=100010; 8 const int INF=1000000000; 9 int cnt,tot,fir[maxn],fron[maxn],dis[maxn]; 10 int to[maxm],nxt[maxm],gap[maxn],path[maxn]; 11 int cap[maxm];queue<int>q; 12 13 struct Max_Flow{ 14 void Init(int tot_=0){ 15 tot=tot_;cnt=1; 16 memset(fir,0,sizeof(fir)); 17 memset(dis,0,sizeof(dis)); 18 memset(gap,0,sizeof(gap)); 19 } 20 21 void add(int a,int b,int c){ 22 nxt[++cnt]=fir[a]; 23 fir[a]=cnt; 24 cap[cnt]=c; 25 to[cnt]=b; 26 } 27 28 void addedge(int a,int b,int c){ 29 add(a,b,c); 30 add(b,a,0); 31 } 32 33 bool BFS(int s,int t){ 34 dis[t]=1;q.push(t); 35 while(!q.empty()){ 36 int x=q.front();q.pop(); 37 for(int i=fir[x];i;i=nxt[i]) 38 if(!dis[to[i]]){ 39 dis[to[i]]=dis[x]+1; 40 q.push(to[i]); 41 } 42 } 43 return dis[s]; 44 } 45 46 int Aug(int s,int t,int &p){ 47 int f=INF; 48 while(p!=s){ 49 f=min(f,cap[path[p]]); 50 p=to[path[p]^1]; 51 }p=t; 52 while(p!=s){ 53 cap[path[p]]-=f; 54 cap[path[p]^1]+=f; 55 p=to[path[p]^1]; 56 } 57 return f; 58 } 59 60 int ISAP(int s,int t){ 61 if(!BFS(s,t))return 0; 62 for(int i=s;i<=t;i++)fron[i]=fir[i]; 63 for(int i=s;i<=t;i++)gap[dis[i]]+=1; 64 int p=s,ret=0; 65 while(dis[s]<=tot){ 66 if(p==t)ret+=Aug(s,t,p); 67 68 for(int &i=fron[p];i;i=nxt[i]) 69 if(cap[i]&&dis[p]==dis[to[i]]+1){ 70 path[p=to[i]]=i; 71 break; 72 } 73 74 if(!fron[p]){ 75 if(--gap[dis[p]]==0) 76 break; 77 int Min=tot; 78 for(int i=fir[p];i;i=nxt[i]) 79 if(cap[i])Min=min(Min,dis[to[i]]); 80 gap[dis[p]=Min+1]+=1;fron[p]=fir[p]; 81 if(p!=s)p=to[path[p]^1]; 82 } 83 } 84 return ret; 85 } 86 }isap; 87 88 int n,m,top; 89 int tag[maxn],st[maxn]; 90 void DFS(int x){ 91 tag[x]=1; 92 for(int i=fir[x];i;i=nxt[i]) 93 if(cap[i]&&!tag[to[i]])DFS(to[i]); 94 } 95 96 int main(){ 97 scanf("%d%d",&n,&m); 98 int s=0,t=2*n+1; 99 isap.Init(t+1); 100 for(int i=1,v;i<=n;i++){ 101 scanf("%d",&v); 102 isap.addedge(s,i,v); 103 } 104 for(int i=1,v;i<=n;i++){ 105 scanf("%d",&v); 106 isap.addedge(i+n,t,v); 107 } 108 for(int i=1,a,b;i<=m;i++){ 109 scanf("%d%d",&a,&b); 110 isap.addedge(b,a+n,INF); 111 } 112 113 printf("%d\n",isap.ISAP(s,t)); 114 DFS(0); 115 for(int i=1;i<=n;i++){ 116 if(!tag[i]) 117 st[++top]=i; 118 if(tag[i+n]) 119 st[++top]=i+n; 120 } 121 printf("%d\n",top); 122 for(int i=1;i<=top;i++){ 123 if(st[i]<=n) 124 printf("%d +\n",st[i]); 125 else 126 printf("%d -\n",st[i]-n); 127 } 128 return 0; 129 }
图论(网络流,二分图最小点权覆盖):POJ 2125 Destroying The Graph
标签:
原文地址:http://www.cnblogs.com/TenderRun/p/5651711.html