Http://acm.zju.edu.cn/onlinejudge/showProblem.do? Problemid = 5300
Question: Give an undirected graph, as well as the start and end points. To delete some edges so that the start point and the end point are not connected, the number of edges to be deleted should be as few as possible when the sum of the right values of the deleted edges is the smallest. Obtain a ratio: the number of remaining edges and the number of deleted edges.
Idea: the minimum sum of the weight of the deleted edge is obviously the minimum cut, that is, the maximum stream. But at the same time, the minimum number of deleted edges is required. The solution is to add the number of edges to the weight value and calculate the maximum flow together, because the maximum number of edges is 1000, that is, the edge weight of each edge is set to w * 10000 + 1, and 1 represents this edge. Then the minimum cut is obtained, and the minimum edge number is also obtained. If the minimum cut is ans, the number of deleted edges is ans % 10000, and the minimum cut is ANS/10000.
#include <stdio.h>#include <iostream>#include <map>#include <stack>#include <vector>#include <math.h>#include <string.h>#include <queue>#include <string>#include <stdlib.h>#include <algorithm>#define LL long long#define _LL __int64#define eps 1e-8#define PI acos(-1.0)using namespace std;const int INF = 0x3f3f3f3f;const int maxn = 55;const int maxm = 1010;struct node{int v,w,next,re;}edge[4*maxm];int n,m,s,t;int cnt,head[maxn];int dis[maxn],vis[maxn];queue <int> que;void init(){cnt = 0;memset(head,-1,sizeof(head));}void add(int u, int v, int w){edge[cnt] = ((struct node){v,w,head[u],cnt+1});head[u] = cnt++;edge[cnt] = ((struct node){u,0,head[v],cnt-1});head[v] = cnt++;}bool bfs(){memset(vis,0,sizeof(vis));memset(dis,0,sizeof(dis));while(!que.empty()) que.pop();dis[s] = 0;vis[s] = 1;que.push(s);while(!que.empty()){int u = que.front();que.pop();for(int i = head[u]; i != -1; i = edge[i].next){int v = edge[i].v;if(!vis[v] && edge[i].w){vis[v] = 1;que.push(v);dis[v] = dis[u] + 1;}}}if(dis[t] == 0)return false;return true;}int dfs(int u, int delta){if(u == t)return delta;int ret = 0,tmp;for(int i = head[u]; i != -1; i = edge[i].next){int v = edge[i].v;if(edge[i].w && dis[v] == dis[u] + 1 && (tmp = dfs(v,min(delta,edge[i].w)))){edge[i].w -= tmp;edge[edge[i].re].w += tmp;return tmp;}}if(!ret)dis[u] = -1;return ret;}int Dinic(){int ans = 0,res;while(bfs()){while(res = dfs(s,INF))ans += res;}return ans;}int main(){int test;int u,v,w;int sum;scanf("%d",&test);while(test--){scanf("%d %d %d %d",&n,&m,&s,&t);init();sum = 0;for(int i = 1; i <= m; i++){scanf("%d %d %d",&u,&v,&w);add(u,v,w*10000+1);add(v,u,w*10000+1);sum += w;}int ans = Dinic();if(ans == 0){printf("Inf\n");continue;}int a = sum - ans/10000;int b = ans%10000;printf("%.2lf\n",(double)a/b);}return 0;}