HDU 4240 route Redundancy

Source: Internet
Author: User
Route redundancytime limit: 1000 msmemory limit: 32768 kbthis 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. inputthe 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. the th integer, B, (O <= B <n,! = 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. outputfor 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
Source2011 greater New York Regional solution: Maximum stream
  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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.