Title Description
Given a n- n-point m undirected FIG. M edges, between two interrogation times the minimum cut
Minimum cut between two points is defined as: a picture of each side has cut its price, you need to use the minimum cost so that the two points do not communicate
Input and output formats
Input formats:
The first line of the two numbers n-, m n- , m
Subsequently m m rows, each row . 3 three numbers U, V, W U , V , W, indicates there is a connection U U to V V undirected edges, which cut the cost for the W W
Then this line have an integer Q Q, represents the number of inquiries
The next Q Q lines of two numbers U, v U , v, you need to find U U and v between the smallest cut v
Note: Because the data error, a real number of points should be given graph n-+. 1 n- + . 1 th, numbered 0 0 to n- n-
Output formats:
Total output Q Q rows, each row corresponding to an integer answer to the query
Sample input and output
Explanation
n \ leq 500, \ quad m \ leq 1500, \ quad Q \ leq 10 ^ 5, \ quad 0 \ leq w \ leq 10 ^ 4 n ≤ 5 0 0 , m ≤ 1 5 0 0 , Q ≤ 1 0 5 , 0 ≤ w ≤ 1 0 4
answer
-
Beginning to put a set of all points inside
-
Two start points set arbitrarily taken as sources and sinks, the maximum run again flow across the figure, the get minimum two cutting points
-
And all the points into a set of s and t set, then update set s to t answer set, and set the s and t set down recursive
-
And so, in the end you can get all the points of the minimal cut
Code
1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <algorithm> 5 #include <queue> 6 #define inf 0x3f3f3f3f 7 #define N 510 8 using namespace std; 9 int s,t,cnt=1,n,m,q,dis[N],head[N],a[N],tmp[N],ans[N][N],mark[N]; 10 struct edge{int to,c,from;}e[N*200]; 11 queue <int> Q; 12 void insert(int u,int v,int c) 13 { 14 e[++cnt].to=v,e[cnt].c=c,e[cnt].from=head[u],head[u]=cnt; 15 e[++cnt].to=u,e[cnt].c=c,e[cnt].from=head[v],head[v]=cnt; 16 } 17 bool bfs() 18 { 19 memset(dis,0,sizeof(dis)),dis[s]=2; 20 while (!Q.empty()) Q.pop(); Q.push(s); 21 while (!Q.empty()) 22 { 23 int u=Q.front(); Q.pop(); 24 for (int i=head[u];i;i=e[i].from) 25 if (e[i].c&&!dis[e[i].to]) 26 { 27 dis[e[i].to]=dis[u]+1; 28 if (e[i].to==t) return 1; 29 Q.push(e[i].to); 30 } 31 } 32 return 0; 33 } 34 int dfs(int x,int maxf) 35 { 36 if (x==t||!maxf) return maxf; 37 int ret=0; 38 for (int i=head[x];i;i=e[i].from) 39 if (e[i].c&&dis[e[i].to]==dis[x]+1) 40 { 41 int f=dfs(e[i].to,min(e[i].c,maxf-ret)); 42 e[i].c-=f,e[i^1].c+=f,ret+=f; 43 if (ret==maxf) break; 44 } 45 if (!ret) dis[x]=0; 46 return ret; 47 } 48 void dfs(int x) 49 { 50 mark[x]=1; 51 for (int i=head[x];i;i=e[i].from) if (e[i].c&&!mark[e[i].to]) dfs(e[i].to); 52 } 53 void solve(int l,int r) 54 { 55 if (l==r) return; 56 s=a[l],t=a[r]; 57 for (int i=2;i<=cnt;i+=2) e[i].c=e[i^1].c=(e[i].c+e[i^1].c)/2; 58 int flow=0; 59 while (bfs()) flow+=dfs(s,inf); 60 memset(mark,0,sizeof(mark)),dfs(s); 61 for (int i=1;i<=n;i++) if (mark[i]) for (int j=1;j<=n;j++) if (!mark[j]) ans[i][j]=ans[j][i]=min(ans[i][j],flow); 62 int i=l,j=r; 63 for (int k=l;k<=r;k++) if (mark[a[k]]) tmp[i++]=a[k]; else tmp[j--]=a[k]; 64 for (int k=l;k<=r;k++) a[k]=tmp[k]; 65 solve(l,i-1),solve(j+1,r); 66 } 67 int main() 68 { 69 scanf("%d%d",&n,&m); 70 for (int i=1;i<=n;i++) a[i]=i; 71 memset(ans,inf,sizeof(ans)); 72 for (int i=1,x,y,z;i<=m;i++) scanf("%d%d%d",&x,&y,&z),insert(x,y,z); 73 solve(1,n),scanf("%d",&q); 74 for (int i=1,x,y;i<=q;i++) scanf("%d%d",&x,&y),printf("%d\n",ans[x][y]); 75 }