Route Redundancy
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 488 Accepted Submission(s): 279
Problem Description
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.
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.
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
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
Sample Output
1 1.667
A到B的冗余率:(从A到B的最大流 ) / (A到 B 一条路径上可通过的最大容量)。
题意:现在给你N个点,E条边和各边的起点终点以及容量,让你求A到B的最小冗余率。
思路:先dinic求出最大流,然后从A到B跑一遍SPFA 更新A到路径上各点的最大容量。
详细看代码:
#include <cstdio>
#include <cstring>
#include <queue>
#include <stack>
#include <vector>
#include <algorithm>
#define MAXN 2000
#define MAXM 4000000
#define INF 10000000
using namespace std;
struct Edge
{
int from, to, cap, flow, next;
}edge[MAXM];
int head[MAXN], edgenum;
int cur[MAXN];//当前查询弧
int dist[MAXN];
bool vis[MAXN];
int N, E, A, B;
void init()
{
edgenum = 0;
memset(head, -1, sizeof(head));
}
void addEdge(int u, int v, int w)
{
Edge E1 = {u, v, w, 0, head[u]};
edge[edgenum] = E1;
head[u] = edgenum++;
Edge E2 = {v, u, 0, 0, head[v]};
edge[edgenum] = E2;
head[v] = edgenum++;
}
void getMap()
{
int a, b, c;
while(E--)
{
scanf("%d%d%d", &a, &b, &c);
addEdge(a, b, c);
}
}
bool BFS(int sx, int ex)
{
queue<int> Q;
memset(dist, -1, sizeof(dist));
memset(vis, false, sizeof(vis));
dist[sx] = 0;
vis[sx] = true;
Q.push(sx);
while(!Q.empty())
{
int u = Q.front();
Q.pop();
for(int i = head[u]; i != -1; i = edge[i].next)
{
Edge E = edge[i];
if(!vis[E.to] && E.cap > E.flow)
{
vis[E.to] = true;
dist[E.to] = dist[u] + 1;
if(E.to == ex) return true;
Q.push(E.to);
}
}
}
return false;
}
int DFS(int x, int a, int end)
{
if(x == end || a == 0) return a;
int flow = 0, f;
for(int &i = cur[x]; i != -1; i = edge[i].next)
{
Edge &E = edge[i];
if(dist[E.to] == dist[x] + 1 && (f = DFS(E.to, min(a, E.cap - E.flow), end)) > 0)
{
E.flow += f;
edge[i^1].flow -= f;
flow += f;
a -= f;
if(a == 0) break;
}
}
return flow;
}
int Maxflow(int sx, int ex)
{
int flow = 0;
while(BFS(sx, ex))//找到增广路
{
memcpy(cur, head, sizeof(head));
flow += DFS(sx, INF, ex);
}
return flow;
}
int Flow[MAXN];//Flow[i]存储起点到i的 一条路上的最大流量
int SPFA()//找一条流量最大的路
{
queue<int> Q;
memset(Flow, 0, sizeof(Flow));
Flow[A] = INF;//初始化为无穷大
Q.push(A);
while(!Q.empty())
{
int u = Q.front();
Q.pop();
for(int i = head[u]; i != -1; i = edge[i].next)
{
int v = edge[i].to;//终点
int flow = edge[i].cap;//这条边上的容量
flow = min(flow, Flow[u]);//取较小值 因为流量不能超过这条边的容量
if(flow > Flow[v])//满足 更新
{
Flow[v] = flow;
Q.push(v);
}
}
}
}
int main()
{
int t, k;
scanf("%d", &t);
while(t--)
{
scanf("%d%d%d%d%d", &k, &N, &E, &A, &B);
init();
getMap();
SPFA();
printf("%d %.3lf\n", k, Maxflow(A, B) / (Flow[B] * 1.0));
}
return 0;
}