hust 1082 Adding-the-maximum-flow arc
2014-05-14 13:13
120 查看
题目描述
In the network G(s, t, N, A, U)(s: the souce node, t: the sink node, N: the set of the nodes, A: the set of the arcs, U: the set of the capacity of the arcs), adding a arc's capacity with one unit may make the maximum flow larger. We call this arc as the adding-the-maximum-flow arc. Now the problem is that giving a network G, you must calculate the number of the the adding-the-maximum-flow arcs in the network G.
输入
The first line of the input contains an integer T, giving the number of the test cases. For each test case: The first line contains four integers: n, m, s and t(0 < n <= 400, 0 < m <= 10000, 0 < s, t <= n), giving the number of nodes, the number of arcs, the source node and the sink node. And there are m lines followed: each line describe an arc: a, b, c, the arc is from a to b and its capacity is c(0 < a, b <= n, 0 < c < 1000).
输出
For each test case, the output contains a single integer which is the number of the adding-the-maximum flow arcs.
样例输入
样例输出
提示There may be multiarcs in the network, that is to say there may be more than one arc between two nodes.
最大流问题,这个题就是求最小割点的数目,我的想法是先求原图的最大流,再把每条边的容量加1,再做一次最大流,第二次的最大流减第一次的最大流就是割点的数目
In the network G(s, t, N, A, U)(s: the souce node, t: the sink node, N: the set of the nodes, A: the set of the arcs, U: the set of the capacity of the arcs), adding a arc's capacity with one unit may make the maximum flow larger. We call this arc as the adding-the-maximum-flow arc. Now the problem is that giving a network G, you must calculate the number of the the adding-the-maximum-flow arcs in the network G.
输入
The first line of the input contains an integer T, giving the number of the test cases. For each test case: The first line contains four integers: n, m, s and t(0 < n <= 400, 0 < m <= 10000, 0 < s, t <= n), giving the number of nodes, the number of arcs, the source node and the sink node. And there are m lines followed: each line describe an arc: a, b, c, the arc is from a to b and its capacity is c(0 < a, b <= n, 0 < c < 1000).
输出
For each test case, the output contains a single integer which is the number of the adding-the-maximum flow arcs.
样例输入
1 4 4 1 4 1 2 3 1 3 5 2 4 2 3 4 6
样例输出
2
提示There may be multiarcs in the network, that is to say there may be more than one arc between two nodes.
最大流问题,这个题就是求最小割点的数目,我的想法是先求原图的最大流,再把每条边的容量加1,再做一次最大流,第二次的最大流减第一次的最大流就是割点的数目
#include<map> #include<set> #include<stack> #include<queue> #include<cmath> #include<vector> #include<cstdio> #include<string> #include<cstring> #include<cstdlib> #include<iostream> #include<algorithm> #define inf 0x0f0f0f0f using namespace std; const double pi=acos(-1.0); const double eps=1e-8; typedef pair<int,int>pii; const int maxn=10000+10; struct Edge { int from,to,cap,flow; }; int n,m,s,t; vector<Edge>edges; vector<int>G[maxn]; int d[maxn],cur[maxn]; bool vis[maxn]; void AddEdge(int from,int to,int cap) { Edge temp; temp.cap=cap; temp.flow=0; temp.from=from; temp.to=to; edges.push_back(temp); temp.cap=0; temp.flow=0; temp.from=to; temp.to=from; edges.push_back(temp); m=edges.size(); G[from].push_back(m-2); G[to].push_back(m-1); } bool BFS() { memset(vis,0,sizeof(vis)); queue<int>Q; Q.push(s); d[s]=0; vis[s]=1; while(!Q.empty()) { int x=Q.front();Q.pop(); for (int i=0;i<G[x].size();i++) { Edge& e=edges[G[x][i]]; if (!vis[e.to] && e.cap>e.flow) { vis[e.to]=1; d[e.to]=d[x]+1; Q.push(e.to); } } } return vis[t]; } int DFS(int x,int a) { if (x==t || a==0) return a; int flow=0,f; for (int& i=cur[x];i<G[x].size();i++) { Edge& e=edges[G[x][i]]; if (d[x]+1==d[e.to] && (f=DFS(e.to,min(a,e.cap-e.flow)))>0) { e.flow+=f; edges[G[x][i]^1].flow-=f; flow+=f; a-=f; if (a==0) break; } } return flow; } int Dinic() { int flow=0; while (BFS()) { memset(cur,0,sizeof(cur)); flow+=DFS(s,inf); } return flow; } void init() { for (int i=0;i<=maxn;i++) G[i].clear(); edges.clear(); } int main() { //freopen("in.txt","r",stdin); int T,u[maxn],v[maxn],c[maxn],M; scanf("%d",&T); while (T--) { init(); scanf("%d%d%d%d",&n,&M,&s,&t); for (int i=0;i<M;i++) { scanf("%d%d%d",&u[i],&v[i],&c[i]); AddEdge(u[i],v[i],c[i]); c[i]++; } int ans1=Dinic(); init(); for (int i=0;i<M;i++) AddEdge(u[i],v[i],c[i]); int ans2=Dinic(); printf("%d\n",ans2-ans1); } //fclose(stdin); return 0; }
相关文章推荐
- ASP.NET Error:Adding the specified count to the semaphore would cause it to exceed its maximum count
- [转载]ASP.NET Error – Adding the specified count to the semaphore would cause it to exceed its maximum count
- 【English】Android -> Training -> Adding the Action Bar -> Styling the Action Bar
- 2017ICPC南宁 M题 The Maximum Unreachable Node Set [二分图最大独立集]
- the request was rejected because its size (***) exceeds the configured maximum (2097152) 上传不成功
- HUST 1010-The Minimum Length-KMP
- 【POJ2699】The Maximum Number of Strong Kings 枚举(二分)+网络流check、
- WCF常见异常-The maximum string content length quota (8192) has been exceeded while reading XML data
- Given n points on a 2D plane, find the maximum number of points that lie on the same straight line.
- hust 1010 - The Minimum Length KMP求循环节
- Solutions for the Maximum Subsequence Sum Problem
- Adding Entries to the Standard Context Menu(转载 from msdn)
- The maximum of three numbers code
- git push failed Because the Fault of insufficient permission for adding an object
- HUST1010-The Minimum Length
- hust NENU Summer Training 2014 #1 G - The Famous ICPC Team Again
- POJ2699 The Maximum Number of Strong Kings (maxflow)
- SSIS 之 Exception "truncated if it exceeds the maximum length of 4000 characters"
- hdu 4002 Find the maximum
- hust1010 The Minimum Length