Time Limit: 20000/10000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 9473 Accepted Submission(s): 3069
ios
最大流裸題 *可用模板spa
1 #include<cstdio> 2 #include<cstring> 3 #include<iostream> 4 #include<algorithm> 5 #include<map> 6 using namespace std; 7 #define ll long long 8 const int M = 100010; 9 const int INF = 0x3f3f3f3f; 10 struct Edge{ 11 int to,next,cap,flow; 12 }edge[4*M]; 13 int tol; 14 int head[M]; 15 int gap[M],dep[M],cur[M]; 16 void init() 17 { 18 tol=0; 19 memset(head,-1,sizeof(head)); 20 } 21 void addedge(int u,int v,int w){ 22 int rw=0; 23 edge[tol].to=v;edge[tol].cap=w;edge[tol].flow=0; 24 edge[tol].next=head[u];head[u]=tol++; 25 edge[tol].to=u;edge[tol].cap=rw;edge[tol].flow=0; 26 edge[tol].next=head[v];head[v]=tol++; 27 } 28 int Q[M]; 29 void bfs(int start,int end){ 30 memset(dep,-1,sizeof(dep)); 31 memset(gap,0,sizeof(gap)); 32 gap[0]=1; 33 int front=0,rear=0; 34 dep[end]=0; 35 Q[rear++]=end; 36 while(front !=rear){ 37 int u=Q[front++]; 38 for(int i=head[u];i!=-1;i=edge[i].next){ 39 int v=edge[i].to; 40 if(dep[v]!=-1) continue; 41 Q[rear++]=v; 42 dep[v]=dep[u]+1; 43 gap[dep[v]]++; 44 } 45 } 46 } 47 int S[M]; 48 int sap(int start,int end,int N){ 49 bfs(start,end); 50 memcpy(cur,head,sizeof(head)); 51 int top=0; 52 int u=start; 53 int ans=0; 54 while(dep[start]<N){ 55 if(u==end){ 56 int Min=INF; 57 int inser; 58 for(int i=0;i<top;i++){ 59 if(Min>edge[S[i]].cap-edge[S[i]].flow){ 60 Min=edge[S[i]].cap-edge[S[i]].flow; 61 inser=i; 62 } 63 } 64 for(int i=0;i<top;i++){ 65 edge[S[i]].flow+=Min; 66 edge[S[i]^1].flow-=Min; 67 } 68 ans+=Min; 69 top=inser; 70 u=edge[S[top]^1].to; 71 continue; 72 } 73 bool flag=false; 74 int v; 75 for(int i=cur[u];i!=-1;i=edge[i].next){ 76 v=edge[i].to; 77 if(edge[i].cap-edge[i].flow&&dep[v]+1==dep[u]){ 78 flag=true; 79 cur[u]=i; 80 break; 81 } 82 } 83 if(flag){ 84 S[top++] = cur[u]; 85 u=v; 86 continue; 87 } 88 int Min=N; 89 for(int i=head[u];i!=-1;i=edge[i].next){ 90 if(edge[i].cap-edge[i].flow&&dep[edge[i].to]<Min){ 91 Min=dep[edge[i].to]; 92 cur[u]=i; 93 } 94 } 95 gap[dep[u]]--; 96 if(!gap[dep[u]]) return ans; 97 dep[u]=Min+1; 98 gap[dep[u]]++; 99 if(u!=start) u=edge[S[--top]^1].to; 100 } 101 return ans; 102 } 103 int t; 104 int n,m; 105 int main() 106 { 107 scanf("%d",&t); 108 while(t--){ 109 init(); 110 scanf("%d %d",&n,&m); 111 int minx=10000000; 112 int maxx=-10000000; 113 int start,tail; 114 for(int i=1;i<=n;i++){ 115 int x,y; 116 scanf("%d %d",&x,&y); 117 if(x<minx){minx=x;start=i;} 118 else if(x>maxx){maxx=x;tail=i;} 119 } 120 for(int i=1;i<=m;i++){ 121 int qq,w,e; 122 scanf("%d %d %d",&qq,&w,&e); 123 addedge(qq,w,e); 124 addedge(w,qq,e); 125 } 126 printf("%d\n",sap(start,tail,n)); 127 } 128 return 0; 129 }