1 1 7 11 0 6 0 1 3 0 3 3 1 2 4 2 0 3 2 3 1 2 4 2 3 4 2 3 5 6 4 1 1 4 6 1 5 6 9
1 1.667
1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <cmath> 5 #include <algorithm> 6 #include <climits> 7 #include <vector> 8 #include <queue> 9 #include <cstdlib> 10 #include <string> 11 #include <set> 12 #include <stack> 13 #define LL long long 14 #define INF 0x3f3f3f3f 15 using namespace std; 16 const int maxn = 10010; 17 struct arc{ 18 int to,flow,next; 19 arc(int x = 0,int y = 0,int z = -1){ 20 to = x; 21 flow = y; 22 next = z; 23 } 24 }; 25 arc e[maxn*10]; 26 int head[maxn],d[maxn],cur[maxn],tot,s,t,n; 27 void add(int u,int v,int flow){ 28 e[tot] = arc(v,flow,head[u]); 29 head[u] = tot++; 30 e[tot] = arc(u,0,head[v]); 31 head[v] = tot++; 32 } 33 bool bfs(){ 34 queue<int>q; 35 for(int i = 0; i <= n; ++i) d[i] = -1; 36 q.push(s); 37 d[s] = 1; 38 while(!q.empty()){ 39 int u = q.front(); 40 q.pop(); 41 for(int i = head[u]; ~i; i = e[i].next){ 42 if(e[i].flow && d[e[i].to] == -1){ 43 d[e[i].to] = d[u] + 1; 44 q.push(e[i].to); 45 } 46 } 47 } 48 return d[t] > -1; 49 } 50 int dfs(int u,int low){ 51 if(u == t) return low; 52 int tmp = 0,a; 53 for(int &i = cur[u]; ~i; i = e[i].next){ 54 if(e[i].flow && d[e[i].to] == d[u] + 1 && (a = dfs(e[i].to,min(e[i].flow,low)))){ 55 e[i].flow -= a; 56 e[i^1].flow += a; 57 tmp += a; 58 low -= a; 59 break; 60 } 61 } 62 if(!tmp) d[u] = -1; 63 return tmp; 64 } 65 bool vis[maxn]; 66 int maxcap; 67 void dfs2(int u){ 68 vis[u] = true; 69 for(int i = head[u]; ~i; i = e[i].next){ 70 if(!vis[e[i].to]){ 71 maxcap = max(maxcap,e[i^1].flow); 72 if(e[i^1].flow == 5){ 73 cout<<u<<" "<<e[i].to<<endl; 74 } 75 dfs2(e[i].to); 76 } 77 } 78 } 79 int main(){ 80 int p,cs,m,u,v,cap; 81 scanf("%d",&p); 82 while(p--){ 83 scanf("%d %d %d %d %d",&cs,&n,&m,&s,&t); 84 memset(head,-1,sizeof(head)); 85 for(int i = tot = 0; i < m; ++i){ 86 scanf("%d %d %d",&u,&v,&cap); 87 add(u,v,cap); 88 } 89 int ans = 0,o; 90 maxcap = 0; 91 while(bfs()){ 92 memcpy(cur,head,sizeof(head)); 93 o = dfs(s,INF); 94 ans += o; 95 maxcap = max(maxcap,o); 96 } 97 memset(vis,false,sizeof(vis)); 98 //maxcap = 0; 99 //dfs2(s); 100 printf("%d %.3f\n",cs,ans*1.0/maxcap); 101 //cout<<ans<<" "<<maxcap<<endl; 102 } 103 return 0; 104 }