首页 > 代码库 > HDU 4240 Route Redundancy

HDU 4240 Route Redundancy

Route Redundancy

Time Limit: 1000ms
Memory Limit: 32768KB
This problem will be judged on HDU. Original ID: 4240
64-bit integer IO format: %I64d      Java class name: Main
 
A city is made up exclusively of one-way steets.each street in the city has a capacity,which is the minimum of the capcities of the streets along that route.

The redundancy ratio from point A to point B is the ratio of the maximum number of cars that can get from point A to point B in an hour using all routes simultaneously,to the maximum number of cars thar can get from point A to point B in an hour using one route.The minimum redundancy ratio is the number of capacity of the single route with the laegest capacity.
 

Input

The first line of input contains asingle integer P,(1<=P<=1000),which is the number of data sets that follow.Each data set consists of several lines and represents a directed graph with positive integer weights.

The first line of each data set contains five apace separatde integers.The first integer,D is the data set number. The second integer,N(2<=N<=1000),is the number of nodes inthe graph. The thied integer,E,(E>=1),is the number of edges in the graph. The fourth integer,A,(0<=A<N),is the index of point A.The fifth integer,B,(o<=B<N,A!=B),is the index of point B.

The remaining E lines desceibe each edge. Each line contains three space separated in tegers.The First integer,U(0<=U<N),is the index of node U. The second integer,V(0<=v<N,V!=U),is the node V.The third integer,W (1<=W<=1000),is th capacity (weight) of path from U to V.
 

Output

For each data set there is one line of output.It contains the date set number(N) follow by a single space, followed by a floating-point value which is the minimum redundancy ratio to 3 digits after the decimal point.
 

Sample Input

11 7 11 0 60 1 30 3 31 2 42 0 32 3 12 4 23 4 23 5 64 1 14 6 15 6 9

Sample Output

1 1.667

Source

2011 Greater New York Regional
 
 
解题:最大流
 
  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 }
View Code

 

HDU 4240 Route Redundancy