首页 > 代码库 > 复习图---Delay Constrained Maximum Capacity Path(SPFA+二分)

复习图---Delay Constrained Maximum Capacity Path(SPFA+二分)

Delay Constrained Maximum Capacity Path
Time Limit:10000MS     Memory Limit:65535KB     64bit IO Format:%I64d & %I64u
Submit Status

Description

Consider an undirected graph with N vertices, numbered from 1 to N, and M edges. The vertex numbered with 1 corresponds to a mine from where some precious minerals are extracted. The vertex numbered with N corresponds to a minerals processing factory. Each edge has an associated travel time (in time units) and capacity (in units of minerals). It has been decided that the minerals which are extracted from the mine will be delivered to the factory using a single path. This path should have the highest capacity possible, in order to be able to transport simultaneously as many units of minerals as possible. The capacity of a path is equal to the smallest capacity of any of its edges. However, the minerals are very sensitive and, once extracted from the mine, they will start decomposing after T time units, unless they reach the factory within this time interval. Therefore, the total travel time of the chosen path (the sum of the travel times of its edges) should be less or equal to T. 
 

Input

The first line of input contains an integer number X, representing the number of test cases to follow. The first line of each test case contains 3 integer numbers, separated by blanks: N (2 <= N <= 10.000), M (1 <= M <= 50.000) and T (1 <= T <= 500.000). Each of the next M lines will contain four integer numbers each, separated by blanks: A, B, C and D, meaning that there is an edge between vertices A and B, having capacity C (1 <= C <= 2.000.000.000) and the travel time D (1 <= D <= 50.000). A and B are different integers between 1 and N. There will exist at most one edge between any two vertices. 
 

Output

For each of the X test cases, in the order given in the input, print one line containing the highest capacity of a path from the mine to the factory, considering the travel time constraint. There will always exist at least one path between the mine and the factory obbeying the travel time constraint. 
 

Sample Input

2 2 1 10 1 2 13 10 4 4 20 1 2 1000 15 2 4 999 6 1 3 100 15 3 4 99 4
 

Sample Output

13 99
 
给出n个点m条路和最大时间t,每条路有最大 容量和通过时间,找到在不超过t时间内,由1点到n点的最大的通过量的一条路

由1点到n点的通过量取决于在这条路上的最小的路的容量,这样按最终通过量来进行二分,枚举可能出现的通过量,计算最短时间,如果时间小于或等于最大时间t,将通过量上调,直到最短时间大于t,这样最后一次通过的 就是最大通过量。使用SPFA来简化计算量

原理:随着通过量的增加,会使可以使用的边变少,总会使求出的最短时间不变或是增加,这样就得到了通过量于最短时间的线性关系。最终只要最短时间不超过t就可以让通过量增加。


#include <cstdio>
#include <cstring>
#include <queue>
#include <algorithm>
#define INF 0x3f3f3f3f
using namespace std;
struct node{
    int x , y , v , w ;
    node *next ;
} *head[20000];
void add(int x,int y,int v,int w)
{
    node *q = new node ;
    q->x = x ;
    q->y = y ;
    q->v = v ;
    q->w = w ;
    q->next = head[x] ;
    head[x] = q ;
}
int dis[20000] , vis[20000] ;
queue <int> Q ;
int spfa(int n,int mid)
{
    memset(dis,INF,sizeof(dis));
    memset(vis,0,sizeof(vis));
    while( !Q.empty() )
        Q.pop() ;
    dis[1] = 0 ;
    vis[1] = 1 ;
    Q.push(1);
    while( !Q.empty() )
    {
        int x = Q.front() , y , v ;
        Q.pop() ;
        vis[x] = 0 ;
        for( node *q = head[x] ; q != NULL ; q = q->next )
        {
            x = q->x ;
            y = q->y ;
            v = q->v ;
            if( v >= mid && dis[y] > dis[x] + q->w )
            {
                dis[y] = dis[x] + q->w ;
                if( !vis[y] )
                {
                    vis[y] = 1;
                    Q.push(y) ;
                }
            }
        }
    }
    return dis[n] ;
}
int main()
{
    int t , i , j , n , m , time ;
    node *q ;
    scanf("%d", &t);
    while(t--)
    {
        memset(head,NULL,sizeof(head));
        scanf("%d %d %d", &n, &m, &time);
        int x , y , v , w , low = INF , top = 0 ;
        for(i = 0 ; i < m ; i++)
        {
            scanf("%d %d %d %d", &x, &y, &v, &w);
            add(x,y,v,w);
            add(y,x,v,w);
            low = min(low,v);
            top = max(top,v);
        }
        int mid , ans ;
        while(low <= top)
        {
            mid = (low + top)/2;
            if( spfa(n,mid) <= time )
            {
                ans = mid ;
                low = mid + 1;
            }
            else
                top = mid - 1 ;
        }
        printf("%d\n", ans);
    }
    return 0;
}